3 5e8f809e 2020-06-27 op [blog.rss :as rss]
4 5e8f809e 2020-06-27 op [blog.templates :as templates]
5 5e8f809e 2020-06-27 op [blog.time :as time]
6 5e8f809e 2020-06-27 op [clojure.edn :as edn]
7 5e8f809e 2020-06-27 op [clojure.java.io :as io]
8 5e8f809e 2020-06-27 op [clojure.java.shell :refer [sh]]
9 5e8f809e 2020-06-27 op [ring.adapter.jetty :as jetty]
10 5e8f809e 2020-06-27 op [ring.middleware.content-type :refer [wrap-content-type]]
11 5e8f809e 2020-06-27 op [ring.middleware.resource :refer [wrap-resource]])
12 5e8f809e 2020-06-27 op (:import (java.io File))
15 ddc03123 2020-03-28 op (defn copy-file [src dst]
16 ddc03123 2020-03-28 op (with-open [in (io/input-stream (io/file src))
17 ddc03123 2020-03-28 op out (io/output-stream (io/file dst))]
18 ddc03123 2020-03-28 op (io/copy in out)))
20 37740488 2020-06-05 op (defn post [{:keys [slug title short date tags toot]}]
21 ddc03123 2020-03-28 op (let [f (io/resource (str "posts/" slug ".md"))]
25 37740488 2020-06-05 op :date (time/parse date)
26 37740488 2020-06-05 op :body (slurp f)
30 ddc03123 2020-03-28 op (def per-tag (atom {}))
31 ddc03123 2020-03-28 op (def posts (atom []))
33 ddc03123 2020-03-28 op (defn add-post! [m]
34 ddc03123 2020-03-28 op (let [p (post m)]
35 ddc03123 2020-03-28 op (swap! posts conj p)
36 ddc03123 2020-03-28 op (doseq [t (:tags m)]
37 ddc03123 2020-03-28 op (swap! per-tag update t conj p))))
39 5e8f809e 2020-06-27 op (defn load-posts! []
40 5e8f809e 2020-06-27 op (reset! per-tag {})
41 5e8f809e 2020-06-27 op (reset! posts [])
42 5e8f809e 2020-06-27 op (doseq [p (-> "posts.edn"
45 5e8f809e 2020-06-27 op edn/read-string)]
46 5e8f809e 2020-06-27 op (add-post! p)))
48 ddc03123 2020-03-28 op (defn create-dirs! []
49 ddc03123 2020-03-28 op (doseq [d ["resources/out"
50 ddc03123 2020-03-28 op "resources/out/css"
51 ddc03123 2020-03-28 op "resources/out/post"
52 ddc03123 2020-03-28 op "resources/out/tag"
53 ddc03123 2020-03-28 op "resources/out/img"]]
54 ddc03123 2020-03-28 op (.. (File. d) mkdirs)))
56 ddc03123 2020-03-28 op (defn post-pages []
57 ddc03123 2020-03-28 op (let [tags (keys @per-tag)]
58 ddc03123 2020-03-28 op (map-indexed (fn [i posts]
59 ddc03123 2020-03-28 op {:filename (if (= i 0)
61 ddc03123 2020-03-28 op (str (inc i) ".html"))
65 ddc03123 2020-03-28 op :has-next true
66 ddc03123 2020-03-28 op :has-prev true})
67 ddc03123 2020-03-28 op (partition-all 6 @posts))))
69 ddc03123 2020-03-28 op (defn fix-next-last
70 ddc03123 2020-03-28 op "Fix the :has-prev/:has-next for the post pages. This assumes
71 ddc03123 2020-03-28 op that `(not (empty? post-pages))`"
73 ddc03123 2020-03-28 op (-> post-pages
74 ddc03123 2020-03-28 op (->> (into []))
75 ddc03123 2020-03-28 op (update 0 assoc :has-prev false)
76 ddc03123 2020-03-28 op (update (dec (count post-pages)) assoc :has-next false)))
78 ddc03123 2020-03-28 op (defn render-post-list []
79 ddc03123 2020-03-28 op (doseq [p (fix-next-last (post-pages))
80 ddc03123 2020-03-28 op :let [{:keys [filename]} p]]
81 ddc03123 2020-03-28 op (spit (str "resources/out/" filename)
82 ddc03123 2020-03-28 op (templates/home-page p))))
84 ddc03123 2020-03-28 op (defn render-post [{s :slug, :as post}]
85 ddc03123 2020-03-28 op (spit (str "resources/out/post/" s ".html")
86 ddc03123 2020-03-28 op (templates/post-page post)))
88 ddc03123 2020-03-28 op (defn render-tags [tags]
89 ddc03123 2020-03-28 op (spit (str "resources/out/tags.html")
90 ddc03123 2020-03-28 op (templates/tags-page tags)))
92 ddc03123 2020-03-28 op (defn render-tag [tag posts]
93 ddc03123 2020-03-28 op (spit (str "resources/out/tag/" tag ".html")
94 ddc03123 2020-03-28 op (templates/tag-page tag posts)))
96 4f9bd2e2 2020-03-30 op (defn render-rss []
97 4f9bd2e2 2020-03-30 op (spit (str "resources/out/rss.xml")
98 4f9bd2e2 2020-03-30 op (rss/feed @posts)))
100 ddc03123 2020-03-28 op (defn copy-dir
101 ddc03123 2020-03-28 op "Copy the content of resources/`dir` to resources/out/`dir`, assuming
102 ddc03123 2020-03-28 op these two directories exists. It does not copy recursively."
104 ddc03123 2020-03-28 op (let [in (io/file (str "resources/" dir "/"))
105 ddc03123 2020-03-28 op out (str "resources/out/" dir "/")]
106 ddc03123 2020-03-28 op (doseq [f (->> in file-seq (filter #(.isFile %)))]
107 ddc03123 2020-03-28 op (io/copy f (io/file (str out (.getName f)))))))
110 ddc03123 2020-03-28 op (copy-dir "img")
111 ddc03123 2020-03-28 op (io/copy (io/file "resources/img/unbound-dashboard.png")
112 ddc03123 2020-03-28 op (io/file "resources/out/img/unbound-dashboard.png"))
115 ddc03123 2020-03-28 op (defn copy-assets
116 ddc03123 2020-03-28 op "Copy css and images to their places"
118 ddc03123 2020-03-28 op (copy-dir "img")
119 ddc03123 2020-03-28 op (copy-file "resources/favicon.ico" "resources/out/favicon.ico")
120 ddc03123 2020-03-28 op (copy-file "resources/css/style.css" "resources/out/css/style.css"))
123 ddc03123 2020-03-28 op "Build the blog"
125 ddc03123 2020-03-28 op (create-dirs!)
126 ddc03123 2020-03-28 op (copy-assets)
128 ddc03123 2020-03-28 op (render-post-list)
129 ddc03123 2020-03-28 op (doseq [p @posts]
130 ddc03123 2020-03-28 op (render-post p))
131 ddc03123 2020-03-28 op (render-tags (keys @per-tag))
132 ddc03123 2020-03-28 op (doseq [t @per-tag
133 ddc03123 2020-03-28 op :let [[tag posts] t]]
134 ddc03123 2020-03-28 op (render-tag (name tag) posts)))
136 ddc03123 2020-03-28 op (def j (atom nil))
139 ddc03123 2020-03-28 op "Serve a preview"
143 ddc03123 2020-03-28 op (jetty/run-jetty (-> (fn [_] {:status 404, :body "not found"})
144 ddc03123 2020-03-28 op (wrap-resource "out")
145 ddc03123 2020-03-28 op (wrap-content-type))
147 ddc03123 2020-03-28 op :join? false})))
150 60905fa3 2020-03-29 op "clean the output directory"
152 ac603499 2020-03-29 op (sh "rm" "-rf" "resources/out/"))
155 ddc03123 2020-03-28 op "Copy the files to the server"
157 5e8f809e 2020-06-27 op (sh "rsync" "-r" "--delete" "resources/out/" "op:sites/www.omarpolo.com/"))
159 ddc03123 2020-03-28 op (defn stop-jetty []
161 ddc03123 2020-03-28 op (reset! j nil))
163 5e8f809e 2020-06-27 op (defn -main [& actions]
164 5e8f809e 2020-06-27 op (load-posts!)
165 5e8f809e 2020-06-27 op (doseq [action actions]
167 5e8f809e 2020-06-27 op "clean" (clean)
168 5e8f809e 2020-06-27 op "build" (build)
169 5e8f809e 2020-06-27 op "deploy" (deploy)
171 5e8f809e 2020-06-27 op (println "unrecognized action" action))))
174 5e8f809e 2020-06-27 op (load-posts!)