From 5609772cd3b43307d13a1b3a0d5f378fc31f816d Mon Sep 17 00:00:00 2001 From: Adam Tankanow Date: Fri, 9 Jan 2015 07:16:22 -0500 Subject: [PATCH] Add docstring and support for creating links for header id attributes - this method will now look for both of the following references, this commit adds support for (2) (1)

Reference Text

and (2)

Reference Text

--- src/cryogen_core/toc.clj | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/cryogen_core/toc.clj b/src/cryogen_core/toc.clj index a1021d5..871dba4 100644 --- a/src/cryogen_core/toc.clj +++ b/src/cryogen_core/toc.clj @@ -18,16 +18,26 @@ headings))) [] content)) -(defn make-links [headings] +(defn make-links + "Create a table of contents from the given headings. This function will look + for either: + (1) headings with a child anchor with a non-nil name attribute, e.g. +

Reference Title

+ or + (2) headings with an id attribute, e.g.

Reference Title

+ In both cases above, the anchor reference becomes \"#reference\" and the + anchor text is \"Reference Title\"." + [headings] (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 "
    " (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) "
")) - (hiccup/html entry)) tag)))) + (if-let [{tag :tag {id :id} :attrs [{{name :name} :attrs} title :as htext] :content} (first items)] + (let [anchor (or id name)] + (if (nil? anchor) (recur (rest items) acc nil) + (let [entry [:li [:a {:href (str "#" anchor)} (or title (first htext))]] + jump (compare_index _last tag)] + (cond (> jump 0) (recur (rest items) (str acc "
    " (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) "
")) + (hiccup/html entry)) tag))))) (str acc "")))) (defn generate-toc [html]