From 89c6377a9be4ae7887df7a78b0a5044ef16f725c Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Tue, 13 Nov 2018 18:40:35 +0100 Subject: [PATCH] first shot at blogs --- resources/page/blogs/datblag/index.md | 0 resources/page/blogs/datblag/index.md~ | 4 ++ src/leiningen/render.clj | 78 +++++++++++++++++++++++--- 3 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 resources/page/blogs/datblag/index.md create mode 100644 resources/page/blogs/datblag/index.md~ diff --git a/resources/page/blogs/datblag/index.md b/resources/page/blogs/datblag/index.md new file mode 100644 index 0000000..e69de29 diff --git a/resources/page/blogs/datblag/index.md~ b/resources/page/blogs/datblag/index.md~ new file mode 100644 index 0000000..185ef3c --- /dev/null +++ b/resources/page/blogs/datblag/index.md~ @@ -0,0 +1,4 @@ +--- +{:title "datblag"} +--- + diff --git a/src/leiningen/render.clj b/src/leiningen/render.clj index a23d13e..228e792 100644 --- a/src/leiningen/render.clj +++ b/src/leiningen/render.clj @@ -5,6 +5,7 @@ (:import java.time.LocalDateTime)) (def input-root "resources/page") +(def output-root "target/page") (defn filename [file] (-> file @@ -48,7 +49,8 @@ ""))) (defn site-destination [filename] - (str "target/page/" + (str output-root + "/" (clean-name filename) ".html")) @@ -99,9 +101,9 @@ (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])) + (string/replace #"&:nav" (:navcode context "")) + (string/replace #"&:content" (:content page "")) + (string/replace #"&:title" (get-in page [:metadata :title] "")) (string/replace #"&:generated-at" (now-str))) particles (particles-in-content simple)] (reduce (fn [result particle] @@ -141,16 +143,78 @@ (clear-dir file)) (.delete file)))) +(defn blog-dirs [] + (->> (str input-root "/blogs") + io/file + .listFiles + (filter #(.isDirectory %)) + (sort-by #(filename %)))) + +(comment + (blog-dirs)) + +(defn blog-index-file [dir-name] + (io/file (str input-root "/blogs/" dir-name "/index.md"))) + +(defn analysed-blog-dir-name [dir-name] + (let [name-parts (string/split dir-name #"\+")] + (case (count name-parts) + 1 ["" (first name-parts)] + 2 [(first name-parts) (second name-parts)]))) + +(comment + (analysed-blog-dir-name "hudel") + (analysed-blog-dir-name "4711+wudel")) + +(defn blog-config [dir-name] + (let [[ix blog-dir-name] (analysed-blog-dir-name dir-name) + analysed-index (analysed-page (blog-index-file dir-name))] + (merge {:ix ix + :blog-dir-name blog-dir-name} + (update-in analysed-index + [:metadata :page-size] + #(or % 10))))) + +(comment + (blog-index-file "datblag") + (blog-config "datblag")) + +(defn blog-target-root [config] + (str output-root + "/" + (:blog-dir-name config))) + +(defn blog-sites-dir [dir-name] + (str input-root "/blogs/" dir-name "/entries")) + +(comment + (blog-target-root {:blog-dir-name "hudel"}) + (blog-sites-dir "datblag")) + +(defn render-blog [context template dir] + (let [dir-name (filename dir) + config (merge (blog-config dir-name) + {:dir dir}) + target-root (blog-target-root config) + blog-index-file (io/file (str target-root "/index.html")) + blog-sites (md-files (blog-sites-dir dir-name))] + (io/make-parents blog-index-file) + (spit blog-index-file + (fill-in-placeholders context template config)) + config)) + (defn render "Renders all markdown pages under resources/user using the structure in resources/user/structure to target/page" [project] - (clear-dir (io/file "target/page")) - (.mkdirs (io/file "target/page")) + (clear-dir (io/file output-root)) + (io/make-parents output-root) (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))) + (write-sites context template rendered-sites) + (for [blog-dir (blog-dirs)] + (render-blog context template blog-dir)))) (comment (render {})