theming moved to JS completely

This commit is contained in:
Christian Mueller
2014-01-19 12:22:47 +01:00
parent 99ac828ba3
commit 226d8133da
6 changed files with 162 additions and 148 deletions
-66
View File
@@ -1,66 +0,0 @@
(ns NoteHub.views.common
(:use
[NoteHub.settings :only [get-message]]
[noir.core :only [defpartial]]
[noir.options :only [dev-mode?]]
[hiccup.util :only [escape-html]]
[ring.util.codec :only [url-encode]]
[hiccup.core]
[hiccup.page :only [include-js html5]]
[hiccup.element :only [javascript-tag]]))
(defn url
"Creates a local url from the given substrings"
[& args]
(apply str (interpose "/" (cons "" (map url-encode args)))))
; Creates the main html layout
(defpartial generate-layout
[params title & content]
; for the sake of security: escape all symbols of the param values
(let [params (into {} (for [[k v] params] [k (escape-html v)]))
theme (:theme params "default")
header-font (:header-font params)
text-font (:text-font params)]
(html5
[:head
[:title (print-str (get-message :name) "—" title)]
[:meta {:charset "UTF-8"}]
; generating a link to google's webfonts
[:link {:href
(clojure.string/replace
(str "http://fonts.googleapis.com/css?family="
(apply str
(interpose "|" ["PT+Serif:700" "Noticia+Text:700" header-font text-font]))
"&subset=latin,cyrillic") " " "+")
:rel "stylesheet"
:type "text/css"}]
[:link {:rel "stylesheet/less" :type "text/css" :href "/style.less"}]
(html
(include-js "/js/less.js")
(include-js "/js/md5.js")
(include-js "/js/marked.js")
(include-js "/js/main.js")
(include-js "/js/themes.js")
(javascript-tag (str "less.modifyVars({
'@background': themes['" theme "'].background.normal,
'@background_halftone': themes['" theme "'].background.halftone,
'@foreground': themes['" theme "'].foreground.normal,
'@foreground_halftone': themes['" theme "'].foreground.halftone,
'@link_fresh': themes['" theme "'].link.fresh,
'@link_visited': themes['" theme "'].link.visited,
'@link_hover': themes['" theme "'].link.hover"
(when header-font (str ", '@header_font': '" header-font "'"))
(when text-font (str ",'@text_font': '" text-font "'"))
"});")))
; google analytics code should appear in prod mode only
(if-not (dev-mode?) (include-js "/js/google-analytics.js"))]
[:body {:onload "loadPage()"} content])))
(defn layout
"Generates the main html layout"
[& args]
; if some parameter weren't added we provide an empty map
(if (map? (first args))
(apply generate-layout args)
(apply generate-layout {} args)))
+60 -22
View File
@@ -4,22 +4,55 @@
[NoteHub.storage :as storage]
[cheshire.core :refer :all])
(:use
[NoteHub.settings]
[NoteHub.views.common]
[clojure.string :rename {replace sreplace}
:only [escape split replace blank? split-lines lower-case]]
[clojure.core.incubator :only [-?>]]
[noir.util.crypt :only [encrypt]]
[hiccup.form]
[hiccup.core]
[hiccup.element]
[noir.response :only [redirect status content-type]]
[noir.core :only [defpage defpartial]]
[noir.statuses]))
[NoteHub.settings]
[clojure.string :rename {replace sreplace}
:only [escape split replace blank? split-lines lower-case]]
[clojure.core.incubator :only [-?>]]
[noir.util.crypt :only [encrypt]]
[hiccup.form]
[hiccup.core]
[noir.options :only [dev-mode?]]
[ring.util.codec :only [url-encode]]
[hiccup.element]
[hiccup.util :only [escape-html]]
[hiccup.page :only [include-js html5]]
[noir.response :only [redirect status content-type]]
[noir.core :only [defpage defpartial]]
[noir.statuses]))
(when-not (storage/valid-publisher? api/domain)
(storage/register-publisher api/domain))
; Creates the main html layout
(defpartial generate-layout
[params title & content]
; for the sake of security: escape all symbols of the param values
(let [params (into {} (for [[k v] params] [k (escape-html v)]))
theme (:theme params "default")
header-font (:header-font params)
text-font (:text-font params)]
(html5
[:head
[:title (print-str (get-message :name) "—" title)]
[:meta {:charset "UTF-8"}]
[:link {:rel "stylesheet/less" :type "text/css" :href "/style.less"}]
(html
(include-js "/js/less.js")
(include-js "/js/md5.js")
(include-js "/js/marked.js")
(include-js "/js/main.js")
(include-js "/js/themes.js"))
; google analytics code should appear in prod mode only
(if-not (dev-mode?) (include-js "/js/google-analytics.js"))]
[:body {:onload "onLoad()"} content])))
(defn layout
"Generates the main html layout"
[& args]
(if (map? (first args))
(apply generate-layout args)
(apply generate-layout {} args)))
; Sets a custom message for each needed HTTP status.
; The message to be assigned is extracted with a dynamically generated key
(doseq [code [400 403 404 500]]
@@ -32,16 +65,21 @@
(defn- response [code]
(status code (get-page code)))
(defn url
"Creates a local url from the given substrings"
[& args]
(apply str (interpose "/" (cons "" (map url-encode args)))))
; Converts given markdown to html and wraps with the main layout
(defn- wrap [short-url params md-text]
(when md-text
(layout params (params :title)
[:article.bottom-space.markdown md-text]
(let [links (map #(link-to
(if (= :short-url %)
(url short-url)
(str (params :title) "/" (name %)))
(get-message %))
(if (= :short-url %)
(url short-url)
(str (params :title) "/" (name %)))
(get-message %))
[:stats :edit :export :short-url])
space (apply str (repeat 4 " "))
separator (str space "·" space)
@@ -89,9 +127,9 @@
; Displays the note
(defpage "/:year/:month/:day/:title" {:keys [year month day title theme header-font text-font] :as params}
(wrap
(storage/create-short-url params)
(select-keys params [:title :theme :header-font :text-font])
(:note (api/get-note (api/build-key [year month day] title)))))
(storage/create-short-url params)
(select-keys params [:title :theme :header-font :text-font])
(:note (api/get-note (api/build-key [year month day] title)))))
; Provides Markdown of the specified note
(defpage "/:year/:month/:day/:title/export" {:keys [year month day title]}
@@ -156,8 +194,8 @@
psk (storage/get-psk pid)]
(if (storage/valid-publisher? pid)
(let [resp (api/update-note noteID note pid
(api/get-signature (str pid psk noteID note password))
password)]
(api/get-signature (str pid psk noteID note password))
password)]
(if (get-in resp [:status :success])
(redirect (:longURL resp))
(response 403)))
@@ -167,7 +205,7 @@
(defpage "/api" args
(let [title (get-message :api-title)]
(layout title [:article.markdown (slurp "API.md")])))
(layout title [:article.markdown (slurp "API.md")])))
(defpage [:get "/api/note"] {:keys [version noteID]}
(generate-string (api/get-note noteID)))