From a1527fec211945bc683c5564e601cd0c61854e9c Mon Sep 17 00:00:00 2001 From: Christian Mueller Date: Sat, 2 Jun 2012 13:41:23 +0200 Subject: [PATCH] session key problem at preview fixed --- src-cljs/main.cljs | 8 +++++--- src/NoteHub/views/common.clj | 6 ++++-- src/NoteHub/views/pages.clj | 39 +++++++++++++++++++++++------------- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src-cljs/main.cljs b/src-cljs/main.cljs index b9d2f42..ea5d0a7 100644 --- a/src-cljs/main.cljs +++ b/src-cljs/main.cljs @@ -10,6 +10,7 @@ ; frequently used selectors (def $draft ($ :#draft)) (def $preview ($ :#preview)) +(def $session-key ($ :#session-key)) (def $preview-start-line ($ :#preview-start-line)) (defn scroll-to @@ -30,12 +31,13 @@ (.click ($ :#preview-button) (fn [e] (do - (fm/remote (md-to-html (val $draft)) [result] + (fm/remote (get-preview-md (val $session-key) (val $draft)) [{:keys [preview session-key]}] (show $preview-start-line) - (inner $preview result) + (inner $preview preview) + (val $session-key session-key) (scroll-to $preview-start-line))))) (.click ($ :#publish-button) (fn [e] (val ($ :#session-value) - (nh/hash #(.charCodeAt % 0) (str (val $draft) (val ($ :#session-key))))))) + (nh/hash #(.charCodeAt % 0) (str (val $draft) (val $session-key)))))) diff --git a/src/NoteHub/views/common.clj b/src/NoteHub/views/common.clj index 0ee1510..ebb73e8 100644 --- a/src/NoteHub/views/common.clj +++ b/src/NoteHub/views/common.clj @@ -89,14 +89,16 @@ :border-bottom [:1px :dashed :gray] :margin-bottom :5em) (rule "h1, h2, h3, h4" - :font-family (gen-comma-list "'Noticia Text'" "Georgia")))) + :font-family (gen-comma-list "'Noticia Text'" "'PT Serif'")))) (defpartial layout [title & content] (html5 [:head [:title "NoteHub — " title] (include-js "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js") - [:link {:href "http://fonts.googleapis.com/css?family=Noticia+Text:400,700" + [:link {:href (str "http://fonts.googleapis.com/css?family=" + "PT+Serif:700|Noticia+Text:700" + "&subset=latin,cyrillic" ) :rel "stylesheet" :type "text/css"}] [:style {:type "text/css"} global-css]] diff --git a/src/NoteHub/views/pages.clj b/src/NoteHub/views/pages.clj index 2323ce0..b0dc844 100644 --- a/src/NoteHub/views/pages.clj +++ b/src/NoteHub/views/pages.clj @@ -20,8 +20,19 @@ (def max-title-length 80) ; Markdown -> HTML mapper -(defremote md-to-html [draft] - (.markdownToHtml (PegDownProcessor.) draft)) +(defn md-to-html [md-text] + (.markdownToHtml (PegDownProcessor.) md-text)) + +(defn get-flash-key [] + (let [k (encrypt (str (rand-int Integer/MAX_VALUE)))] + (do (flash-put! k true) + (print-str k)))) + +; This function answers to a corresponding AJAX request +(defremote get-preview-md [session-key md] + (when (flash-get session-key) + {:session-key (get-flash-key) + :preview (md-to-html md)})) ; Template for the error sites (defn page-setter [code message] @@ -50,8 +61,7 @@ (common/layout "New Markdown Note" [:div.central-element (form-to [:post "/post-note"] - (hidden-field :session-key (let [k (encrypt (str (rand-int Integer/MAX_VALUE)))] - (do (flash-put! k true) (print-str k)))) + (hidden-field :session-key (get-flash-key)) (hidden-field {:id :session-value} :session-value) (text-area {:class :max-width} :draft) [:div#buttons.hidden @@ -88,14 +98,15 @@ (let [valid-session (flash-get session-key) ; it was posted from a newly generated form valid-draft (not (empty? draft)) ; the note is non-empty valid-hash (= (Short/parseShort session-value) ; the hash code is correct - (nh/hash #(.codePointAt % 0) (str draft session-key)))] - (do - (println "session:" valid-session "draft:" valid-draft "hash:" - (Short/parseShort session-value) - (nh/hash #(.codePointAt % 0) (str draft session-key))) - (if (and valid-session valid-draft valid-hash) + (nh/hash #(.codePointAt % 0) (str draft session-key)))] (do - (set-note date title draft) - ; TODO: the redirect is broken if title contains UTF chars - (redirect (apply str (interpose "/" ["" year month day title])))) - (get-page 400)))))) + ; TODO: delete this if tests are written + (println "session:" valid-session "draft:" valid-draft "hash:" + (Short/parseShort session-value) + (nh/hash #(.codePointAt % 0) (str draft session-key))) + (if (and valid-session valid-draft valid-hash) + (do + (set-note date title draft) + ; TODO: the redirect is broken if title contains UTF chars + (redirect (apply str (interpose "/" ["" year month day title])))) + (get-page 400))))))