minor and mostly unnecessary code cleanups

This commit is contained in:
Gered 2013-07-30 21:40:56 -04:00
parent fe072d010f
commit 4b831d3030
7 changed files with 68 additions and 71 deletions

View file

@ -32,32 +32,28 @@
[coll fields] [coll fields]
(cond (cond
(map? coll) (map? coll)
(merge (->> fields
coll
(apply (apply
(fn [field] #(if-let [timestamp (get coll %)]
(if-let [timestamp (get coll field)] {% (parse-timestamp timestamp)}))
{field (parse-timestamp timestamp)})) (merge coll))
fields))
(seq? coll) (seq? coll)
(map (fn [m] (string->date m fields)) coll))) (map #(string->date % fields) coll)))
(defn date->string (defn date->string
"Does the reverse of string->date (converts LocalDate's back to strings)" "Does the reverse of string->date (converts LocalDate's back to strings)"
[coll fields] [coll fields]
(cond (cond
(map? coll) (map? coll)
(merge (->> fields
coll
(apply (apply
(fn [field] #(if-let [date (get coll %)]
(if-let [date (get coll field)] {% (get-timestamp date)}))
{field (get-timestamp date)})) (merge coll))
fields))
(seq? coll) (seq? coll)
(map (fn [m] (date->string m fields)) coll))) (map #(date->string % fields) coll)))
(defn ->relative-timestamp (defn ->relative-timestamp
"Returns a readable string representing the time between the current date "Returns a readable string representing the time between the current date

View file

@ -20,7 +20,7 @@
(couch/get-view "files" view-name {:key f}))] (couch/get-view "files" view-name {:key f}))]
(let [id (:_id file-info) (let [id (:_id file-info)
attachment (:filename file-info) attachment (:filename file-info)
attachment-info (second (first (:_attachments file-info))) attachment-info (->> file-info :_attachments first second)
gz (couch/get-attachment id attachment)] gz (couch/get-attachment id attachment)]
(if gz (if gz
(merge (merge
@ -34,7 +34,7 @@
(couch/get-view "files" "listAllByPath" {:key p})))] (couch/get-view "files" "listAllByPath" {:key p})))]
(map (map
(fn [f] (fn [f]
(let [attachment (second (first (:_attachments f)))] (let [attachment (->> f :_attachments first second)]
{:id (:_id f) {:id (:_id f)
:filename (:filename f) :filename (:filename f)
:last_modified (parse-timestamp (:last_modified_at f)) :last_modified (parse-timestamp (:last_modified_at f))

View file

@ -96,14 +96,10 @@
(if-let [posts (->post-list (if-let [posts (->post-list
(couch/with-db posts (couch/with-db posts
(couch/get-view "posts" view-name {:descending true})))] (couch/get-view "posts" view-name {:descending true})))]
(vals (->> posts
(group-by (map #(assoc % :mmyyyy (->nicer-month-year-str (:mmyyyy %))))
(fn [p] (group-by #(:mmyyyy %))
(:mmyyyy p)) (vals)))))
(map
(fn [p]
(assoc p :mmyyyy (->nicer-month-year-str (:mmyyyy p))))
posts))))))
(defn count-posts (defn count-posts
[unpublished?] [unpublished?]

View file

@ -16,7 +16,8 @@
(defn list-files [path] (defn list-files [path]
(let [p (ensure-prefix-suffix path "/")] (let [p (ensure-prefix-suffix path "/")]
(layout/render (layout/render
"files/list.html" {:html-title (->html-title [(str "Files in " p)]) "files/list.html"
{:html-title (->html-title [(str "Files in " p)])
:path p :path p
:files (files/list-files p) :files (files/list-files p)
:tree (files/get-tree) :tree (files/get-tree)

View file

@ -13,11 +13,9 @@
[blarg.routes.auth :as auth])) [blarg.routes.auth :as auth]))
(defn string->tags [s] (defn string->tags [s]
(set (->> (clojure.string/split s #",")
(map (map #(->slug %))
(fn [tag] (set)))
(->slug tag))
(clojure.string/split s #","))))
(defn tags->string [tags] (defn tags->string [tags]
(clojure.string/join "," tags)) (clojure.string/join "," tags))
@ -44,7 +42,8 @@
currentpage (make-in-range page 0 lastpage) currentpage (make-in-range page 0 lastpage)
offset (* currentpage posts/per-page)] offset (* currentpage posts/per-page)]
(layout/render (layout/render
"posts/list.html" {:posts (posts/list-posts (auth/logged-in?) posts/per-page offset) "posts/list.html"
{:posts (posts/list-posts (auth/logged-in?) posts/per-page offset)
:prevpage (- currentpage 1) :prevpage (- currentpage 1)
:nextpage (+ currentpage 1) :nextpage (+ currentpage 1)
:atlastpage (= currentpage lastpage) :atlastpage (= currentpage lastpage)
@ -53,25 +52,29 @@
(defn list-by-tag [tag] (defn list-by-tag [tag]
(layout/render (layout/render
"posts/listbytag.html" {:posts (posts/list-posts-by-tag (auth/logged-in?) tag) "posts/listbytag.html"
{:posts (posts/list-posts-by-tag (auth/logged-in?) tag)
:tag tag})) :tag tag}))
(defn list-archive [] (defn list-archive []
(layout/render (layout/render
"posts/listarchive.html" {:months (posts/list-posts-archive (auth/logged-in?))})) "posts/listarchive.html"
{:months (posts/list-posts-archive (auth/logged-in?))}))
(defn show-post-page [year month day slug] (defn show-post-page [year month day slug]
(let [date (str (string->int year) "-" (string->int month) "-" (string->int day)) (let [date (str (string->int year) "-" (string->int month) "-" (string->int day))
post (posts/get-post-by-date-slug date slug)] post (posts/get-post-by-date-slug date slug)]
(if (not-empty post) (if (not-empty post)
(layout/render (layout/render
"posts/showpost.html" {:post post "posts/showpost.html"
{:post post
:html-title (->html-title [(:title post)])}) :html-title (->html-title [(:title post)])})
(resp/redirect "/notfound")))) (resp/redirect "/notfound"))))
(defn new-post-page [& post] (defn new-post-page [& post]
(layout/render (layout/render
"posts/newpost.html" (merge (first post) "posts/newpost.html"
(merge (first post)
{:all-tags (posts/list-tags) {:all-tags (posts/list-tags)
:html-title (->html-title ["New Post"]) :html-title (->html-title ["New Post"])
:validation-errors @vali/*errors*}))) :validation-errors @vali/*errors*})))
@ -90,7 +93,8 @@
(first posted-post) (first posted-post)
(posts/get-post id))] (posts/get-post id))]
(layout/render (layout/render
"posts/editpost.html" (merge post "posts/editpost.html"
(merge post
{:tags (tags->string (:tags post)) {:tags (tags->string (:tags post))
:all-tags (posts/list-tags) :all-tags (posts/list-tags)
:html-title (->html-title ["Edit Post"]) :html-title (->html-title ["Edit Post"])

View file

@ -15,18 +15,17 @@
(defn handle-rss [] (defn handle-rss []
(let [channel {:title rss-title (let [channel {:title rss-title
:link rss-site-url :link rss-site-url
:description rss-description} :description rss-description}]
items (doall (resp/content-type "text/xml"
(map (apply
(fn [post] (partial rss/channel-xml channel)
(->> (posts/list-posts false 10)
(map (fn [post]
{:title (:title post) {:title (:title post)
:pubDate (clj-time.coerce/to-date (:created_at post)) :pubDate (clj-time.coerce/to-date (:created_at post))
:link (str rss-site-url (subs (get-post-url post) 1)) :link (str rss-site-url (subs (get-post-url post) 1))
:description (md/md-to-html-string (:body post))}) :description (md/md-to-html-string (:body post))}))
(posts/list-posts false 10)))] (doall))))))
(resp/content-type "text/xml"
(apply (partial rss/channel-xml channel)
items))))
(defroutes rss-routes (defroutes rss-routes
(GET "/rss" [] (handle-rss))) (GET "/rss" [] (handle-rss)))

View file

@ -7,7 +7,8 @@
(def template-path "blarg/views/templates/") (def template-path "blarg/views/templates/")
(defn render [template & [params]] (defn render [template & [params]]
(parser/render-file (str template-path template) (parser/render-file
(str template-path template)
(assoc params (assoc params
:context (:context *request*) :context (:context *request*)
:user-id (session/get :user)))) :user-id (session/get :user))))