|
|
|
@ -1,70 +1,16 @@ |
|
|
|
(ns notehub.handler |
|
|
|
(ns notehub.handler |
|
|
|
(:use compojure.core |
|
|
|
(:use compojure.core |
|
|
|
|
|
|
|
[notehub.views] |
|
|
|
[notehub.settings] |
|
|
|
[notehub.settings] |
|
|
|
[clojure.string :rename {replace sreplace} :only [replace]] |
|
|
|
[clojure.string :rename {replace sreplace} :only [replace]] |
|
|
|
[clojure.core.incubator :only [-?>]] |
|
|
|
[clojure.core.incubator :only [-?>]]) |
|
|
|
[hiccup.form] |
|
|
|
(:require |
|
|
|
[hiccup.core] |
|
|
|
[hiccup.util :as util] |
|
|
|
[hiccup.element] |
|
|
|
[compojure.handler :as handler] |
|
|
|
[hiccup.util :only [escape-html]] |
|
|
|
[compojure.route :as route] |
|
|
|
[hiccup.page :only [include-js html5]]) |
|
|
|
[notehub.api :as api] |
|
|
|
(:require [compojure.handler :as handler] |
|
|
|
[notehub.storage :as storage] |
|
|
|
[compojure.route :as route] |
|
|
|
[cheshire.core :refer :all])) |
|
|
|
[hiccup.util :as util] |
|
|
|
|
|
|
|
[notehub.api :as api] |
|
|
|
|
|
|
|
[notehub.storage :as storage] |
|
|
|
|
|
|
|
[cheshire.core :refer :all])) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; Creates the main html layout |
|
|
|
|
|
|
|
(defn layout |
|
|
|
|
|
|
|
[title & content] |
|
|
|
|
|
|
|
(html5 |
|
|
|
|
|
|
|
[:head |
|
|
|
|
|
|
|
[:title (print-str (get-message :name) "—" title)] |
|
|
|
|
|
|
|
[:meta {:charset "UTF-8"}] |
|
|
|
|
|
|
|
[:meta {:name "viewport" :content "width=device-width, initial-scale=1.0"}] |
|
|
|
|
|
|
|
[:link {:rel "stylesheet/less" :type "text/css" :href "/styles/main.less"}] |
|
|
|
|
|
|
|
(html |
|
|
|
|
|
|
|
(include-js "/js/less.js") |
|
|
|
|
|
|
|
(include-js "/js/themes.js") |
|
|
|
|
|
|
|
(include-js "/js/md5.js") |
|
|
|
|
|
|
|
(include-js "/js/marked.js") |
|
|
|
|
|
|
|
(include-js "/js/main.js")) |
|
|
|
|
|
|
|
; google analytics code should appear in prod mode only |
|
|
|
|
|
|
|
(if-not (get-setting :dev-mode) (include-js "/js/google-analytics.js"))] |
|
|
|
|
|
|
|
[:body {:onload "onLoad()"} content])) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defn md-node |
|
|
|
|
|
|
|
"Returns an HTML element with a textarea inside |
|
|
|
|
|
|
|
containing the markdown text (to keep all chars unescaped)" |
|
|
|
|
|
|
|
([cls input] (md-node cls {} input)) |
|
|
|
|
|
|
|
([cls opts input] |
|
|
|
|
|
|
|
[(keyword (str (name cls) ".markdown")) opts |
|
|
|
|
|
|
|
[:textarea input]])) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defn sanitize |
|
|
|
|
|
|
|
"Breakes all usages of <script> & <iframe>" |
|
|
|
|
|
|
|
[input] |
|
|
|
|
|
|
|
(sreplace input #"(</?(iframe|script).*?>|javascript:)" "")) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; input form for the markdown text with a preview area |
|
|
|
|
|
|
|
(defn input-form [form-url command fields content passwd-msg] |
|
|
|
|
|
|
|
(let [css-class (when (= :publish command) :hidden)] |
|
|
|
|
|
|
|
(layout (get-message :new-note) |
|
|
|
|
|
|
|
[:article#preview ""] |
|
|
|
|
|
|
|
[:div#dashed-line {:class css-class}] |
|
|
|
|
|
|
|
[:div.central-element.helvetica {:style "margin-bottom: 3em"} |
|
|
|
|
|
|
|
(form-to {:autocomplete :off} [:post form-url] |
|
|
|
|
|
|
|
(hidden-field :action command) |
|
|
|
|
|
|
|
(hidden-field :version api/version) |
|
|
|
|
|
|
|
(hidden-field :password) |
|
|
|
|
|
|
|
fields |
|
|
|
|
|
|
|
(text-area {:class :max-width} :note content) |
|
|
|
|
|
|
|
[:fieldset#input-elems {:class css-class} |
|
|
|
|
|
|
|
(text-field {:class "ui-elem" :placeholder (get-message passwd-msg)} |
|
|
|
|
|
|
|
:plain-password) |
|
|
|
|
|
|
|
(submit-button {:class "button ui-elem" |
|
|
|
|
|
|
|
:id :publish-button} (get-message command))])]))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; TODO: make sure the status is really set to the response!!!! |
|
|
|
; TODO: make sure the status is really set to the response!!!! |
|
|
|
(defn- response |
|
|
|
(defn- response |
|
|
|
@ -102,18 +48,7 @@ |
|
|
|
(context "/api" [] |
|
|
|
(context "/api" [] |
|
|
|
#(ring.util.response/content-type (api-routes %) "application/json")) |
|
|
|
#(ring.util.response/content-type (api-routes %) "application/json")) |
|
|
|
|
|
|
|
|
|
|
|
(GET "/" [] |
|
|
|
(GET "/" [] landing-page) |
|
|
|
(layout (get-message :page-title) |
|
|
|
|
|
|
|
[:div#hero |
|
|
|
|
|
|
|
[:h1 (get-message :name)] |
|
|
|
|
|
|
|
[:h2 (get-message :title)] |
|
|
|
|
|
|
|
[:br] |
|
|
|
|
|
|
|
[:a.landing-button {:href "/new" :style "color: white"} (get-message :new-page)]] |
|
|
|
|
|
|
|
[:div#dashed-line] |
|
|
|
|
|
|
|
(md-node :article.helvetica.bottom-space |
|
|
|
|
|
|
|
{:style "font-size: 1em"} |
|
|
|
|
|
|
|
(slurp "LANDING.md")) |
|
|
|
|
|
|
|
(md-node :div.centered.helvetica (get-message :footer)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(GET "/:year/:month/:day/:title/export" [year month day title] |
|
|
|
(GET "/:year/:month/:day/:title/export" [year month day title] |
|
|
|
(when-let [md-text (:note (api/get-note {:noteID (api/build-key year month day title)}))] |
|
|
|
(when-let [md-text (:note (api/get-note {:noteID (api/build-key year month day title)}))] |
|
|
|
@ -121,51 +56,15 @@ |
|
|
|
|
|
|
|
|
|
|
|
(GET "/:year/:month/:day/:title/stats" [year month day title] |
|
|
|
(GET "/:year/:month/:day/:title/stats" [year month day title] |
|
|
|
(when-let [resp (api/get-note {:noteID (api/build-key year month day title)})] |
|
|
|
(when-let [resp (api/get-note {:noteID (api/build-key year month day title)})] |
|
|
|
(let [stats (:statistics resp) |
|
|
|
(statistics-page resp))) |
|
|
|
statistics (get-message :statistics)] |
|
|
|
|
|
|
|
(layout statistics |
|
|
|
|
|
|
|
[:h2.central-element (api/derive-title (:note resp))] |
|
|
|
|
|
|
|
[:h3.central-element.helvetica statistics] |
|
|
|
|
|
|
|
[:table#stats.helvetica.central-element |
|
|
|
|
|
|
|
(map |
|
|
|
|
|
|
|
#(when-let [v (% stats)] |
|
|
|
|
|
|
|
[:tr |
|
|
|
|
|
|
|
[:td (str (get-message %) ":")] |
|
|
|
|
|
|
|
[:td (if (or (= % :published) (= % :edited)) |
|
|
|
|
|
|
|
(str (java.util.Date. (Long/parseLong v))) v)]]) |
|
|
|
|
|
|
|
[:published :edited :publisher :views])])))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(GET "/:year/:month/:day/:title/edit" [year month day title] |
|
|
|
(GET "/:year/:month/:day/:title/edit" [year month day title] |
|
|
|
(let [noteID (api/build-key year month day title)] |
|
|
|
(note-update-page year month day title)) |
|
|
|
(input-form "/update-note" :update |
|
|
|
|
|
|
|
(html (hidden-field :noteID noteID)) |
|
|
|
|
|
|
|
(:note (api/get-note {:noteID noteID})) :enter-passwd))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(GET "/new" [] |
|
|
|
(GET "/new" [] new-note-page) |
|
|
|
(input-form "/post-note" :publish |
|
|
|
|
|
|
|
(html (hidden-field :session (storage/sign (str (rand-int Integer/MAX_VALUE)))) |
|
|
|
|
|
|
|
(hidden-field {:id :signature} :signature)) |
|
|
|
|
|
|
|
(get-message :loading) :set-passwd)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(GET "/:year/:month/:day/:title" [year month day title :as params] |
|
|
|
(GET "/:year/:month/:day/:title" [year month day title :as params] |
|
|
|
(let [params (assoc (:query-params params) |
|
|
|
(note-page year month day title params)) |
|
|
|
:year year :month month :day day :title title) |
|
|
|
|
|
|
|
noteID (api/build-key year month day title)] |
|
|
|
|
|
|
|
(when (storage/note-exists? noteID) |
|
|
|
|
|
|
|
(let [note (api/get-note {:noteID noteID}) |
|
|
|
|
|
|
|
sanitized-note (sanitize (:note note))] |
|
|
|
|
|
|
|
(layout (:title note) |
|
|
|
|
|
|
|
(md-node :article.bottom-space sanitized-note) |
|
|
|
|
|
|
|
(let [urls {:short-url (api/url (storage/create-short-url noteID params)) |
|
|
|
|
|
|
|
:notehub "/"} |
|
|
|
|
|
|
|
links (map #(link-to |
|
|
|
|
|
|
|
(if (urls %) |
|
|
|
|
|
|
|
(urls %) |
|
|
|
|
|
|
|
(str (:longURL note) "/" (name %))) |
|
|
|
|
|
|
|
(get-message %)) |
|
|
|
|
|
|
|
[:notehub :stats :edit :export :short-url]) |
|
|
|
|
|
|
|
links (interpose [:span.middot "·"] links)] |
|
|
|
|
|
|
|
[:div#links links])))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(GET "/:short-url" [short-url] |
|
|
|
(GET "/:short-url" [short-url] |
|
|
|
(when-let [params (storage/resolve-url short-url)] |
|
|
|
(when-let [params (storage/resolve-url short-url)] |
|
|
|
|