2 (:require [blog.time :as time]
3 [blog.templates :as templates]
6 [boot.task.built-in :as task]
7 [ring.adapter.jetty :as jetty]
8 [ring.middleware.resource :refer [wrap-resource]]
9 [ring.middleware.content-type :refer [wrap-content-type]]
10 [clojure.java.io :as io]
11 [clojure.java.shell :refer [sh]])
12 (:import (java.io File)))
14 (defn copy-file [src dst]
15 (with-open [in (io/input-stream (io/file src))
16 out (io/output-stream (io/file dst))]
19 (defn post [{:keys [slug title short date tags]}]
20 (let [f (io/resource (str "posts/" slug ".md"))]
24 :date (time/parse date)
28 (def per-tag (atom {}))
35 (swap! per-tag update t conj p))))
40 (doseq [d ["resources/out"
45 (.. (File. d) mkdirs)))
48 (let [tags (keys @per-tag)]
49 (map-indexed (fn [i posts]
50 {:filename (if (= i 0)
52 (str (inc i) ".html"))
58 (partition-all 6 @posts))))
61 "Fix the :has-prev/:has-next for the post pages. This assumes
62 that `(not (empty? post-pages))`"
66 (update 0 assoc :has-prev false)
67 (update (dec (count post-pages)) assoc :has-next false)))
69 (defn render-post-list []
70 (doseq [p (fix-next-last (post-pages))
71 :let [{:keys [filename]} p]]
72 (spit (str "resources/out/" filename)
73 (templates/home-page p))))
75 (defn render-post [{s :slug, :as post}]
76 (spit (str "resources/out/post/" s ".html")
77 (templates/post-page post)))
79 (defn render-tags [tags]
80 (spit (str "resources/out/tags.html")
81 (templates/tags-page tags)))
83 (defn render-tag [tag posts]
84 (spit (str "resources/out/tag/" tag ".html")
85 (templates/tag-page tag posts)))
88 (spit (str "resources/out/rss.xml")
92 "Copy the content of resources/`dir` to resources/out/`dir`, assuming
93 these two directories exists. It does not copy recursively."
95 (let [in (io/file (str "resources/" dir "/"))
96 out (str "resources/out/" dir "/")]
97 (doseq [f (->> in file-seq (filter #(.isFile %)))]
98 (io/copy f (io/file (str out (.getName f)))))))
102 (io/copy (io/file "resources/img/unbound-dashboard.png")
103 (io/file "resources/out/img/unbound-dashboard.png"))
107 "Copy css and images to their places"
110 (copy-file "resources/favicon.ico" "resources/out/favicon.ico")
111 (copy-file "resources/css/style.css" "resources/out/css/style.css"))
122 (render-tags (keys @per-tag))
124 :let [[tag posts] t]]
125 (render-tag (name tag) posts)))
134 (jetty/run-jetty (-> (fn [_] {:status 404, :body "not found"})
135 (wrap-resource "out")
141 "clean the output directory"
143 (sh "rm" "-rf" "resources/out/"))
146 "Copy the files to the server"
148 (sh "openrsync" "-r" "--delete" "resources/out/" "op:sites/www.omarpolo.com/"))