Move page generation to ClojureScript

This commit is contained in:
Dan Holmsand 2014-01-19 12:13:19 +01:00
parent 8fc6bf3e6e
commit 446e05adec
4 changed files with 67 additions and 37 deletions

4
.gitignore vendored
View File

@ -1,6 +1,6 @@
index.html
site/demo.js
site/democss.css
assets/
news/
target
pom.xml
.lein-repl-history

View File

@ -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

View File

@ -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 = ['<head>',
'<meta charset="utf-8">',
'<title>Cloact: Minimalistic React for ClojureScript</title>',
'<meta name="viewport" content="width=device-width, initial-scale=1.0" />',
'<link rel="stylesheet" href="site/democss.css' + ts + '">',
'</head>'].join('\n');
var srcFile = "target/cljs-client.js";
var src = fs.readFileSync(srcFile);
var body = ['<body>',
main,
'<script type="text/javascript" src="site/demo.js' + ts + '"></script>',
'<script type="text/javascript">',
'setTimeout(demo.mountdemo, 200);',
'</script>',
'</body>'].join('\n');
var clj_genpages = function (profile) {
if (typeof demo === 'undefined') {
vm.runInThisContext(src, srcFile);
}
return demo.genpages(profile);
}
var html = ['<!doctype html>', '<html>', head, body, '</html>'].join('\n');
console.log('Writing site');
fs.writeFileSync("index.html", html);
fs.writeFileSync("site/demo.js", src);
fs.writeFileSync("site/democss.css",
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');
generate();

View File

@ -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 "<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<title>" title "</title>
<meta name='viewport' content='width=device-width, initial-scale=1.0' />
<link rel='stylesheet' href='" (prefix "assets/demo.css") timestamp "'>
</head>
<body>
" body "
<script type='text/javascript'
src='" (prefix "assets/demo.js") timestamp "'></script>
<script type='text/javascript'>
setTimeout(function() {demo.mountdemo('" p "')}, 200);
</script>
</body>
</html>")))
(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)))