Make site generation pure functional

This commit is contained in:
Dan Holmsand 2015-09-08 17:50:50 +02:00
parent 0847dcc9ed
commit 573a51bc67
1 changed files with 23 additions and 28 deletions

View File

@ -120,18 +120,12 @@
(let [depth (-> #"/" (re-seq (as-relative page)) count)] (let [depth (-> #"/" (re-seq (as-relative page)) count)]
(str (->> "../" (repeat depth) (apply str)) href))) (str (->> "../" (repeat depth) (apply str)) href)))
(defn body []
(let [b (:body @config)]
(assert (vector? b) (str "body is not a vector: " b))
b))
(defn danger [t s] (defn danger [t s]
[t {:dangerouslySetInnerHTML {:__html s}}]) [t {:dangerouslySetInnerHTML {:__html s}}])
(defn html-template [{:keys [title body timestamp page-conf (defn html-template [{:keys [title body-html timestamp page-conf
req]}] js-file css-file main-div]}]
(let [{:keys [js-file css-file main-div]} @config (let [main (str js-file timestamp)]
main (str js-file timestamp)]
(r/render-to-static-markup (r/render-to-static-markup
[:html [:html
[:head [:head
@ -142,19 +136,20 @@
[:link {:href (str css-file timestamp) :rel 'stylesheet}] [:link {:href (str css-file timestamp) :rel 'stylesheet}]
[:title title]] [:title title]]
[:body [:body
[:div {:id main-div} (danger :div body)] [:div {:id main-div} (danger :div body-html)]
(danger :script (str "var pageConfig = " (danger :script (str "var pageConfig = "
(-> page-conf clj->js js/JSON.stringify))) (-> page-conf clj->js js/JSON.stringify)))
[:script {:src main :type "text/javascript"}]]]))) [:script {:src main :type "text/javascript"}]]])))
(defn gen-page [page-name timestamp] (defn gen-page [page-name conf]
(dispatch [:set-page page-name]) (dispatch [:set-page page-name])
(let [b (r/render-component-to-string (body))] (let [b (:body conf)
_ (assert (vector? b))
bhtml (r/render-component-to-string b)]
(str "<!doctype html>" (str "<!doctype html>"
(html-template {:title (:title @config) (html-template (assoc conf
:body b :page-conf {:page-name page-name}
:page-conf {:page-name page-name} :body-html bhtml)))))
:timestamp timestamp}))))
(defn mkdirs [f] (defn mkdirs [f]
(let [fs (js/require "fs") (let [fs (js/require "fs")
@ -177,12 +172,12 @@
(defn path-join [& paths] (defn path-join [& paths]
(apply (.' (js/require "path") :join) paths)) (apply (.' (js/require "path") :join) paths))
(defn read-css [] (defn read-css [{cssfiles :css-infiles}]
(string/join "\n" (map read-file (:css-infiles @config)))) (string/join "\n" (map read-file cssfiles)))
(defn write-resources [dir] (defn write-resources [dir {file :css-file :as conf}]
(write-file (path-join dir (:css-file @config)) (write-file (path-join dir file)
(read-css))) (read-css conf)))
;;; Main entry points ;;; Main entry points
@ -190,12 +185,12 @@
(defn ^:export genpages [opts] (defn ^:export genpages [opts]
(log "Generating site") (log "Generating site")
(swap! config merge (js->clj opts :keywordize-keys true)) (swap! config merge (js->clj opts :keywordize-keys true))
(let [dir (:site-dir @config) (let [{:keys [site-dir pages] :as conf} @config
timestamp (str "?" (js/Date.now))] conf (assoc conf :timestamp (str "?" (js/Date.now)))]
(doseq [f (:pages @config)] (doseq [f pages]
(write-file (path-join dir (dbg (as-relative f))) (write-file (path-join site-dir (as-relative f))
(gen-page f timestamp))) (gen-page f conf)))
(write-resources dir)) (write-resources site-dir conf))
(log "Wrote site")) (log "Wrote site"))
(defn start! [site-config] (defn start! [site-config]
@ -205,5 +200,5 @@
(js->clj js/pageConfig :keywordize-keys true))] (js->clj js/pageConfig :keywordize-keys true))]
(swap! config merge conf) (swap! config merge conf)
(init-history) (init-history)
(r/render-component (body) (r/render-component (:body @config)
(js/document.getElementById (:main-div @config)))))) (js/document.getElementById (:main-div @config))))))