From e763984e595e587dc81541e2335aa19b35126908 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Mon, 12 Nov 2018 19:52:13 +0100 Subject: [PATCH] remoce render code from core namespace --- src/leiningen/equilibrium.clj | 146 +--------------------------------- src/leiningen/render.clj | 140 ++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+), 142 deletions(-) create mode 100644 src/leiningen/render.clj diff --git a/src/leiningen/equilibrium.clj b/src/leiningen/equilibrium.clj index 130c813..bcec78d 100644 --- a/src/leiningen/equilibrium.clj +++ b/src/leiningen/equilibrium.clj @@ -1,144 +1,6 @@ (ns leiningen.equilibrium - (:require [leiningen.core.main :refer [info warn]] - [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 "
  • " - "" - (clean-file-name-fn %) - "
  • ")] - (str "
      " - (reduce #(str %1 (nav-line-fn %2)) - "" - sorted-sites) - "
    "))) - -(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")))) + (:require [leiningen.render :as render] + [leiningen.core.main :refer [info warn]])) (defn deploy "Deploys the rendered page under target/page to the configured ftp target" @@ -148,12 +10,12 @@ (warn "refer to 'lein help equilibrium' for available tasks")) (def argmapping - {:render render + {:render render/render :deploy deploy :? invalid-input}) (defn equilibrium - {:subtasks [#'render #'deploy]} + {:subtasks [#'render/render #'deploy]} [project & args] (let [kwarg (keyword (or (first args) "?")) callfn (kwarg argmapping invalid-input)] diff --git a/src/leiningen/render.clj b/src/leiningen/render.clj new file mode 100644 index 0000000..28a515d --- /dev/null +++ b/src/leiningen/render.clj @@ -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 "
  • " + "" + (clean-file-name-fn %) + "
  • ")] + (str "
      " + (reduce #(str %1 (nav-line-fn %2)) + "" + sorted-sites) + "
    "))) + +(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"))))