From bf98817cf183ec6cda5047b056b5173ddd9eff5e Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Wed, 2 Jan 2019 15:08:30 +0100 Subject: [PATCH] some code refactoring --- src/equilibrium/blog.clj | 25 ++++++++++++++++ src/equilibrium/core.clj | 7 +++++ src/equilibrium/render.clj | 58 ++++---------------------------------- 3 files changed, 37 insertions(+), 53 deletions(-) diff --git a/src/equilibrium/blog.clj b/src/equilibrium/blog.clj index ec45296..f4ee28a 100644 --- a/src/equilibrium/blog.clj +++ b/src/equilibrium/blog.clj @@ -70,3 +70,28 @@ :let [target (site->target-file config output-root site)]] (spit target (fill-in-fn site)))) + +(defn sites-preview [sites] + (reduce (fn [res site] + (let [content (:content site) + divider-pos (string/index-of content "

") + length (count content) + end (or divider-pos length) + pre-content (subs content 0 end) + article-link (str "" + "↪" + (when divider-pos "…") + "")] + (str res + "
" + pre-content + "

" + article-link + "

" + "
"))) + "" + (take-last + 5 + (reverse sites)))) diff --git a/src/equilibrium/core.clj b/src/equilibrium/core.clj index df5ee30..d6fff8e 100644 --- a/src/equilibrium/core.clj +++ b/src/equilibrium/core.clj @@ -11,3 +11,10 @@ .toPath .getFileName .toString)) + +(defn env-val [env-name] + (let [env-val (System/getenv env-name)] + (when (= nil env-val) + (throw (ex-info (str "env var not found " env-name) + {:env-var env-name}))) + env-val)) diff --git a/src/equilibrium/render.clj b/src/equilibrium/render.clj index bef6a7a..8b3520c 100644 --- a/src/equilibrium/render.clj +++ b/src/equilibrium/render.clj @@ -25,22 +25,6 @@ (defn index-template [] (slurp (str input-root "/index.html"))) -(comment - (defn navcode [sites] - (let [sorted-sites (sort-by #(core/filename %) sites) - clean-file-name-fn #(core/clean-name (core/filename %)) - nav-line-fn #(str "
  • " - "" - (clean-file-name-fn %) - "
  • ")] - (str "
      " - (reduce #(str %1 (nav-line-fn %2)) - "" - sorted-sites) - "
    ")))) - (defn site-destination [filename] (str output-root "/" @@ -129,36 +113,8 @@ (comment (particle-content "aside")) -(defn blog-sites-preview [blog-name] - (reduce (fn [res site] - (let [content (:content site) - divider-pos (string/index-of content "

    ") - length (count content) - end (or divider-pos length) - pre-content (subs content 0 end) - article-link (str "" - "↪" - (when divider-pos - "…") - "")] - (str res - "
    " - pre-content - "

    " - article-link - "

    " - "
    "))) - "" - (->> blog-name - blog-sites - (take-last 5) - reverse))) - (defn fill-in-placeholders [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 #"&:generated-at" (now-str))) @@ -171,18 +127,14 @@ (first particle) (particle-content (second particle)))) blog-sites-fn (fn [result [match-str blog-name]] - (string/replace - result - match-str - (blog-sites-preview blog-name))) + (let [sites (blog-sites blog-name)] + (string/replace result + match-str + (blog/sites-preview sites)))) envs-fn (fn [result [match-str env-name]] - (when (= nil (System/getenv env-name)) - (throw (ex-info (str "env var not found " env-name) - {:env-var env-name - :match-str match-str}))) (string/replace result match-str - (System/getenv env-name)))] + (core/env-val env-name)))] (as-> simple $ (reduce particle-fn $ particles) (reduce blog-sites-fn $ last-blog-sites)