-
Notifications
You must be signed in to change notification settings - Fork 38
/
perun.clj
191 lines (168 loc) · 6.49 KB
/
perun.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
(ns io.perun
{:boot/export-tasks true}
(:require [boot.core :as boot :refer [deftask]]
[boot.pod :as pod]
[boot.util :as u]
[io.perun.core :as perun]))
(def ^:private global-deps
'[[clj-time "0.9.0"]])
(def ^:private
+defaults+ {})
(defn- create-pod [deps]
(-> (boot/get-env)
(update-in [:dependencies] into global-deps)
(update-in [:dependencies] into deps)
pod/make-pod
future))
(defn find-data-file [fileset filename]
(->> fileset boot/input-files (boot/by-name [filename]) first))
(def ^:private markdown-deps
'[[markdown-clj "0.9.40"]
[endophile "0.1.2"]])
(def ^:private +markdown-defaults+
(merge +defaults+
{:create-filename "io.perun.markdown/generate-filename"}))
(defn- commit [fileset tmp]
(-> fileset
(boot/add-resource tmp)
boot/commit!))
(deftask markdown
"Parse markdown files"
[f create-filename CREATE_FILENAME str "Function that creates final target filename of the file"]
(let [pod (create-pod markdown-deps)
options (merge +markdown-defaults+ *opts*)]
(boot/with-pre-wrap fileset
(let [markdown-files (->> fileset boot/user-files (boot/by-ext ["md" "markdown"]) (map #(.getPath (boot/tmp-file %))))
parsed-metadata (pod/with-call-in @pod
(io.perun.markdown/parse-markdown
~markdown-files
~options))
fs-with-meta (with-meta fileset {:metadata parsed-metadata})]
(u/info "Parsed markdown files\n")
fs-with-meta))))
(def ^:private ttr-deps
'[[time-to-read "0.1.0"]])
(deftask ttr
"Calculate time to read for each file"
[]
(let [pod (create-pod ttr-deps)]
(boot/with-pre-wrap fileset
(let [metadata (:metadata (meta fileset))
updated-metadata (pod/with-call-in @pod
(io.perun.ttr/calculate-ttr ~metadata))
fs-with-meta (with-meta fileset {:metadata updated-metadata})]
fs-with-meta))))
(deftask draft
"Exclude draft files"
[]
(boot/with-pre-wrap fileset
(let [files-metadata (:metadata (meta fileset))
updated-metadata (remove #(true? (:draft %)) files-metadata)
fs-with-meta (with-meta fileset {:metadata updated-metadata})]
(u/info "Remove draft files. Remaining %s files\n" (count updated-metadata))
fs-with-meta)))
(defn- create-filepath [file options]
(let [file-path (str (:target options) "/" (:filename file) "/index.html")]
(assoc file :filepath file-path)))
(deftask permalink
"Make files permalinked"
[]
(let [options (merge +defaults+ *opts*)]
(boot/with-pre-wrap fileset
(let [files-metadata (:metadata (meta fileset))
updated-metadata (map #(create-filepath % options) files-metadata)
fs-with-meta (with-meta fileset {:metadata updated-metadata})]
(u/info "Added permalinks to %s files\n" (count updated-metadata))
fs-with-meta))))
(def ^:private sitemap-deps
'[[sitemap "0.2.4"]])
(def ^:private +sitemap-defaults+
(merge +defaults+
{:filename "sitemap.xml"
:target "public"}))
(deftask sitemap
"Generate sitemap"
[f filename FILENAME str "Generated sitemap filename"
o target OUTDIR str "The output directory"
u url URL str "Base URL"]
(let [pod (create-pod sitemap-deps)
tmp (boot/tmp-dir!)
options (merge +sitemap-defaults+ *opts*)]
(boot/with-pre-wrap fileset
(let [files-metadata (:metadata (meta fileset))]
(pod/with-call-in @pod
(io.perun.sitemap/generate-sitemap
~(.getPath tmp)
~files-metadata
~options))
(commit fileset tmp)))))
(def ^:private rss-deps
'[[clj-rss "0.1.9"]])
(def ^:private +rss-defaults+
(merge +defaults+
{:filename "feed.rss"
:target "public"}))
(deftask rss
"Generate RSS feed"
[f filename FILENAME str "Generated RSS feed filename"
o target OUTDIR str "The output directory"
t title TITLE str "RSS feed title"
p description DESCRIPTION str "RSS feed description"
l link LINK str "RSS feed link"]
(let [pod (create-pod rss-deps)
tmp (boot/tmp-dir!)
options (merge +rss-defaults+ *opts*)]
(boot/with-pre-wrap fileset
(let [files-metadata (:metadata (meta fileset))]
(pod/with-call-in @pod
(io.perun.rss/generate-rss
~(.getPath tmp)
~files-metadata
~options))
(commit fileset tmp)))))
(def ^:private +render-defaults+
(merge +defaults+
{:target "public"}))
(deftask render
"Render pages"
[o target OUTDIR str "The output directory"
r renderer RENDERER code "Page renderer"]
(let [tmp (boot/tmp-dir!)
options (merge +render-defaults+ *opts*)]
(boot/with-pre-wrap fileset
(let [files-metadata (:metadata (meta fileset))]
(doseq [file files-metadata]
(let [render-fn (:renderer options)
html (render-fn file)
page-filepath (str (:target options) "/"
(or (:filepath file)
(str (:filename file) ".html")))]
(perun/create-file tmp page-filepath html)))
(u/info (str "Render all pages\n"))
(commit fileset tmp)))))
(def ^:private +collection-defaults+
(merge +defaults+
{:target "public"
:filterer identity
:sortby (fn [file] (:date_published file))
:comparator (fn [i1 i2] (compare i1 i2))}))
(deftask collection
"Render collection files"
[o target OUTDIR str "The output directory"
r renderer RENDERER code "Page renderer"
f filterer FILTER code "Filter function"
s sortby SORTBY code "Sort by function"
c comparator COMPARATOR code "Sort by comparator function"
p page PAGE str "Collection result page path"]
(let [tmp (boot/tmp-dir!)
options (merge +collection-defaults+ *opts*)]
(boot/with-pre-wrap fileset
(let [files-metadata (:metadata (meta fileset))
filtered-files (filter (:filterer options) files-metadata)
sorted-files (sort-by (:sortby options) (:comparator options) files-metadata)
render-fn (:renderer options)
html (render-fn sorted-files)
page-filepath (str (:target options) "/" page)]
(perun/create-file tmp page-filepath html)
(u/info (str "Render collection " page "\n"))
(commit fileset tmp)))))