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]
(cond
(map? coll)
(merge
coll
(apply
(fn [field]
(if-let [timestamp (get coll field)]
{field (parse-timestamp timestamp)}))
fields))
(->> fields
(apply
#(if-let [timestamp (get coll %)]
{% (parse-timestamp timestamp)}))
(merge coll))
(seq? coll)
(map (fn [m] (string->date m fields)) coll)))
(map #(string->date % fields) coll)))
(defn date->string
"Does the reverse of string->date (converts LocalDate's back to strings)"
[coll fields]
(cond
(map? coll)
(merge
coll
(apply
(fn [field]
(if-let [date (get coll field)]
{field (get-timestamp date)}))
fields))
(->> fields
(apply
#(if-let [date (get coll %)]
{% (get-timestamp date)}))
(merge coll))
(seq? coll)
(map (fn [m] (date->string m fields)) coll)))
(map #(date->string % fields) coll)))
(defn ->relative-timestamp
"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}))]
(let [id (:_id 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)]
(if gz
(merge
@ -34,7 +34,7 @@
(couch/get-view "files" "listAllByPath" {:key p})))]
(map
(fn [f]
(let [attachment (second (first (:_attachments f)))]
(let [attachment (->> f :_attachments first second)]
{:id (:_id f)
:filename (:filename f)
:last_modified (parse-timestamp (:last_modified_at f))

View file

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

View file

@ -16,13 +16,14 @@
(defn list-files [path]
(let [p (ensure-prefix-suffix path "/")]
(layout/render
"files/list.html" {:html-title (->html-title [(str "Files in " p)])
:path p
:files (files/list-files p)
:tree (files/get-tree)
:error (session/flash-get :file-error)
:success (session/flash-get :file-success)
:notice (session/flash-get :file-notice)})))
"files/list.html"
{:html-title (->html-title [(str "Files in " p)])
:path p
:files (files/list-files p)
:tree (files/get-tree)
:error (session/flash-get :file-error)
:success (session/flash-get :file-success)
:notice (session/flash-get :file-notice)})))
(defn handle-new-file [path file returnpath]
(if (valid-upload? file)

View file

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

View file

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

View file

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