2015-02-10 13:18:56 +00:00
|
|
|
(ns sitetools.core
|
2014-11-29 17:30:24 +00:00
|
|
|
(:require [clojure.string :as string]
|
|
|
|
[goog.events :as evt]
|
|
|
|
[goog.history.EventType :as hevt]
|
2015-07-31 13:13:27 +00:00
|
|
|
[reagent.core :as r]
|
2015-09-07 20:22:26 +00:00
|
|
|
[secretary.core :as secretary :refer-macros [defroute]]
|
2014-11-29 17:30:24 +00:00
|
|
|
[reagent.debug :refer-macros [dbg log dev?]]
|
|
|
|
[reagent.interop :as i :refer-macros [.' .!]])
|
2015-09-07 22:41:04 +00:00
|
|
|
(:import goog.History
|
|
|
|
goog.history.Html5History))
|
2014-11-29 17:30:24 +00:00
|
|
|
|
|
|
|
(when (exists? js/console)
|
|
|
|
(enable-console-print!))
|
|
|
|
|
2015-09-07 20:22:26 +00:00
|
|
|
(defn rswap! [a f & args]
|
2015-09-07 22:12:41 +00:00
|
|
|
;; Roughly like swap!, except that recursive swaps are ok
|
2015-09-08 06:38:45 +00:00
|
|
|
(let [fs (or (.-rswapfs a)
|
|
|
|
(set! (.-rswapfs a) (array)))]
|
2015-09-07 20:22:26 +00:00
|
|
|
(.push fs #(apply f % args))
|
|
|
|
(if (< 1 (.-length fs))
|
2015-09-07 22:12:41 +00:00
|
|
|
nil
|
|
|
|
(let [f' (fn [state]
|
|
|
|
(let [s ((aget fs 0) state)]
|
|
|
|
(.shift fs)
|
|
|
|
(if (-> fs .-length pos?)
|
|
|
|
(recur s)
|
|
|
|
s)))]
|
|
|
|
(swap! a f')))))
|
2015-09-07 20:22:26 +00:00
|
|
|
|
2014-11-29 17:30:24 +00:00
|
|
|
|
|
|
|
;;; Configuration
|
|
|
|
|
2015-09-07 22:29:06 +00:00
|
|
|
(declare page-content)
|
|
|
|
|
2015-09-08 06:52:10 +00:00
|
|
|
(defonce config (r/atom {;;:page-map {"index.html" [:div "Empty"]}
|
|
|
|
;;:page-titles {}
|
2015-07-31 13:13:27 +00:00
|
|
|
:body [page-content]
|
2015-09-07 20:22:26 +00:00
|
|
|
:main-content [:div]
|
|
|
|
:pages #{}
|
2015-07-31 13:13:27 +00:00
|
|
|
:site-dir "outsite/public"
|
|
|
|
:css-infiles ["site/public/css/main.css"]
|
|
|
|
:css-file "css/built.css"
|
|
|
|
:js-file "js/main.js"
|
|
|
|
:js-dir "js/out"
|
|
|
|
:main-div "main-content"
|
2015-09-07 22:29:06 +00:00
|
|
|
:default-title ""
|
2015-07-31 13:13:27 +00:00
|
|
|
:allow-html5-history false}))
|
2014-11-29 17:30:24 +00:00
|
|
|
|
2015-09-08 06:52:10 +00:00
|
|
|
;; (defonce page (r/atom "index.html"))
|
|
|
|
;; (defonce page-state (r/atom {:has-history false}))
|
2014-11-29 17:30:24 +00:00
|
|
|
|
2015-09-07 20:22:26 +00:00
|
|
|
(defonce history nil)
|
|
|
|
|
|
|
|
(defn demo-handler [state [id v1 v2 :as event]]
|
|
|
|
(case id
|
|
|
|
:content (do
|
2015-09-07 22:29:06 +00:00
|
|
|
(let [title (if v2
|
|
|
|
(str (:title-prefix @config) v2)
|
|
|
|
(str (:default-title @config)))]
|
2015-09-07 22:12:41 +00:00
|
|
|
(when r/is-client
|
|
|
|
(r/next-tick #(set! js/document.title title)))
|
|
|
|
(assoc state
|
|
|
|
:main-content v1
|
|
|
|
:title title)))
|
|
|
|
:set-page (do (secretary/dispatch! v1)
|
|
|
|
(assoc state :page v1))
|
2015-09-07 20:22:26 +00:00
|
|
|
:goto-page (do
|
2015-09-07 22:12:41 +00:00
|
|
|
(when r/is-client
|
|
|
|
(.setToken history v1 false)
|
|
|
|
(r/next-tick #(set! js/document.body.scrollTop 0)))
|
|
|
|
(recur state [:set-page v1 v2]))
|
2015-09-07 20:22:26 +00:00
|
|
|
state))
|
|
|
|
|
|
|
|
(defn dispatch [event]
|
2015-09-08 06:38:45 +00:00
|
|
|
;; (dbg event)
|
2015-09-07 20:22:26 +00:00
|
|
|
(rswap! config demo-handler event)
|
|
|
|
nil)
|
|
|
|
|
|
|
|
(defn reg-page [url]
|
|
|
|
(swap! config update-in [:pages] conj url))
|
|
|
|
|
|
|
|
(defn init-history []
|
|
|
|
(when-not history
|
|
|
|
(doto (set! history (History.))
|
2015-09-07 22:12:41 +00:00
|
|
|
(evt/listen hevt/NAVIGATE #(dispatch [:set-page (.-token %)]))
|
2015-09-07 20:22:26 +00:00
|
|
|
(.setEnabled true))))
|
|
|
|
|
|
|
|
|
2015-09-07 22:29:06 +00:00
|
|
|
;; (defn register-page
|
|
|
|
;; ([pageurl comp]
|
|
|
|
;; (register-page pageurl comp nil))
|
|
|
|
;; ([pageurl comp title]
|
|
|
|
;; (assert (string? pageurl)
|
|
|
|
;; (str "expected string, not " pageurl))
|
|
|
|
;; (assert (vector? comp)
|
|
|
|
;; (str "expected vector, not " (pr-str 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)
|
|
|
|
;; pageurl))
|
2014-11-29 17:30:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
;;; Components
|
|
|
|
|
2015-09-07 20:22:26 +00:00
|
|
|
(defn link [props child]
|
|
|
|
[:a (assoc props
|
|
|
|
:on-click #(do (.preventDefault %)
|
|
|
|
(dispatch [:goto-page (:href props)])))
|
|
|
|
child])
|
|
|
|
|
2015-09-07 22:29:06 +00:00
|
|
|
;; (defn link
|
|
|
|
;; [props child]
|
|
|
|
;; (let [p (:href props)
|
|
|
|
;; f ((:page-map @config) p)]
|
|
|
|
;; (assert (vector? f) (str "couldn't resolve page " p))
|
|
|
|
;; (assert (string? p))
|
|
|
|
;; [:a (assoc props
|
|
|
|
;; :href p
|
|
|
|
;; :on-click (if (:has-history @page-state)
|
|
|
|
;; (fn [e]
|
|
|
|
;; (.preventDefault e)
|
|
|
|
;; (reset! page p)
|
|
|
|
;; (r/next-tick
|
|
|
|
;; #(set! (.-scrollTop (.-body js/document))
|
|
|
|
;; 0)))
|
|
|
|
;; identity))
|
|
|
|
;; child]))
|
2014-11-29 17:30:24 +00:00
|
|
|
|
|
|
|
(defn page-content []
|
2015-09-07 21:01:33 +00:00
|
|
|
(:main-content @config))
|
2014-11-29 17:30:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Implementation:
|
|
|
|
|
2015-09-07 22:29:06 +00:00
|
|
|
;; (defn get-title []
|
|
|
|
;; (get-in @config [:page-titles @page]
|
|
|
|
;; (or (get-in @config [:page-titles "index.html"])
|
|
|
|
;; "")))
|
2014-11-29 18:51:45 +00:00
|
|
|
|
2015-09-07 22:29:06 +00:00
|
|
|
;; (defn default-content []
|
|
|
|
;; [:div "Empty"])
|
2014-11-29 17:30:24 +00:00
|
|
|
|
2015-09-07 22:29:06 +00:00
|
|
|
;; (add-watch page ::title-watch
|
|
|
|
;; (fn [_ _ _ p]
|
|
|
|
;; (when r/is-client
|
|
|
|
;; (set! (.-title js/document) (get-title)))
|
|
|
|
;; ;; First title on a page wins
|
|
|
|
;; #_(reset! page-title "")))
|
2014-11-29 17:30:24 +00:00
|
|
|
|
|
|
|
;;; History
|
|
|
|
|
2015-09-07 22:29:06 +00:00
|
|
|
;; (defn use-html5-history []
|
|
|
|
;; (when r/is-client
|
|
|
|
;; (let [proto (.' js/window :location.protocol)]
|
|
|
|
;; (and (:allow-html5-history @config)
|
|
|
|
;; (.isSupported Html5History)
|
|
|
|
;; (#{"http:" "https:"} proto)))))
|
|
|
|
|
|
|
|
;; (defn create-history [p]
|
|
|
|
;; (if (use-html5-history)
|
|
|
|
;; (doto (Html5History.)
|
|
|
|
;; (.setUseFragment false))
|
|
|
|
;; (let [h (History.)]
|
|
|
|
;; (when p
|
|
|
|
;; (.setToken h p))
|
|
|
|
;; h)))
|
|
|
|
|
|
|
|
;; (defn token-base []
|
|
|
|
;; (if (use-html5-history)
|
|
|
|
;; (:base-path @config)))
|
|
|
|
|
|
|
|
;; (defn setup-history [p]
|
|
|
|
;; (when (nil? history)
|
|
|
|
;; (set! history (create-history p))
|
|
|
|
;; (swap! page-state assoc :has-history (some? history))
|
|
|
|
;; (when-let [h history]
|
|
|
|
;; (evt/listen h hevt/NAVIGATE
|
|
|
|
;; (fn [e]
|
|
|
|
;; (let [t (.-token e)
|
|
|
|
;; tb (token-base)]
|
|
|
|
;; (reset! page (if (and tb (== 0 (.indexOf t tb)))
|
|
|
|
;; (subs t (count tb))
|
|
|
|
;; t)))
|
|
|
|
;; (r/flush)))
|
|
|
|
;; (add-watch page ::history
|
|
|
|
;; (fn [_ _ oldp newp]
|
|
|
|
;; (when-not (= oldp newp)
|
|
|
|
;; (.setToken h (str (token-base) newp)))))
|
|
|
|
;; (.setEnabled h true))))
|
|
|
|
|
|
|
|
;; (defn base-path [loc p]
|
|
|
|
;; ;; Find base-path for html5 history
|
|
|
|
;; (let [split #".[^/]*"
|
|
|
|
;; depth (->> (case p "" "." p) (re-seq split) count)
|
|
|
|
;; base (->> loc (re-seq split) (drop-last depth) (apply str))]
|
|
|
|
;; (string/replace (str base "/") #"^/" "")))
|
|
|
|
|
|
|
|
;; (defn set-start-page [p]
|
|
|
|
;; (when (and (not (:base-path @config))
|
|
|
|
;; (use-html5-history))
|
|
|
|
;; (swap! config assoc :base-path
|
|
|
|
;; (base-path (.' js/window -location.pathname) p)))
|
|
|
|
;; (reset! page p))
|
2014-11-29 17:30:24 +00:00
|
|
|
|
2015-09-08 06:52:10 +00:00
|
|
|
(defn prefix [href page]
|
|
|
|
(let [depth (-> #"/" (re-seq page) count)]
|
2014-11-29 17:30:24 +00:00
|
|
|
(str (->> "../" (repeat depth) (apply str)) href)))
|
|
|
|
|
|
|
|
|
|
|
|
;;; Static site generation
|
|
|
|
|
|
|
|
(defn body []
|
|
|
|
(let [b (:body @config)]
|
2015-01-31 21:02:30 +00:00
|
|
|
(assert (vector? b) (str "body is not a vector: " b))
|
|
|
|
b))
|
2014-11-29 17:30:24 +00:00
|
|
|
|
|
|
|
(defn danger [t s]
|
|
|
|
[t {:dangerouslySetInnerHTML {:__html s}}])
|
|
|
|
|
|
|
|
(defn html-template [{:keys [title body timestamp page-conf
|
|
|
|
opt-none req]}]
|
|
|
|
(let [c @config
|
2015-01-29 13:49:01 +00:00
|
|
|
main (str (:js-file c) timestamp)
|
|
|
|
css-file (:css-file c)
|
2014-11-29 17:30:24 +00:00
|
|
|
opt-none (:opt-none c)]
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/render-to-static-markup
|
2014-11-29 17:30:24 +00:00
|
|
|
[:html
|
|
|
|
[:head
|
|
|
|
[:meta {:charset "utf-8"}]
|
|
|
|
[:meta {:name 'viewport
|
|
|
|
:content "width=device-width, initial-scale=1.0"}]
|
2015-09-08 06:52:10 +00:00
|
|
|
[:base {:href (prefix "" (:page-name page-conf))}]
|
2014-11-29 17:30:24 +00:00
|
|
|
[:link {:href (str css-file timestamp) :rel 'stylesheet}]
|
|
|
|
[:title title]]
|
|
|
|
[:body
|
2014-12-09 06:30:57 +00:00
|
|
|
[:div {:id (:main-div @config)}
|
|
|
|
(danger :div body)]
|
2014-11-29 17:30:24 +00:00
|
|
|
(danger :script (str "var pageConfig = " (-> page-conf
|
|
|
|
clj->js
|
|
|
|
js/JSON.stringify)))
|
2015-01-29 13:49:01 +00:00
|
|
|
[:script {:src main :type "text/javascript"}]]])))
|
2014-11-29 17:30:24 +00:00
|
|
|
|
|
|
|
(defn gen-page [page-name timestamp]
|
2015-09-08 06:52:10 +00:00
|
|
|
;; (reset! page page-name)
|
|
|
|
(dispatch [:set-page page-name])
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [b (r/render-component-to-string (body))]
|
2014-11-29 17:30:24 +00:00
|
|
|
(str "<!doctype html>"
|
2015-09-07 22:29:06 +00:00
|
|
|
(html-template {:title (:title @config) ;;(get-title)
|
2014-11-29 17:30:24 +00:00
|
|
|
:body b
|
|
|
|
:page-conf {:allow-html5-history true
|
|
|
|
:page-name page-name}
|
|
|
|
:timestamp timestamp}))))
|
|
|
|
|
|
|
|
(defn mkdirs [f]
|
|
|
|
(let [fs (js/require "fs")
|
|
|
|
path (js/require "path")
|
|
|
|
items (as-> f _
|
2015-09-07 20:22:26 +00:00
|
|
|
(.' path dirname _)
|
|
|
|
(.' path normalize _)
|
|
|
|
(string/split _ #"/"))
|
2014-11-29 17:30:24 +00:00
|
|
|
parts (reductions #(str %1 "/" %2) items)]
|
|
|
|
(doseq [d parts]
|
|
|
|
(when-not (.' fs existsSync d)
|
|
|
|
(.' fs mkdirSync d)))))
|
|
|
|
|
|
|
|
(defn write-file [f content]
|
|
|
|
(let [fs (js/require "fs")]
|
|
|
|
(mkdirs f)
|
|
|
|
(.' fs writeFileSync f content)))
|
|
|
|
|
|
|
|
(defn read-file [f]
|
|
|
|
(let [fs (js/require "fs")]
|
|
|
|
(.' fs readFileSync f)))
|
|
|
|
|
|
|
|
(defn path-join [& paths]
|
|
|
|
(let [path (js/require "path")]
|
|
|
|
(apply (.' path :join) paths)))
|
|
|
|
|
|
|
|
(defn read-css []
|
2014-12-11 08:01:25 +00:00
|
|
|
(clojure.string/join "\n"
|
2014-11-29 17:30:24 +00:00
|
|
|
(map read-file (:css-infiles @config))))
|
|
|
|
|
|
|
|
(defn write-resources [dir]
|
|
|
|
(write-file (path-join dir (:css-file @config))
|
|
|
|
(read-css)))
|
|
|
|
|
2015-09-08 06:52:10 +00:00
|
|
|
(defn as-relative [f]
|
|
|
|
(clojure.string/replace f #"^/" ""))
|
2014-11-29 17:30:24 +00:00
|
|
|
|
|
|
|
;;; Main entry points
|
|
|
|
|
|
|
|
(defn ^:export genpages [opts]
|
|
|
|
(log "Generating site")
|
|
|
|
(swap! config merge (js->clj opts :keywordize-keys true))
|
|
|
|
(let [dir (:site-dir @config)
|
2015-08-20 07:54:15 +00:00
|
|
|
timestamp (str "?" (.' js/Date now))]
|
2015-09-08 06:52:10 +00:00
|
|
|
(doseq [f (:pages @config)]
|
|
|
|
(write-file (path-join dir (dbg (as-relative f)))
|
2015-08-20 07:54:15 +00:00
|
|
|
(gen-page f timestamp)))
|
2014-11-29 17:30:24 +00:00
|
|
|
(write-resources dir))
|
|
|
|
(log "Wrote site"))
|
|
|
|
|
|
|
|
(defn start! [site-config]
|
|
|
|
(swap! config merge site-config)
|
2015-07-31 13:13:27 +00:00
|
|
|
(when r/is-client
|
2014-11-29 17:30:24 +00:00
|
|
|
(let [conf (when (exists? js/pageConfig)
|
|
|
|
(js->clj js/pageConfig :keywordize-keys true))
|
|
|
|
page-name (:page-name conf)]
|
2014-11-30 08:40:12 +00:00
|
|
|
(swap! config merge conf)
|
2014-12-07 15:38:56 +00:00
|
|
|
(when (nil? history)
|
2015-09-07 22:29:06 +00:00
|
|
|
;; (when page-name
|
|
|
|
;; (set-start-page page-name))
|
2015-09-07 20:22:26 +00:00
|
|
|
(init-history)
|
|
|
|
;; (setup-history page-name)
|
2015-09-07 22:29:06 +00:00
|
|
|
;; (set! (.-title js/document) (get-title))
|
|
|
|
)
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/render-component (body)
|
|
|
|
(.' js/document getElementById
|
|
|
|
(:main-div @config))))))
|