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
(:require [selmer.parser :refer [cache-off! render-file]]
[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.rss :as rss]
[io.aviso.exception :refer [write-exception]]
@ -288,6 +289,7 @@
(wipe-public-folder keep-files)
(println (blue "copying resources"))
(copy-resources config)
(copy-images-from-markdown-folders config)
(compile-pages default-params pages config)
(compile-posts default-params posts config)
(compile-tags default-params posts-by-tag config)

View file

@ -1,6 +1,7 @@
(ns cryogen-core.io
(: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")
@ -26,6 +27,11 @@
(doseq [path (.listFiles (file public) filenamefilter)]
(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]}]
(doseq [resource resources]
(let [src (str "resources/templates/" resource)

View file

@ -2,10 +2,13 @@
(:require [crouton.html :as html]
[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]
(reduce
(fn [headings {:keys [tag attrs content] :as elm}]
(if (some #{tag} [:h1 :h2 :h3])
(if (some #{tag} _h)
(conj headings elm)
(if-let [more-headings (get-headings content)]
(into headings more-headings)
@ -13,9 +16,16 @@
[] content))
(defn make-links [headings]
(into [:ol.contents]
(for [{[{{name :name} :attrs} title] :content} headings]
[:li [:a {:href (str "#" name)} title]])))
(loop [items headings acc nil _last nil]
(if-let [{tag :tag [{{name :name} :attrs} title] :content} (first items)]
(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]
(-> html
@ -25,4 +35,4 @@
:content
(get-headings)
(make-links)
(hiccup/html)))
(clojure.string/replace-first #"ol" "ol class=\"contents\"")))