From 446e05adecd89cb4913fd029290b50688006362e Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Sun, 19 Jan 2014 12:13:19 +0100 Subject: [PATCH] Move page generation to ClojureScript --- .gitignore | 4 ++-- Makefile | 2 ++ bin/gen-site.js | 51 ++++++++++++++++++++----------------------------- demo/demo.cljs | 47 ++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 67 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index 17065e8..04c5219 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ index.html -site/demo.js -site/democss.css +assets/ +news/ target pom.xml .lein-repl-history diff --git a/Makefile b/Makefile index 080d0ef..2311413 100644 --- a/Makefile +++ b/Makefile @@ -32,9 +32,11 @@ preclean: rm -rf repl .repl target out clean: preclean + rm -rf news assets lein -o clean setup: preclean copyjs + mkdir -p news assets show-outdated: lein ancient :all diff --git a/bin/gen-site.js b/bin/gen-site.js index 3e6f5d5..4119972 100755 --- a/bin/gen-site.js +++ b/bin/gen-site.js @@ -3,42 +3,33 @@ var fs = require("fs"); var vm = require('vm'); -var srcFile = "target/cljs-client.js"; -var src = fs.readFileSync(srcFile); -vm.runInThisContext(src, srcFile); - -console.log('Generating page'); -var main = demo.genpage(); - -var ts = '?' + Date.now(); - var cssFiles = ['examples/todomvc/todos.css', 'examples/todomvc/todosanim.css', 'examples/simple/example.css', 'site/demo.css']; -var head = ['', - '', - 'Cloact: Minimalistic React for ClojureScript', - '', - '', - ''].join('\n'); +var srcFile = "target/cljs-client.js"; +var src = fs.readFileSync(srcFile); -var body = ['', - main, - '', - '', - ''].join('\n'); +var clj_genpages = function (profile) { + if (typeof demo === 'undefined') { + vm.runInThisContext(src, srcFile); + } + return demo.genpages(profile); +} -var html = ['', '', head, body, ''].join('\n'); +var generate = function () { + var pages = clj_genpages(); + Object.keys(pages).map(function (page) { + fs.writeFileSync(page, pages[page]); + }); + fs.writeFileSync("assets/demo.js", src); + fs.writeFileSync("assets/demo.css", + cssFiles.map(function (x) { + return fs.readFileSync(x); + }).join("\n")); + console.log('Wrote site'); +} console.log('Writing site'); -fs.writeFileSync("index.html", html); -fs.writeFileSync("site/demo.js", src); -fs.writeFileSync("site/democss.css", - cssFiles.map(function (x) { - return fs.readFileSync(x); - }).join("\n")); -console.log('Wrote site'); +generate(); diff --git a/demo/demo.cljs b/demo/demo.cljs index 7cdf7f0..f336b26 100644 --- a/demo/demo.cljs +++ b/demo/demo.cljs @@ -8,9 +8,16 @@ [reagent.debug :refer-macros [dbg println]])) (def page rpage/page) +(def title-atom (atom "Reagent: Minimalistic React for ClojureScript")) + +(defn prefix [href] + (let [depth (-> #"/" (re-seq @page) count) + pref (->> "../" (repeat depth) (apply str))] + (str pref href))) (defn link [props children] (apply vector :a (assoc props + :href (-> props :href prefix) :on-click (if rpage/history (fn [e] (.preventDefault e) @@ -29,16 +36,46 @@ [:div [:div [:ul - [:li [link {:href "news.html"} "News"]] + [:li [link {:href "news/index.html"} "News"]] [:li [link {:href "index.html"} "Intro"]]]] (case (dbg @page) "index.html" [intro/main] - "news.html" [news/main] + "news/index.html" [news/main] + "news/cloact-reagent-undo-demo.html" [news/main] + "news/" [news/main] [intro/main]) [github-badge]]) -(defn ^:export mountdemo [] +(defn ^:export mountdemo [p] + (when p (reset! page p)) (reagent/render-component [demo] (.-body js/document))) -(defn ^:export genpage [] - (reagent/render-component-to-string [demo])) +(defn gen-page [p timestamp] + (reset! page p) + (let [body (reagent/render-component-to-string [demo]) + title @title-atom] + (str " + + + + " title " + + + + + " body " + + + +"))) + +(defn ^:export genpages [] + (let [timestamp (str "?" (.now js/Date))] + (->> ["index.html" "news/index.html" + "news/cloact-reagent-undo-demo.html"] + (map #(vector % (gen-page % timestamp))) + (into {}) + clj->js)))