parent
fe03d24074
commit
e763984e59
@ -0,0 +1,140 @@
|
||||
(ns leiningen.render
|
||||
(:require [clojure.java.io :as io]
|
||||
[clojure.string :as string]
|
||||
[markdown.core :as md]))
|
||||
|
||||
(def input-root "resources/page")
|
||||
|
||||
(defn filename [file]
|
||||
(-> file
|
||||
.toPath
|
||||
.getFileName
|
||||
.toString))
|
||||
|
||||
(defn is-md-file? [file]
|
||||
(.endsWith (filename file) ".md"))
|
||||
|
||||
(defn md-files [root]
|
||||
(->> root
|
||||
io/file
|
||||
file-seq
|
||||
(filter is-md-file?)))
|
||||
|
||||
(defn render-markdown [content]
|
||||
(md/md-to-html-string content))
|
||||
|
||||
(defn index-template []
|
||||
(slurp (str input-root "/index.html")))
|
||||
|
||||
(defn clean-name [filename]
|
||||
(let [end (- (count filename) 3)
|
||||
start (inc (or (string/index-of filename "+") -1))]
|
||||
(subs filename start end)))
|
||||
|
||||
(defn navcode [sites]
|
||||
(let [sorted-sites (sort-by #(filename %) sites)
|
||||
clean-file-name-fn #(clean-name (filename %))
|
||||
nav-line-fn #(str "<li>"
|
||||
"<a href=\""
|
||||
(clean-file-name-fn %)
|
||||
".html\">"
|
||||
(clean-file-name-fn %)
|
||||
"</a></li>")]
|
||||
(str "<ol>"
|
||||
(reduce #(str %1 (nav-line-fn %2))
|
||||
""
|
||||
sorted-sites)
|
||||
"</ol>")))
|
||||
|
||||
(defn site-destination [filename]
|
||||
(str "target/page/"
|
||||
(clean-name filename)
|
||||
".html"))
|
||||
|
||||
(comment
|
||||
(site-destination "01+allo.md")
|
||||
(site-destination "aside.md"))
|
||||
|
||||
(defn particle-content [particle]
|
||||
(-> (str input-root "/particles/" particle ".md")
|
||||
io/file
|
||||
slurp
|
||||
render-markdown))
|
||||
|
||||
(comment
|
||||
(particle-content "aside"))
|
||||
|
||||
(def metadata-regex #"^---\n(.+?)\n---")
|
||||
|
||||
(defn page-metadata [content]
|
||||
(let [metadata (re-find metadata-regex content)]
|
||||
(if metadata
|
||||
(read-string (second metadata))
|
||||
{})))
|
||||
|
||||
(comment
|
||||
(page-metadata "---\nDINGE\n---\nwullwupp")
|
||||
(page-metadata "---\n{:a \"b\"}\n---dingedinge")
|
||||
(page-metadata "---\n{:a \"b\"}---dingedinge")
|
||||
(page-metadata "aiuaiaia"))
|
||||
|
||||
(defn page-content [content]
|
||||
(if-let [metadata (re-find metadata-regex content)]
|
||||
(subs content (inc (count (first metadata))))
|
||||
content))
|
||||
|
||||
(comment
|
||||
(page-content "---\nDINGE\n---\nwulle")
|
||||
(page-content "aiaiaiai"))
|
||||
|
||||
(defn particles-in-content [content]
|
||||
(re-seq #"&particle:([^\s]+)" content))
|
||||
|
||||
(comment
|
||||
(particles-in-content "asd &particle:hullu"))
|
||||
|
||||
(defn fill-in-placeholders [context html page]
|
||||
(let [simple (-> html
|
||||
(string/replace #"&nav" (:navcode context))
|
||||
(string/replace #"&content" (:content page))
|
||||
(string/replace #"&title" (get-in page [:metadata :title])))
|
||||
particles (particles-in-content simple)]
|
||||
(reduce (fn [result particle]
|
||||
(string/replace result
|
||||
(first particle)
|
||||
(particle-content (second particle))))
|
||||
simple
|
||||
particles)))
|
||||
|
||||
(comment
|
||||
(fill-in-placeholders {:navcode "hhh"}
|
||||
"&nav &content &particle:aside\n "
|
||||
{:content "dinge"})
|
||||
)
|
||||
|
||||
(defn write-sites [context template sites]
|
||||
(doseq [site sites
|
||||
:let [translated (fill-in-placeholders context template site)
|
||||
filename (filename (:file site))
|
||||
destination (site-destination filename)]]
|
||||
(spit destination translated)))
|
||||
|
||||
(defn analysed-page [file]
|
||||
(let [content (slurp file)]
|
||||
{:file file
|
||||
:content (-> content page-content render-markdown)
|
||||
:metadata (page-metadata content)}))
|
||||
|
||||
(defn render
|
||||
"Renders all markdown pages under resources/user using the structure in resources/user/structure to target/page"
|
||||
[project]
|
||||
(.mkdirs (io/file "target/page"))
|
||||
(let [sites (md-files (str input-root "/sites"))
|
||||
rendered-sites (map analysed-page sites)
|
||||
template (index-template)
|
||||
context {:navcode (navcode sites)}]
|
||||
(write-sites context template rendered-sites)))
|
||||
|
||||
(comment
|
||||
(render {})
|
||||
(doall (navcode (md-files "resources/user/sites"))))
|
Loading…
Reference in new issue