cryogen-core/src/cryogen_core/toc.clj

29 lines
749 B
Clojure
Raw Normal View History

2014-12-05 15:56:40 +00:00
(ns cryogen-core.toc
2014-12-04 16:38:48 +00:00
(:require [crouton.html :as html]
[hiccup.core :as hiccup]))
(defn get-headings [content]
(reduce
(fn [headings {:keys [tag attrs content] :as elm}]
(if (some #{tag} [:h1 :h2 :h3])
(conj headings elm)
(if-let [more-headings (get-headings content)]
(into headings more-headings)
headings)))
[] content))
(defn make-links [headings]
(into [:ol.contents]
(for [{[{{name :name} :attrs} title] :content} headings]
[:li [:a {:href (str "#" name)} title]])))
(defn generate-toc [html]
(-> html
(.getBytes)
(java.io.ByteArrayInputStream.)
(html/parse)
:content
(get-headings)
(make-links)
(hiccup/html)))