lots of minor improvements; storage interface improvement
This commit is contained in:
+5
-2
@@ -1,6 +1,7 @@
|
|||||||
(ns NoteHub.main
|
(ns NoteHub.main
|
||||||
(:use [jayq.core :only [$ css inner val anim]])
|
(:use [jayq.core :only [$ css inner val anim show]])
|
||||||
(:require [fetch.remotes :as remotes]
|
(:require [fetch.remotes :as remotes]
|
||||||
|
[goog.dom :as gdom]
|
||||||
[clojure.browser.dom :as dom]
|
[clojure.browser.dom :as dom]
|
||||||
[clojure.browser.event :as event])
|
[clojure.browser.event :as event])
|
||||||
(:require-macros [fetch.macros :as fm]))
|
(:require-macros [fetch.macros :as fm]))
|
||||||
@@ -8,6 +9,7 @@
|
|||||||
; frequently used selectors
|
; frequently used selectors
|
||||||
(def $draft ($ :#draft))
|
(def $draft ($ :#draft))
|
||||||
(def $preview ($ :#preview))
|
(def $preview ($ :#preview))
|
||||||
|
(def $preview-start-line ($ :#preview-start-line))
|
||||||
|
|
||||||
(defn scroll-to
|
(defn scroll-to
|
||||||
"scrolls to the given selector"
|
"scrolls to the given selector"
|
||||||
@@ -28,5 +30,6 @@
|
|||||||
(fn [e]
|
(fn [e]
|
||||||
(do
|
(do
|
||||||
(fm/remote (md-to-html (val $draft)) [result]
|
(fm/remote (md-to-html (val $draft)) [result]
|
||||||
|
(show $preview-start-line)
|
||||||
(inner $preview result)
|
(inner $preview result)
|
||||||
(scroll-to ($ :#preview-start))))))
|
(scroll-to $preview-start-line)))))
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
(ns NoteHub.storage
|
(ns NoteHub.storage
|
||||||
(:refer-clojure :exclude (set get))
|
|
||||||
(:require [clj-redis.client :as redis]))
|
(:require [clj-redis.client :as redis]))
|
||||||
|
|
||||||
(def db (redis/init))
|
(def db (redis/init))
|
||||||
|
|
||||||
(defn set [k v]
|
(def note "note")
|
||||||
(redis/set db k v))
|
|
||||||
|
|
||||||
(defn get [k]
|
(defn- build-key [[year month day] key]
|
||||||
(redis/get db k))
|
(print-str year month day key))
|
||||||
|
|
||||||
|
(defn set-note [date key v]
|
||||||
|
(redis/hset db note (build-key date key) v))
|
||||||
|
|
||||||
|
(defn get-note [date key]
|
||||||
|
(redis/hget db note (build-key date key)))
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
(defn gen-comma-list [& fonts] (apply str (interpose "," fonts)))
|
(defn gen-comma-list [& fonts] (apply str (interpose "," fonts)))
|
||||||
(def page-width
|
(def page-width
|
||||||
(mixin
|
(mixin
|
||||||
:width :900px))
|
:width :800px))
|
||||||
(def helvetica-neue
|
(def helvetica-neue
|
||||||
(mixin
|
(mixin
|
||||||
:font-weight 300
|
:font-weight 300
|
||||||
@@ -17,6 +17,13 @@
|
|||||||
"Arial"
|
"Arial"
|
||||||
"'Lucida Grande'"
|
"'Lucida Grande'"
|
||||||
"sans-serif")))
|
"sans-serif")))
|
||||||
|
(def central-element
|
||||||
|
(mixin
|
||||||
|
page-width
|
||||||
|
:margin-top :5em
|
||||||
|
:margin-bottom :10em
|
||||||
|
:margin-left "auto"
|
||||||
|
:margin-right "auto"))
|
||||||
|
|
||||||
(def global-css
|
(def global-css
|
||||||
(css
|
(css
|
||||||
@@ -43,6 +50,7 @@
|
|||||||
(rule "h2"
|
(rule "h2"
|
||||||
helvetica-neue))
|
helvetica-neue))
|
||||||
(rule "article"
|
(rule "article"
|
||||||
|
central-element
|
||||||
:font-family :Georgia
|
:font-family :Georgia
|
||||||
:font-size :1.2em
|
:font-size :1.2em
|
||||||
(rule "& > h1:first-child"
|
(rule "& > h1:first-child"
|
||||||
@@ -61,7 +69,7 @@
|
|||||||
:font-size :1.2em
|
:font-size :1.2em
|
||||||
:border :none
|
:border :none
|
||||||
; TODO: make this dynamic
|
; TODO: make this dynamic
|
||||||
:height :600px
|
:height :500px
|
||||||
:margin-bottom :2em)
|
:margin-bottom :2em)
|
||||||
(rule ".hidden"
|
(rule ".hidden"
|
||||||
:display :none)
|
:display :none)
|
||||||
@@ -73,15 +81,11 @@
|
|||||||
:opacity 0.8
|
:opacity 0.8
|
||||||
:font-size :1em
|
:font-size :1em
|
||||||
:background :white)
|
:background :white)
|
||||||
(rule ".central-body"
|
(rule ".central-element"
|
||||||
page-width
|
central-element)
|
||||||
:margin-top :5em
|
|
||||||
:margin-bottom :10em
|
|
||||||
:margin-left "auto"
|
|
||||||
:margin-right "auto")
|
|
||||||
(rule "h1"
|
(rule "h1"
|
||||||
:font-size :2em)
|
:font-size :2em)
|
||||||
(rule "#preview-start"
|
(rule "#preview-start-line"
|
||||||
:border-bottom [:1px :dashed :gray]
|
:border-bottom [:1px :dashed :gray]
|
||||||
:margin-bottom :5em)
|
:margin-bottom :5em)
|
||||||
(rule "h1, h2, h3, h4"
|
(rule "h1, h2, h3, h4"
|
||||||
@@ -90,7 +94,7 @@
|
|||||||
(defpartial layout [title & content]
|
(defpartial layout [title & content]
|
||||||
(html5
|
(html5
|
||||||
[:head
|
[:head
|
||||||
[:title "NoteHub - " title]
|
[:title "NoteHub — " title]
|
||||||
(include-js "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js")
|
(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 "http://fonts.googleapis.com/css?family=Noticia+Text:400,700"
|
||||||
:rel "stylesheet"
|
:rel "stylesheet"
|
||||||
|
|||||||
+43
-22
@@ -1,16 +1,34 @@
|
|||||||
(ns NoteHub.views.pages
|
(ns NoteHub.views.pages
|
||||||
(:require [NoteHub.views.common :as common])
|
(:require [NoteHub.views.common :as common])
|
||||||
(:use
|
(:use
|
||||||
[NoteHub.storage :rename {get s-get set s-set} :only [set get]]
|
[NoteHub.storage]
|
||||||
[noir.response :only [redirect]]
|
|
||||||
[clojure.string :rename {replace sreplace} :only [split replace lower-case]]
|
[clojure.string :rename {replace sreplace} :only [split replace lower-case]]
|
||||||
[noir.core :only [defpage render]]
|
[clojure.core.incubator :only [-?>]]
|
||||||
[hiccup.form]
|
[hiccup.form]
|
||||||
|
[noir.response :only [redirect]]
|
||||||
|
[noir.core :only [defpage render]]
|
||||||
|
[noir.statuses]
|
||||||
[noir.fetch.remotes])
|
[noir.fetch.remotes])
|
||||||
(:import [org.pegdown PegDownProcessor]))
|
(:import [org.pegdown PegDownProcessor]))
|
||||||
|
|
||||||
|
; Fix a maximal title length used in the link
|
||||||
(def max-title-length 80)
|
(def max-title-length 80)
|
||||||
|
|
||||||
|
; Markdown -> HTML mapper
|
||||||
|
(defremote md-to-html [draft]
|
||||||
|
(.markdownToHtml (PegDownProcessor.) draft))
|
||||||
|
|
||||||
|
; Template for the 404 error
|
||||||
|
(set-page! 404
|
||||||
|
(let [message "Page Not Found."]
|
||||||
|
(common/layout message
|
||||||
|
[:article
|
||||||
|
[:h1 message]])))
|
||||||
|
|
||||||
|
; Routes
|
||||||
|
; ======
|
||||||
|
|
||||||
|
; Landing Page
|
||||||
(defpage "/" {}
|
(defpage "/" {}
|
||||||
(common/layout "Free Markdown Hosting"
|
(common/layout "Free Markdown Hosting"
|
||||||
[:div#hero
|
[:div#hero
|
||||||
@@ -19,41 +37,44 @@
|
|||||||
[:br]
|
[:br]
|
||||||
[:a.landing-button {:href "/new"} "New Page"]]))
|
[:a.landing-button {:href "/new"} "New Page"]]))
|
||||||
|
|
||||||
|
; New Note Page
|
||||||
(defpage "/new" {}
|
(defpage "/new" {}
|
||||||
(common/layout "New Markdown Note"
|
(common/layout "New Markdown Note"
|
||||||
[:div.central-body
|
[:div.central-element
|
||||||
(form-to [:post "/post-note"]
|
(form-to [:post "/post-note"]
|
||||||
(text-area {:class "max-width"} :draft)
|
(text-area {:class "max-width"} :draft)
|
||||||
[:div#buttons.hidden
|
[:div#buttons.hidden
|
||||||
(submit-button {:style "float: left" :class "button"} "Publish")
|
(submit-button {:style "float: left" :class "button"} "Publish")
|
||||||
[:button#preview-button.button {:type :button :style "float: right"} "Preview"]])]
|
[:button#preview-button.button {:type :button :style "float: right"} "Preview"]])]
|
||||||
[:div#preview-start]
|
[:div#preview-start-line.hidden]
|
||||||
[:article#preview.central-body]))
|
[:article#preview]))
|
||||||
|
|
||||||
(defn get-storage-key [year month day title]
|
|
||||||
(apply str (interpose "-" ["note" year month day (hash [year month day title])])))
|
|
||||||
|
|
||||||
(defremote md-to-html [draft]
|
|
||||||
(.markdownToHtml (PegDownProcessor.) draft))
|
|
||||||
|
|
||||||
|
; Note URL
|
||||||
(defpage "/:year/:month/:day/:title" {:keys [year month day title]}
|
(defpage "/:year/:month/:day/:title" {:keys [year month day title]}
|
||||||
(let [key (get-storage-key year month day title)
|
(let [date [year month day]
|
||||||
post (s-get key)
|
key (hash (conj date title))
|
||||||
title (sreplace (-> post (split #"\n") first) #"[_\*#]" "")]
|
post (get-note date key)
|
||||||
(common/layout title
|
title (-?> post (split #"\n") first (sreplace #"[_\*#]" ""))]
|
||||||
[:article.central-body
|
(if post
|
||||||
; TODO: deal with 404!
|
(common/layout title
|
||||||
(md-to-html post)])))
|
[:article
|
||||||
|
(md-to-html post)])
|
||||||
|
(get-page 400))))
|
||||||
|
|
||||||
|
; New Note Posting
|
||||||
(defpage [:post "/post-note"] {:keys [draft]}
|
(defpage [:post "/post-note"] {:keys [draft]}
|
||||||
(let [[year month day] (split (.format (java.text.SimpleDateFormat. "yyyy-MM-dd") (java.util.Date.)) #"-")
|
(let [[year month day] (split
|
||||||
|
(.format
|
||||||
|
(java.text.SimpleDateFormat. "yyyy-MM-dd")
|
||||||
|
(java.util.Date.)) #"-")
|
||||||
untrimmed-line (filter #(or (= \- %) (Character/isLetterOrDigit %))
|
untrimmed-line (filter #(or (= \- %) (Character/isLetterOrDigit %))
|
||||||
(-> draft (split #"\n") first (sreplace " " "-") lower-case))
|
(-> draft (split #"\n") first (sreplace " " "-") lower-case))
|
||||||
trim (fn [s] (apply str (drop-while #(= \- %) s)))
|
trim (fn [s] (apply str (drop-while #(= \- %) s)))
|
||||||
title-uncut (-> untrimmed-line trim reverse trim reverse)
|
title-uncut (-> untrimmed-line trim reverse trim reverse)
|
||||||
title (apply str (take max-title-length title-uncut))
|
title (apply str (take max-title-length title-uncut))
|
||||||
; TODO: deal with collisions!
|
; TODO: deal with collisions!
|
||||||
key (get-storage-key year month day title)]
|
date [year month day]
|
||||||
|
key (hash (conj date title))]
|
||||||
(do
|
(do
|
||||||
(s-set key draft)
|
(set-note date key draft)
|
||||||
(redirect (apply str (interpose "/" ["" year month day title]))))))
|
(redirect (apply str (interpose "/" ["" year month day title]))))))
|
||||||
|
|||||||
Reference in New Issue
Block a user