A little cleanup

This commit is contained in:
Dan Holmsand 2015-09-16 17:55:45 +02:00
parent 38250c763b
commit 2558cd6708
1 changed files with 15 additions and 22 deletions

View File

@ -1,15 +1,13 @@
(ns sitetools.core (ns sitetools.core
(:require [clojure.string :as string] (:require [clojure.string :as string]
[goog.events :as evt] [goog.events :as evt]
[goog.history.EventType :as hevt]
[reagent.core :as r] [reagent.core :as r]
[reagent.debug :refer-macros [dbg log dev?]] [reagent.debug :refer-macros [dbg log dev?]]
[reagent.interop :as i :refer-macros [.' .!]]) [reagent.interop :as i :refer-macros [.' .!]])
(:import goog.History (:import goog.History
goog.history.Html5History)) [goog.history Html5History EventType]))
(when (exists? js/console) (enable-console-print!)
(enable-console-print!))
(defn rswap! [a f & args] (defn rswap! [a f & args]
;; Like swap!, except that recursive swaps on the same atom are ok, ;; Like swap!, except that recursive swaps on the same atom are ok,
@ -50,14 +48,12 @@
(case id (case id
:set-content (let [page x :set-content (let [page x
title (:title page) title (:title page)
_ (assert (vector? (:content page)))
title (if title title (if title
(str (:title-prefix state) title) (str (:title-prefix state) title)
(str (:default-title state))) (str (:default-title state)))]
page (assoc page :title title)]
(when r/is-client (when r/is-client
(set! js/document.title title)) (set! js/document.title title))
(assoc state :current-page page)) (assoc state :current-page page :title title))
:set-page (let [path x :set-page (let [path x
_ (assert (string? path)) _ (assert (string? path))
ps (:pages state) ps (:pages state)
@ -98,12 +94,13 @@
(re-pattern (str page "$")) "") (re-pattern (str page "$")) "")
(string/replace #"/*$" "")))) (string/replace #"/*$" ""))))
(History.))) (History.)))
(evt/listen hevt/NAVIGATE #(when (.-isNavigation %) (evt/listen EventType.NAVIGATE #(when (.-isNavigation %)
(dispatch [:set-page (.-token %)]))) (dispatch [:set-page (.-token %)])))
(.setEnabled true)) (.setEnabled true))
(let [p (if (and page (not html5) (-> history .getToken empty?)) (let [token (.getToken history)
p (if (and page (not html5) (empty? token))
page page
(.getToken history))] token)]
(dispatch [:set-page p]))))) (dispatch [:set-page p])))))
(defn to-relative [f] (defn to-relative [f]
@ -120,16 +117,14 @@
child]) child])
(defn main-content [] (defn main-content []
(let [comp (get-in @config [:current-page :content])] (get-in @config [:current-page :content]))
(assert (vector? comp))
comp))
;;; Static site generation ;;; Static site generation
(defn prefix [href page] (defn base [page]
(let [depth (-> #"/" (re-seq (to-relative page)) count)] (let [depth (->> page to-relative (re-seq #"/") count)]
(str (->> "../" (repeat depth) (apply str)) href))) (->> "../" (repeat depth) (apply str))))
(defn danger [t s] (defn danger [t s]
[t {:dangerouslySetInnerHTML {:__html s}}]) [t {:dangerouslySetInnerHTML {:__html s}}])
@ -143,25 +138,23 @@
[:meta {:charset 'utf-8}] [:meta {:charset 'utf-8}]
[:meta {:name 'viewport [:meta {:name 'viewport
:content "width=device-width, initial-scale=1.0"}] :content "width=device-width, initial-scale=1.0"}]
[:base {:href (prefix "" (:page-path page-conf))}] [:base {:href (-> page-conf :page-path base)}]
[: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-html)] [: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-path conf] (defn gen-page [page-path conf]
(dispatch [:set-page page-path]) (dispatch [:set-page page-path])
(let [conf (merge conf @config) (let [conf (merge conf @config)
b (:body conf) b (:body conf)
_ (assert (vector? b))
bhtml (r/render-component-to-string b)] bhtml (r/render-component-to-string b)]
(str "<!doctype html>\n" (str "<!doctype html>\n"
(html-template (assoc conf (html-template (assoc conf
:page-conf {:page-path page-path} :page-conf {:page-path page-path}
:title (-> conf :current-page :title)
:body-html bhtml))))) :body-html bhtml)))))
(defn fs [] (js/require "fs")) (defn fs [] (js/require "fs"))