From bc89612a76078a24f66171713d38728684ca4ec4 Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Sat, 29 Nov 2014 19:51:45 +0100 Subject: [PATCH] Fix site-generation and simplify title setting --- Makefile | 5 ++-- demo/demo.cljs | 13 +++++++--- demo/reagentdemo/intro.cljs | 3 +-- demo/reagentdemo/news.cljs | 3 +-- demo/reagentdemo/news/anyargs.cljs | 12 ++++----- demo/reagentdemo/news/async.cljs | 9 ++++--- demo/reagentdemo/news/clockpost.cljs | 12 ++++----- demo/reagentdemo/news/undodemo.cljs | 8 +++--- demo/sitetools.cljs | 37 ++++++++++++++++------------ project.clj | 2 +- site/public/css/examples.css | 3 +++ 11 files changed, 60 insertions(+), 47 deletions(-) diff --git a/Makefile b/Makefile index 78f3464..6c53667 100644 --- a/Makefile +++ b/Makefile @@ -46,8 +46,9 @@ serve-site: trigger-build trigger-build: # always trigger build to make sure page-generation works @echo "(ns empty.generated.ns)" > demo/empty.cljs - cat examples/todomvc/todos.css examples/simple/example.css \ - > site/public/css/examples.css + @(echo "/* Generated, do not modify */\n\n" && \ + cat examples/todomvc/todos.css examples/simple/example.css) \ + > site/public/css/examples.css diff --git a/demo/demo.cljs b/demo/demo.cljs index 067540a..3fd193a 100644 --- a/demo/demo.cljs +++ b/demo/demo.cljs @@ -24,11 +24,14 @@ (def index-page "index.html") (def news-page "news/index.html") -(tools/register-page index-page (fn [] [intro/main])) -(tools/register-page news-page (fn [] [news/main])) +(tools/register-page index-page + (fn [] [intro/main]) + "Reagent: Minimalistic React for ClojureScript") +(tools/register-page news-page + (fn [] [news/main]) + "Reagent news") (defn demo [] - (dbg "demo") [:div [:div.nav [:ul.nav @@ -43,4 +46,6 @@ (defn start! [{:keys [test-results]}] (reset! test-results-comp test-results) - (tools/start! {:body (fn [] [demo])})) + (tools/start! {:body (fn [] [demo]) + :css-infiles ["site/public/css/main.css" + "site/public/css/examples.css"]})) diff --git a/demo/reagentdemo/intro.cljs b/demo/reagentdemo/intro.cljs index 2192633..e3ef872 100644 --- a/demo/reagentdemo/intro.cljs +++ b/demo/reagentdemo/intro.cljs @@ -5,7 +5,7 @@ [reagent.debug :refer-macros [dbg println]] [clojure.string :as string] [reagentdemo.syntax :refer-macros [get-source]] - [sitetools :refer [link title]] + [sitetools :refer [link]] [reagentdemo.common :as common :refer [demo-component]] [simpleexample :as simple] [todomvc :as todo])) @@ -323,7 +323,6 @@ (js/setTimeout #(reset! show-all true) 500) (fn [] [:div.reagent-demo - [title head] [:h1 head] [intro] [managing-state] diff --git a/demo/reagentdemo/news.cljs b/demo/reagentdemo/news.cljs index f1d7bbf..58c08ef 100644 --- a/demo/reagentdemo/news.cljs +++ b/demo/reagentdemo/news.cljs @@ -3,7 +3,7 @@ [reagent.interop :refer-macros [.' .!]] [reagent.debug :refer-macros [dbg println]] [reagentdemo.common :as common :refer [demo-component]] - [sitetools :as tools :refer [title link]] + [sitetools :as tools :refer [link]] [reagentdemo.news.anyargs :as anyargs] [reagentdemo.news.async :as async] [reagentdemo.news.undodemo :as undodemo] @@ -11,7 +11,6 @@ (defn main [] [:div - [title "Reagent news"] [clock/main {:summary true}] [anyargs/main {:summary true}] [async/main {:summary true}] diff --git a/demo/reagentdemo/news/anyargs.cljs b/demo/reagentdemo/news/anyargs.cljs index 68a15fd..c460458 100644 --- a/demo/reagentdemo/news/anyargs.cljs +++ b/demo/reagentdemo/news/anyargs.cljs @@ -3,11 +3,12 @@ [reagent.interop :refer-macros [.' .!]] [reagent.debug :refer-macros [dbg println]] [reagentdemo.syntax :refer-macros [get-source]] - [sitetools :as tools :refer [title link]] + [sitetools :as tools :refer [link]] [reagentdemo.common :as common :refer [demo-component]] [geometry.core :as geometry])) (def url "news/any-arguments.html") +(def title "All arguments allowed") (def funmap (-> ::this get-source common/fun-map)) (def src-for (partial common/src-for funmap)) @@ -33,12 +34,10 @@ [:p "Some other text in bold."]]]) (defn main [{:keys [summary]}] - (let [head "All arguments allowed" - geometry {:href "https://github.com/reagent-project/reagent/tree/master/examples/geometry"} + (let [geometry {:href "https://github.com/reagent-project/reagent/tree/master/examples/geometry"} jonase {:href "https://github.com/jonase"}] [:div.reagent-demo - [:h1 [link {:href url} head]] - [title (str "Reagent 0.4.0: " head)] + [:h1 [link {:href url} title]] [:div.demo-text [:h2 "If it looks like a function…"] @@ -132,4 +131,5 @@ [demo-component {:comp geometry-example}]])]])) -(tools/register-page url (fn [] [main])) +(tools/register-page url (fn [] [main]) + (str "Reagent: " title)) diff --git a/demo/reagentdemo/news/async.cljs b/demo/reagentdemo/news/async.cljs index e975a76..d3ec291 100644 --- a/demo/reagentdemo/news/async.cljs +++ b/demo/reagentdemo/news/async.cljs @@ -3,10 +3,11 @@ [reagent.interop :refer-macros [.' .!]] [reagent.debug :refer-macros [dbg println]] [reagentdemo.syntax :refer-macros [get-source]] - [sitetools :as tools :refer [title link]] + [sitetools :as tools :refer [link]] [reagentdemo.common :as common :refer [demo-component]])) (def url "news/reagent-is-async.html") +(def title "Faster by waiting") (def funmap (-> "reagentdemo/news/async.cljs" get-source common/fun-map)) (def src-for (partial common/src-for funmap)) @@ -93,8 +94,7 @@ (defn main [{:keys [summary]}] (let [om-article {:href "http://swannodette.github.io/2013/12/17/the-future-of-javascript-mvcs/"}] [:div.reagent-demo - [title "Reagent: Faster by waiting"] - [:h1 [link {:href url} "Faster by waiting"]] + [:h1 [link {:href url} title]] [:div.demo-text [:h2 "Reagent gets async rendering"] @@ -196,4 +196,5 @@ :reset-random-colors :color-choose :ncolors-choose :palette :color-demo])}]])]])) -(tools/register-page url (fn [] [main])) +(tools/register-page url (fn [] [main]) + (str "Reagent: " title)) diff --git a/demo/reagentdemo/news/clockpost.cljs b/demo/reagentdemo/news/clockpost.cljs index 8acee86..1becc70 100644 --- a/demo/reagentdemo/news/clockpost.cljs +++ b/demo/reagentdemo/news/clockpost.cljs @@ -3,11 +3,12 @@ [reagent.interop :refer-macros [.' .!]] [reagent.debug :refer-macros [dbg]] [reagentdemo.syntax :refer-macros [get-source]] - [sitetools :as tools :refer [title link]] + [sitetools :as tools :refer [link]] [reagentdemo.common :as common :refer [demo-component]] [reagentdemo.news.binaryclock :as binaryclock])) (def url "news/binary-clock.html") +(def title "A binary clock") (def funmap (-> "reagentdemo/news/binaryclock.cljs" get-source common/fun-map)) @@ -18,16 +19,14 @@ :no-heading true}]) (defn main [{:keys [summary]}] - (let [head "A binary clock" - lexclock {:href "http://www.lexicallyscoped.com/2014/01/23/clojurescript-react-om-binary-clock.html"} + (let [lexclock {:href "http://www.lexicallyscoped.com/2014/01/23/clojurescript-react-om-binary-clock.html"} hopclock {:href "http://pmbauer.github.io/2014/01/27/hoplon-binary-clock/"} om {:href "https://github.com/swannodette/om"} hoplon {:href "http://hoplon.io"} clocksrc {:href "https://github.com/reagent-project/reagent/blob/master/demo/reagentdemo/news/binaryclock.cljs"}] [:div.reagent-demo - [:h1 [link {:href url} head]] - [title head] + [:h1 [link {:href url} title]] [:div.demo-text (when-not summary @@ -121,4 +120,5 @@ description that corresponds to those arguments, and leave it to React to actually display that UI."]])]])) -(tools/register-page url (fn [] [main])) +(tools/register-page url (fn [] [main]) + (str "Reagent: " title)) diff --git a/demo/reagentdemo/news/undodemo.cljs b/demo/reagentdemo/news/undodemo.cljs index a1c3ad0..31aabc9 100644 --- a/demo/reagentdemo/news/undodemo.cljs +++ b/demo/reagentdemo/news/undodemo.cljs @@ -3,11 +3,12 @@ [reagent.interop :refer-macros [.' .!]] [reagent.debug :refer-macros [dbg println]] [reagentdemo.syntax :refer-macros [get-source]] - [sitetools :as tools :refer [title link]] + [sitetools :as tools :refer [link]] [reagentdemo.common :as common :refer [demo-component]] [todomvc :as todomvc])) (def url "news/cloact-reagent-undo-demo.html") +(def title "Cloact becomes Reagent: Undo is trivial") (def funmap (-> ::this get-source common/fun-map)) (def src-for (partial common/src-for funmap)) @@ -48,10 +49,9 @@ (remove-watch state ::undo-watcher))})) (defn main [{:keys [summary]}] - (let [head "Cloact becomes Reagent: Undo is trivial"] + (let [head title] [:div.reagent-demo [:h1 [link {:href url} head]] - [title head] [:div.demo-text [:h2 "(reset! cloact-name \"Reagent\")"] @@ -88,4 +88,4 @@ [undo-demo-cleanup]])]])) -(tools/register-page url (fn [] [main])) +(tools/register-page url (fn [] [main]) title) diff --git a/demo/sitetools.cljs b/demo/sitetools.cljs index 61c4a64..7550d4f 100644 --- a/demo/sitetools.cljs +++ b/demo/sitetools.cljs @@ -21,6 +21,7 @@ (defonce config (atom {:page-map {"index.html" (fn [] [:div "Empty"])} + :page-titles {} :body (fn [] [:div (page-content)]) :site-dir "outsite/public" :css-infiles ["site/public/css/main.css"] @@ -31,14 +32,19 @@ :allow-html5-history false})) (defonce page (atom "index.html")) -(defonce page-title (atom (:default-title @config))) (defonce page-state (atom {:has-history false})) -(defn register-page [pageurl comp] - (assert (string? pageurl) (str "expected string, not " pageurl)) - (assert (fn? comp)) - (swap! config update-in [:page-map] assoc pageurl comp)) - +(defn register-page + ([pageurl comp] + (register-page pageurl comp nil)) + ([pageurl comp title] + (assert (string? pageurl) + (str "expected string, not " pageurl)) + (assert (fn? comp)) + (assert (or (nil? title) + (string? title))) + (swap! config update-in [:page-map] assoc pageurl comp) + (swap! config update-in [:page-titles] assoc pageurl title))) ;;; Components @@ -61,14 +67,6 @@ identity)) child])) -(defn title [name] - (when (= @page-title "") - ;; First title on a page wins - (reset! page-title name) - (when reagent/is-client - (set! (.-title js/document) @page-title))) - nil) - (defn page-content [] [(get-in @config [:page-map @page] (get-in @config [:page-map "index.html"]))]) @@ -78,13 +76,19 @@ ;;; Implementation: +(defn get-title [] + (get-in @config [:page-titles @page] + (:default-title @config))) + (defn default-content [] [:div "Empty"]) (add-watch page ::title-watch (fn [_ _ _ p] + (when reagent/is-client + (set! (.-title js/document) (get-title))) ;; First title on a page wins - (reset! page-title ""))) + #_(reset! page-title ""))) ;;; History @@ -185,7 +189,7 @@ (reset! page page-name) (let [b (reagent/render-component-to-string (body))] (str "" - (html-template {:title @page-title + (html-template {:title (get-title) :body b :page-conf {:allow-html5-history true :page-name page-name} @@ -255,5 +259,6 @@ (set-start-page page-name)) (swap! config merge conf) (setup-history) + (set! (.-title js/document) (get-title)) (reagent/render-component (body) (.' js/document :body))))) diff --git a/project.clj b/project.clj index cfbc04a..11f0e8b 100644 --- a/project.clj +++ b/project.clj @@ -23,7 +23,7 @@ :output-dir "outsite/public/js/out"}}}}} - :site {:resource-paths ^:replace ["outsite"] + :site {:resource-paths ^:replace ["outsite" "vendor"] :figwheel {:css-dirs ^:replace ["outsite/public/css"]} :cljsbuild {:builds {:client diff --git a/site/public/css/examples.css b/site/public/css/examples.css index 5a7b12d..28840bd 100644 --- a/site/public/css/examples.css +++ b/site/public/css/examples.css @@ -1,3 +1,6 @@ +/* Generated, do not modify */ + + @charset "utf-8"; html, body {