You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.4 KiB

(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-page []
(slurp (str input-root "/structure/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 [nav+content (-> html
(string/replace #"&nav" (:navcode context))
(string/replace #"&content" (:content page)))
particles (particles-in-content nav+content)]
(reduce (fn [result particle]
(println particle)
(string/replace result
(first particle)
(particle-content (second particle))))
nav+content
particles)))
(comment
(fill-in-placeholders {:navcode "hhh"}
"&nav &content &particle:aside\n "
{:content "dinge"})
)
(defn write-sites [context index-page sites]
(doseq [site sites
:let [translated (fill-in-placeholders context index-page 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]
(.mkdir (io/file "target/page"))
(let [sites (md-files (str input-root "/sites"))
rendered-sites (map analysed-page sites)
index-page (index-page)
context {:navcode (navcode sites)}]
(write-sites context index-page rendered-sites)))
(comment
(render {})
(doall (navcode (md-files "resources/user/sites"))))
(defn deploy
"Deploys the rendered page under target/page to the configured ftp target"
[project])
(defn invalid-input [project]
(warn "refer to 'lein help equilibrium' for available tasks"))
(def argmapping
{:render render
:deploy deploy
:? invalid-input})
(defn equilibrium
{:subtasks [#'render #'deploy]}
[project & args]
(let [kwarg (keyword (or (first args) "?"))
callfn (kwarg argmapping invalid-input)]
(callfn project)))