From f59cf5641b7f33be9b8db293f2c13c60f92c03c0 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Thu, 15 Nov 2018 16:35:16 +0100 Subject: [PATCH] move blog logic to own ns --- src/equilibrium/blog.clj | 48 ++++++++++++++++++++++++++++++++ src/equilibrium/render.clj | 57 ++++++++------------------------------ 2 files changed, 59 insertions(+), 46 deletions(-) create mode 100644 src/equilibrium/blog.clj diff --git a/src/equilibrium/blog.clj b/src/equilibrium/blog.clj new file mode 100644 index 0000000..10bfd82 --- /dev/null +++ b/src/equilibrium/blog.clj @@ -0,0 +1,48 @@ +(ns equilibrium.blog + (:require [clojure.java.io :as io] + [clojure.string :as string])) + +(defn index-file [dir-name root] + (io/file (str root "/blogs/" dir-name "/index.md"))) + +(defn sites-dir [dir-name root] + (str root "/blogs/" dir-name "/entries")) + +(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 config [dir-name analysed-index] + (let [[ix blog-dir-name] (analysed-blog-dir-name 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 target-root [config root] + (str root + "/" + (:blog-dir-name config))) + +(comment + (blog-target-root {:blog-dir-name "hudel"}) + (blog-sites-dir "datblag")) + +(defn write-files [config output-root site-files fill-in-fn] + (let [index-file (io/file (str (target-root config + output-root) + "/index.html"))] + (io/make-parents index-file) + (spit index-file + (fill-in-fn config)))) diff --git a/src/equilibrium/render.clj b/src/equilibrium/render.clj index 6724cb9..dd9689a 100644 --- a/src/equilibrium/render.clj +++ b/src/equilibrium/render.clj @@ -1,7 +1,8 @@ (ns equilibrium.render (:require [clojure.java.io :as io] [clojure.string :as string] - [markdown.core :as md]) + [markdown.core :as md] + [equilibrium.blog :as blog]) (:import java.time.LocalDateTime)) (def input-root "resources/page") @@ -153,54 +154,18 @@ (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) + index-file (blog/index-file dir-name input-root) + config (merge (blog/config dir-name + (analysed-page index-file)) {: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)) + blog-sites (md-files (blog/sites-dir dir-name input-root))] + (blog/write-files config + output-root + blog-sites + (partial fill-in-placeholders + context template)) config)) (defn render