|
|
|
|
@ -24,13 +24,14 @@
@@ -24,13 +24,14 @@
|
|
|
|
|
(defn md-to-html [md-text] |
|
|
|
|
(.markdownToHtml (PegDownProcessor.) md-text)) |
|
|
|
|
|
|
|
|
|
; Creates a random session number |
|
|
|
|
; Creates a random session token |
|
|
|
|
(defn- get-flash-key [] |
|
|
|
|
(let [k (encrypt (str (rand-int Integer/MAX_VALUE)))] |
|
|
|
|
(do (flash-put! k true) |
|
|
|
|
(print-str k)))) |
|
|
|
|
|
|
|
|
|
; Sets a custom message for each corresponding HTTP status |
|
|
|
|
; Sets a custom message for each needed HTTP status. |
|
|
|
|
; The message to be assigned is extracted with a dynamically generated key |
|
|
|
|
(doseq [code [400 404 500]] |
|
|
|
|
(set-page! code |
|
|
|
|
(let [message (get-message (keyword (str "status-" code)))] |
|
|
|
|
@ -41,7 +42,7 @@
@@ -41,7 +42,7 @@
|
|
|
|
|
(defn- response [code] |
|
|
|
|
(status code (get-page code))) |
|
|
|
|
|
|
|
|
|
; Converts given markdwon to html and wraps with layout |
|
|
|
|
; Converts given markdown to html and wraps with the main layout |
|
|
|
|
(defn- wrap [params md-text] |
|
|
|
|
(if md-text |
|
|
|
|
(let [title (-?> md-text (split #"\n") first (sreplace #"[_\*#]" ""))] |
|
|
|
|
@ -51,7 +52,7 @@
@@ -51,7 +52,7 @@
|
|
|
|
|
; Routes |
|
|
|
|
; ====== |
|
|
|
|
|
|
|
|
|
; This function answers to a AJAX request: it gets a sesion key and markdown text. |
|
|
|
|
; This function answers to an AJAX request: it gets a sesion key and a markdown text. |
|
|
|
|
; It returns the html code of the provided markdown and a new session key. |
|
|
|
|
(defremote get-preview-md [session-key md] |
|
|
|
|
(when (flash-get session-key) |
|
|
|
|
@ -69,6 +70,7 @@
@@ -69,6 +70,7 @@
|
|
|
|
|
[:div.dashed-line] |
|
|
|
|
[:table.central-element.helvetica-neue |
|
|
|
|
[:tr |
|
|
|
|
; dynamically generates three column, retrieving corresponding messages |
|
|
|
|
(for [e [:column-why :column-how :column-geeks]] |
|
|
|
|
(html |
|
|
|
|
[:td.one-third-column |
|
|
|
|
@ -92,7 +94,7 @@
@@ -92,7 +94,7 @@
|
|
|
|
|
[:div#preview-start-line.dashed-line.hidden] |
|
|
|
|
[:article#preview])) |
|
|
|
|
|
|
|
|
|
; Display the note |
|
|
|
|
; Displays the note |
|
|
|
|
(defpage "/:year/:month/:day/:title" {:keys [year month day title theme header-font text-font] :as params} |
|
|
|
|
(wrap |
|
|
|
|
(select-keys params [:theme :header-font :text-font]) |
|
|
|
|
@ -105,7 +107,7 @@
@@ -105,7 +107,7 @@
|
|
|
|
|
|
|
|
|
|
; Provides the number of views of the specified note |
|
|
|
|
(defpage "/:year/:month/:day/:title/stat" {:keys [year month day title]} |
|
|
|
|
(let [views (get-views [year month day] title)] |
|
|
|
|
(let [views (get-note-views [year month day] title)] |
|
|
|
|
(if views |
|
|
|
|
(layout (get-message :statistics) |
|
|
|
|
[:table.helvetica-neue.central-element |
|
|
|
|
@ -117,16 +119,21 @@
@@ -117,16 +119,21 @@
|
|
|
|
|
[:td views]]]) |
|
|
|
|
(response 404)))) |
|
|
|
|
|
|
|
|
|
; New Note Posting |
|
|
|
|
; New Note Posting — the most "complex" function in the entire app ;) |
|
|
|
|
(defpage [:post "/post-note"] {:keys [draft session-key session-value]} |
|
|
|
|
(let [valid-session (flash-get session-key) ; it was posted from a newly generated form |
|
|
|
|
valid-draft (not (ccs/blank? draft)) ; the note has a meaningful content |
|
|
|
|
; first we collect all info needed to evaluate the validity of the note creation request |
|
|
|
|
(let [valid-session (flash-get session-key) ; was the note posted from a newly generated form? |
|
|
|
|
valid-draft (not (ccs/blank? draft)) ; has the note a meaningful content? |
|
|
|
|
; is the hash code correct? |
|
|
|
|
valid-hash (try |
|
|
|
|
(= (Short/parseShort session-value) ; the hash code is correct |
|
|
|
|
(= (Short/parseShort session-value) |
|
|
|
|
(lib/hash #(.codePointAt % 0) (str draft session-key))) |
|
|
|
|
(catch Exception e nil))] |
|
|
|
|
; check whether the new note can be added |
|
|
|
|
(if (and valid-session valid-draft valid-hash) |
|
|
|
|
; if yes, we compute the current date, extract a title string from the text, |
|
|
|
|
; which will be a part of the url and look whether this title is free today; |
|
|
|
|
; if not, append "-n", where "n" is the next free number |
|
|
|
|
(let [[year month day] (map #(+ (second %) (.get (Calendar/getInstance) (first %))) |
|
|
|
|
{Calendar/YEAR 0, Calendar/MONTH 1, Calendar/DAY_OF_MONTH 0}) |
|
|
|
|
; This is the _only_ point where user's content enters the web app, so we escape the content. |
|
|
|
|
@ -136,6 +143,7 @@
@@ -136,6 +143,7 @@
|
|
|
|
|
trim (fn [s] (apply str (drop-while #(= \- %) s))) |
|
|
|
|
title-uncut (-> untrimmed-line trim reverse trim reverse) |
|
|
|
|
max-length (get-setting :max-title-length #(Integer/parseInt %) 80) |
|
|
|
|
; TODO: replace to ccs/take when it gets fixed |
|
|
|
|
proposed-title (apply str (take max-length title-uncut)) |
|
|
|
|
date [year month day] |
|
|
|
|
title (first (drop-while #(note-exists? date %) |
|
|
|
|
@ -144,5 +152,5 @@
@@ -144,5 +152,5 @@
|
|
|
|
|
(do |
|
|
|
|
(set-note date title draft) |
|
|
|
|
; TODO: the redirect is broken if title contains UTF chars |
|
|
|
|
(redirect (apply str (interpose "/" ["" year month day title]))))) |
|
|
|
|
(redirect (url year month day title)))) |
|
|
|
|
(response 400)))) |
|
|
|
|
|