Merge pull request #2 from kremers/feature_toc

Enable multilevel table of contents
This commit is contained in:
Carmen La 2014-12-14 12:32:44 -05:00
commit 749eb7bb53
3 changed files with 25 additions and 7 deletions

View file

@ -1,7 +1,8 @@
(ns cryogen-core.compiler (ns cryogen-core.compiler
(:require [selmer.parser :refer [cache-off! render-file]] (:require [selmer.parser :refer [cache-off! render-file]]
[cryogen-core.io :refer [cryogen-core.io :refer
[get-resource find-assets create-folder wipe-public-folder copy-resources]] [get-resource find-assets create-folder wipe-public-folder copy-resources
copy-images-from-markdown-folders]]
[cryogen-core.sitemap :as sitemap] [cryogen-core.sitemap :as sitemap]
[cryogen-core.rss :as rss] [cryogen-core.rss :as rss]
[io.aviso.exception :refer [write-exception]] [io.aviso.exception :refer [write-exception]]
@ -288,6 +289,7 @@
(wipe-public-folder keep-files) (wipe-public-folder keep-files)
(println (blue "copying resources")) (println (blue "copying resources"))
(copy-resources config) (copy-resources config)
(copy-images-from-markdown-folders config)
(compile-pages default-params pages config) (compile-pages default-params pages config)
(compile-posts default-params posts config) (compile-posts default-params posts config)
(compile-tags default-params posts-by-tag config) (compile-tags default-params posts-by-tag config)

View file

@ -1,6 +1,7 @@
(ns cryogen-core.io (ns cryogen-core.io
(:require [clojure.java.io :refer [file]] (:require [clojure.java.io :refer [file]]
[me.raynes.fs :as fs])) [me.raynes.fs :as fs]
[clojure.java.io :as io]))
(def public "resources/public") (def public "resources/public")
@ -26,6 +27,11 @@
(doseq [path (.listFiles (file public) filenamefilter)] (doseq [path (.listFiles (file public) filenamefilter)]
(fs/delete-dir path)))) (fs/delete-dir path))))
(defn copy-images-from-markdown-folders [{:keys [blog-prefix]}]
(let [blog-prefix-relative (if (= \/ (first blog-prefix)) (subs blog-prefix 1) blog-prefix)]
(doseq [asset (fs/find-files "resources/templates" #".+(jpg|jpeg|png|gif)")]
(fs/copy asset (io/file public blog-prefix-relative "img" (.getName asset))))))
(defn copy-resources [{:keys [blog-prefix resources]}] (defn copy-resources [{:keys [blog-prefix resources]}]
(doseq [resource resources] (doseq [resource resources]
(let [src (str "resources/templates/" resource) (let [src (str "resources/templates/" resource)

View file

@ -2,10 +2,13 @@
(:require [crouton.html :as html] (:require [crouton.html :as html]
[hiccup.core :as hiccup])) [hiccup.core :as hiccup]))
(def _h [:h1 :h2 :h3 :h4 :h5 :h6])
(defn- compare_index [i1 i2] (- (.indexOf _h i2) (.indexOf _h i1)))
(defn get-headings [content] (defn get-headings [content]
(reduce (reduce
(fn [headings {:keys [tag attrs content] :as elm}] (fn [headings {:keys [tag attrs content] :as elm}]
(if (some #{tag} [:h1 :h2 :h3]) (if (some #{tag} _h)
(conj headings elm) (conj headings elm)
(if-let [more-headings (get-headings content)] (if-let [more-headings (get-headings content)]
(into headings more-headings) (into headings more-headings)
@ -13,9 +16,16 @@
[] content)) [] content))
(defn make-links [headings] (defn make-links [headings]
(into [:ol.contents] (loop [items headings acc nil _last nil]
(for [{[{{name :name} :attrs} title] :content} headings] (if-let [{tag :tag [{{name :name} :attrs} title] :content} (first items)]
[:li [:a {:href (str "#" name)} title]]))) (if (nil? name) (recur (rest items) acc nil)
(let [entry [:li [:a {:href (str "#" name)} title]]
jump (compare_index _last tag)]
(cond (> jump 0) (recur (rest items) (str acc "<ol>" (hiccup/html entry)) tag)
(= jump 0) (recur (rest items) (str acc (hiccup/html entry)) tag)
(< jump 0) (recur (rest items) (str acc (apply str (repeat (* -1 jump) "</ol>"))
(hiccup/html entry)) tag))))
(str acc "</ol>"))))
(defn generate-toc [html] (defn generate-toc [html]
(-> html (-> html
@ -25,4 +35,4 @@
:content :content
(get-headings) (get-headings)
(make-links) (make-links)
(hiccup/html))) (clojure.string/replace-first #"ol" "ol class=\"contents\"")))