Blob


1 (ns blog.templates
2 (:require [blog.time :as time]
3 [hiccup.page :refer [html5 include-css]]
4 [commonmark-hiccup.core :refer [markdown->hiccup default-config]]))
6 (defn link-item [{:keys [url text]}]
7 [:li [:a {:href url} text]])
9 (defn header [{:keys [tags]}]
10 (list
11 [:header
12 [:nav
13 [:ul
14 (link-item {:url "/", :text "Home"})
15 (link-item {:url "/tags.html", :text "All Tags"})]]
16 [:div
17 [:h1 [:a {:href "/"} "yumh"]]
18 [:p "writing about things, sometimes."]]]))
20 (defn with-page
21 [{:keys [title class], :as d} & body]
22 (html5 {:lang "en"}
23 [:head
24 [:meta {:charset "utf8"}]
25 [:meta {:name "viewport", :content "width=device-width, initial-scale=1"}]
26 [:link {:rel "shortcut icon", :href "/favicon.ico"}]
27 [:title title]
28 (include-css "/css/style.css")]
29 [:body {:class (or class "")}
30 (header d)
31 [:main body]
32 [:footer
33 [:p "Blog powered by "
34 [:code "(clojure)"]]]]))
36 (defn post-fragment
37 [{:keys [full? title-with-link?]}
38 {:keys [title date slug tags short body], :as post}]
39 [:article
40 [:header
41 [:h1 (if title-with-link?
42 [:a {:href (str "/post/" slug ".html")} title]
43 title)]
44 [:p.author "Written by " [:em "Omar Polo"] " on " (time/fmt-loc date)]
45 [:ul.tags (map #(vector :li [:a {:href (str "/tag/" (name %) ".html")}
46 (str "#" (name %))])
47 tags)]]
48 [:section
49 (if full?
50 (markdown->hiccup default-config body)
51 [:p short])]])
53 (defn home-page
54 [{:keys [posts has-next has-prev nth]}]
55 (with-page {:title "Home"}
56 (map (partial post-fragment {:title-with-link? true})
57 posts)
58 [:nav.post-navigation
59 (if has-prev
60 [:a.prev {:href (str "/" (if (= (dec nth) 1)
61 "index"
62 (dec nth)) ".html")}
63 "« Newer Posts"])
64 (if has-next
65 [:a.next {:href (str "/" (inc nth) ".html")}
66 "Older Posts »"])]))
68 (defn post-page
69 [{:keys [title], :as post}]
70 (with-page {:title title
71 :class "article"}
72 (post-fragment {:full? true}
73 post)))
75 (defn tags-page
76 [tags]
77 (with-page {:title "All tags"
78 :class "tags"}
79 [:h2 "All tags"]
80 [:nav
81 [:ul
82 (map #(vector :li [:a {:href (str "/tag/" (name %) ".html")} (str "#" (name %))])
83 (sort (fn [a b]
84 (compare (.toLowerCase (name a))
85 (.toLowerCase (name b)))) tags))]]))
87 (defn tag-page
88 [tag posts]
89 (with-page {:title (str "Posts tagged with #" tag)
90 :class "tag"}
91 [:h2 "Post tagged with " [:code "#" tag]]
92 (map (partial post-fragment {:title-with-link? true})
93 (->> posts
94 (sort-by :date)
95 (reverse)))))