/
api.clj
669 lines (601 loc) · 24.1 KB
/
api.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
(ns quickblog.api
{:org.babashka/cli
{:spec
{
;; Blog metadata
:blog-title
{:desc "Title of the blog"
:ref "<title>"
:default "quickblog"
:require true
:group :blog-metadata}
:blog-author
{:desc "Author's name"
:ref "<name>"
:default "Quick Blogger"
:require true
:group :blog-metadata}
:blog-description
{:desc "Blog description for subtitle and RSS feeds"
:ref "<text>"
:default "A blog about blogging quickly"
:require true
:group :blog-metadata}
:blog-root
{:desc "Base URL of the blog"
:ref "<url>"
:default "https://github.com/borkdude/quickblog"
:require true
:group :blog-metadata}
;; Optional metadata
:about-link
{:desc "Link to about the author page"
:ref "<url>"
:group :optional-metadata}
:discuss-link
{:desc "Link to discussion forum for posts"
:ref "<url>"
:group :optional-metadata}
:twitter-handle
{:desc "Author's Twitter handle"
:ref "<handle>"
:group :optional-metadata}
:page-suffix
{:desc "Suffix to use for page links (default .html)"
:ref "<suffix>"
:default ".html"
:group :optional-metadata}
;; Post config
:default-metadata
{:desc "Default metadata to add to posts"
:default {:tags ["clojure"]}
:group :post-config}
:num-index-posts
{:desc "Number of most recent posts to show on the index page"
:ref "<num>"
:default 3
:group :post-config}
:posts-file
{:desc "File containing deprecated post metadata (used only for `migrate`)"
:ref "<file>"
:default "posts.edn"
:group :post-config}
;; Input directories
:assets-dir
{:desc "Directory to copy assets (images, etc.) from"
:ref "<dir>"
:default "assets"
:require true
:group :input-directories}
:posts-dir
{:desc "Directory to read posts from"
:ref "<dir>"
:default "posts"
:require true
:group :input-directories}
:templates-dir
{:desc "Directory to read templates from; see Templates section in README"
:ref "<dir>"
:default "templates"
:require true
:group :input-directories}
;; Output directories
:out-dir
{:desc "Base directory for outputting static site"
:ref "<dir>"
:default "public"
:require true
:group :output-directories}
:assets-out-dir
{:desc "Directory to write assets to (relative to :out-dir)"
:ref "<dir>"
:default "assets"
:require true
:group :output-directories}
:tags-dir
{:desc "Directory to write tags to (relative to :out-dir)"
:ref "<dir>"
:default "tags"
:require true
:group :output-directories}
;; Caching
:force-render
{:desc "If true, pages will be re-rendered regardless of cache status"
:default false
:group :caching}
:cache-dir
{:desc "Directory to use for caching"
:ref "<dir>"
:default ".work"
:require true
:group :caching}
:rendering-system-files
{:desc "Files involved in rendering pages (only set if you know what you're doing!)"
:ref "<file1> <file2>..."
:default ["bb.edn" "deps.edn"]
:coerce []
:require true
:group :caching}
;; Social sharing
:blog-image
{:desc "Blog thumbnail image URL; see Features > Social sharing in README"
:ref "<url>"
:group :social-sharing}
;; Favicon
:favicon
{:desc "If true, favicon will be added to all pages"
:default false
:group :favicon}
:favicon-dir
{:desc "Directory to read favicon assets from"
:ref "<dir>"
:default "assets/favicon"
:group :favicon}
:favicon-out-dir
{:desc "Directory to write favicon assets to (relative to :out-dir)"
:ref "<dir>"
:default "assets/favicon"
:group :favicon}
;; Command-specific opts
}}}
(:require
[babashka.fs :as fs]
[clojure.data.xml :as xml]
[clojure.edn :as edn]
[clojure.set :as set]
[clojure.string :as str]
[quickblog.internal :as lib]
[selmer.parser :as selmer]
[selmer.filters :as filters]))
;; Add filter for tag page links; see:
;; https://github.com/yogthos/Selmer#filters
(filters/add-filter! :escape-tag lib/escape-tag)
(defn- update-out-dirs
[{:keys [out-dir assets-out-dir favicon-out-dir] :as opts}]
(let [out-dir-ify (fn [dir]
(if-not (str/starts-with? (str dir) (str out-dir))
(fs/file out-dir dir)
dir))]
(assoc opts
:assets-out-dir (out-dir-ify assets-out-dir)
:favicon-out-dir (out-dir-ify favicon-out-dir))))
(defn update-cache-dir [opts]
(let [cache-dir (:cache-dir opts)
cache-dir (when cache-dir
(if (:watch opts)
(str (fs/file cache-dir "dev"))
(str (fs/file cache-dir "prod"))))]
(assoc opts :cache-dir cache-dir)))
(defn- update-opts [opts]
(-> opts
(update :rendering-system-files #(map fs/file (cons (:templates-dir opts) %)))
update-out-dirs
update-cache-dir))
(defn- get-defaults [metadata]
(->> (get-in metadata [:org.babashka/cli :spec])
(filter (fn [[_ m]] (contains? m :default)))
(map (fn [[k m]] [k (:default m)]))
(into {})))
(defn- apply-default-opts [opts]
(let [defaults (get-defaults (meta (the-ns 'quickblog.api)))]
(-> (->> defaults
(map (fn [[k default]] [k (if (contains? opts k) (opts k) default)]))
(into {}))
(merge opts)
update-opts)))
(def ^:private favicon-assets
["android-chrome-192x192.png"
"android-chrome-512x512.png"
"apple-touch-icon.png"
"browserconfig.xml"
"favicon-16x16.png"
"favicon-32x32.png"
"favicon.ico"
"mstile-150x150.png"
"safari-pinned-tab.svg"
"site.webmanifest"])
(def ^:private legacy-template "
<html><head>
<meta http-equiv=\"refresh\" content=\"0; URL=/{{new_url}}\" />
</head></html>")
(defn- base-html [opts]
(slurp (lib/ensure-template opts "base.html")))
(defn- ensure-favicon-assets [{:keys [favicon favicon-dir]}]
(when favicon
(doseq [asset favicon-assets]
(lib/ensure-resource (fs/file favicon-dir asset)
(fs/file "assets" "favicon" asset)))))
(defn- gen-posts [{:keys [deleted-posts modified-posts posts
cache-dir out-dir]
:as opts}]
(let [posts-to-write (set/union modified-posts
(lib/modified-post-pages opts))
page-template (base-html opts)
post-template (slurp (lib/ensure-template opts "post.html"))]
(fs/create-dirs cache-dir)
(fs/create-dirs out-dir)
(doseq [[file post] posts
:when (contains? posts-to-write file)
:let [{:keys [file date legacy]} post
html-file (lib/html-file file)]]
(lib/write-post! (assoc opts
:page-template page-template
:post-template post-template)
post)
(let [legacy-dir (fs/file out-dir (str/replace date "-" "/")
(str/replace file ".md" ""))]
(when legacy
(fs/create-dirs legacy-dir)
(let [legacy-file (fs/file (fs/file legacy-dir "index.html"))
redirect-html (selmer/render legacy-template
{:new_url html-file})]
(println "Writing legacy redirect:" (str legacy-file))
(spit legacy-file redirect-html)))))
(doseq [file deleted-posts]
(println "Post deleted; removing from cache and outdir:" (str file))
(fs/delete-if-exists (fs/file cache-dir (lib/cache-file file)))
(fs/delete-if-exists (fs/file out-dir (lib/html-file file))))))
(defn- gen-tags [{:keys [blog-title blog-description
blog-image blog-image-alt twitter-handle
modified-tags posts out-dir tags-dir]
:as opts}]
(let [tags-out-dir (fs/create-dirs (fs/file out-dir tags-dir))
posts-by-tag (lib/posts-by-tag posts)
tags-file (fs/file tags-out-dir "index.html")
template (base-html opts)]
(when (or (seq modified-tags)
(not (fs/exists? tags-file)))
(lib/write-page! opts tags-file template
{:skip-archive true
:title (str blog-title " - Tags")
:relative-path "../"
:body (lib/tag-links "Tags" posts-by-tag opts)
:sharing {:description (format "Tags - %s"
blog-description)
:author twitter-handle
:twitter-handle twitter-handle
:image (lib/blog-link opts blog-image)
:image-alt blog-image-alt
:url (lib/blog-link opts "tags/index.html")}})
(doseq [tag-and-posts posts-by-tag]
(lib/write-tag! opts tags-out-dir template tag-and-posts))
;; Delete tags pages for removed tags
(doseq [tag (remove posts-by-tag modified-tags)
:let [tag-filename (fs/file tags-out-dir (lib/tag-file tag))]]
(println "Deleting removed tag:" (str tag-filename))
(fs/delete-if-exists tag-filename)))))
;;;; Generate index page with last `num-index-posts` posts
(defn- index [{:keys [posts page-suffix] :as opts}]
(let [posts (for [{:keys [file html] :as post} posts
:let [preview (first (str/split @html #"<!-- end-of-preview -->" 2))]]
(assoc post
:post-link (str/replace file ".md" page-suffix)
:body preview
:truncated (not= preview @html)))
index-template (lib/ensure-template opts "index.html")]
(selmer/render (slurp index-template) (merge opts {:posts posts}))))
(defn- spit-index
[{:keys [blog-title blog-description blog-image blog-image-alt twitter-handle
posts cached-posts deleted-posts modified-posts num-index-posts
out-dir]
:as opts}]
(let [index-posts #(->> (vals %)
lib/remove-previews
lib/sort-posts
(take num-index-posts))
posts (index-posts posts)
cached-posts (index-posts cached-posts)
out-file (fs/file out-dir "index.html")
stale? (or (not= (map :file posts)
(map :file cached-posts))
(some modified-posts (map :file posts))
(some deleted-posts (map :file cached-posts))
(not (fs/exists? out-file)))]
(when stale?
(let [body (index (assoc opts :posts posts))]
(lib/write-page! opts out-file
(base-html opts)
{:title blog-title
:body body
:sharing {:description blog-description
:author twitter-handle
:twitter-handle twitter-handle
:image (lib/blog-link opts blog-image)
:image-alt blog-image-alt
:url (lib/blog-link opts "index.html")}})))))
;;;; Generate about page if template exists
(defn- about [{:keys [templates-dir] :as opts}]
(selmer/render (slurp (fs/file templates-dir "about.html")) opts))
(defn- spit-about [{:keys [blog-title blog-description
blog-image blog-image-alt twitter-handle
modified-metadata out-dir]
:as opts}]
(let [out-file (fs/file out-dir "about.html")
stale? (or (some not-empty (vals modified-metadata))
(not (fs/exists? out-file)))]
(when stale?
(let [title (str blog-title " - About")]
(lib/write-page! opts out-file
(base-html opts)
{:skip-archive true
:title title
:body (about opts)
:sharing {:description (format "About - %s"
blog-description)
:author twitter-handle
:twitter-handle twitter-handle
:image (lib/blog-link opts blog-image)
:image-alt blog-image-alt
:url (lib/blog-link opts "about.html")}})))))
;;;; Generate archive page with links to all posts
(defn- spit-archive [{:keys [blog-title blog-description
blog-image blog-image-alt twitter-handle
modified-metadata posts out-dir]
:as opts}]
(let [out-file (fs/file out-dir "archive.html")
stale? (or (some not-empty (vals modified-metadata))
(not (fs/exists? out-file)))]
(when stale?
(let [title (str blog-title " - Archive")
posts (lib/sort-posts (vals posts))]
(lib/write-page! opts out-file
(base-html opts)
{:skip-archive true
:title title
:body (lib/archive-links "Archive" posts opts)
:sharing {:description (format "Archive - %s"
blog-description)
:author twitter-handle
:twitter-handle twitter-handle
:image (lib/blog-link opts blog-image)
:image-alt blog-image-alt
:url (lib/blog-link opts "archive.html")}})))))
;;;; Generate atom feeds
(xml/alias-uri 'atom "http://www.w3.org/2005/Atom")
(import java.time.format.DateTimeFormatter)
(defn- rfc-3339-now []
(let [fmt (DateTimeFormatter/ofPattern "yyyy-MM-dd'T'HH:mm:ssxxx")
now (java.time.ZonedDateTime/now java.time.ZoneOffset/UTC)]
(.format now fmt)))
(defn- rfc-3339 [yyyy-MM-dd]
(let [in-fmt (DateTimeFormatter/ofPattern "yyyy-MM-dd")
local-date (java.time.LocalDate/parse yyyy-MM-dd in-fmt)
fmt (DateTimeFormatter/ofPattern "yyyy-MM-dd'T'HH:mm:ssxxx")
now (java.time.ZonedDateTime/of (.atTime local-date 23 59 59) java.time.ZoneOffset/UTC)]
(.format now fmt)))
(defn- atom-feed
;; validate at https://validator.w3.org/feed/check.cgi
[{:keys [blog-title blog-author blog-root page-suffix] :as opts} posts]
(-> (xml/sexp-as-element
[::atom/feed
{:xmlns "http://www.w3.org/2005/Atom"}
[::atom/title blog-title]
[::atom/link {:href (lib/blog-link opts "atom.xml") :rel "self"}]
[::atom/link {:href blog-root}]
[::atom/updated (rfc-3339-now)]
[::atom/id blog-root]
[::atom/author
[::atom/name blog-author]]
(for [{:keys [title date file preview html]} posts
:when (not preview)
:let [html-file (str/replace file ".md" page-suffix)
link (lib/blog-link opts html-file)]]
[::atom/entry
[::atom/id link]
[::atom/link {:href link}]
[::atom/title title]
[::atom/updated (rfc-3339 date)]
[::atom/content {:type "html"}
[:-cdata @html]]])])
xml/indent-str))
(defn- clojure-post? [{:keys [tags]}]
(let [clojure-tags #{"clojure" "clojurescript"}
lowercase-tags (map str/lower-case tags)]
(some clojure-tags lowercase-tags)))
(defn- spit-feeds [{:keys [out-dir modified-posts posts] :as opts}]
(let [feed-file (fs/file out-dir "atom.xml")
clojure-feed-file (fs/file out-dir "planetclojure.xml")
all-posts (lib/sort-posts (vals posts))
clojure-posts (->> (vals posts)
(filter clojure-post?)
lib/sort-posts)
clojure-posts-modified? (->> modified-posts
(map posts)
(some clojure-post?))]
(if (and (not clojure-posts-modified?) (fs/exists? clojure-feed-file))
(println "No Clojure posts modified; skipping Clojure feed")
(do
(println "Writing Clojure feed" (str clojure-feed-file))
(spit clojure-feed-file (atom-feed opts clojure-posts))))
(if (and (empty? modified-posts) (fs/exists? feed-file))
(println "No posts modified; skipping main feed")
(do
(println "Writing feed" (str feed-file))
(spit feed-file (atom-feed opts all-posts))))))
(defn render
"Renders posts declared in `posts.edn` to `out-dir`."
[opts]
(let [{:keys [assets-dir
assets-out-dir
cache-dir
favicon-dir
favicon-out-dir
out-dir
posts-file
templates-dir]
:as opts}
(-> opts apply-default-opts lib/refresh-cache)]
(if (empty? (:posts opts))
(binding [*out* *err*]
(println
(if (fs/exists? posts-file)
(format "Run `bb migrate` to move metadata from `%s` to post files"
posts-file)
"No posts found; run `bb new` to create one")))
(do
(lib/ensure-template opts "style.css")
(ensure-favicon-assets opts)
(when (fs/exists? assets-dir)
(lib/copy-tree-modified assets-dir assets-out-dir))
(when (fs/exists? favicon-dir)
(lib/copy-tree-modified favicon-dir favicon-out-dir))
(doseq [file (fs/glob templates-dir "*.{css,svg}")]
(lib/copy-modified file (fs/file out-dir (.getFileName file))))
(fs/create-dirs (fs/file cache-dir))
(gen-posts opts)
(gen-tags opts)
(spit-archive opts)
(when (fs/exists? (fs/file templates-dir "about.html"))
(spit-about opts))
(spit-index opts)
(spit-feeds opts)
(lib/write-cache! opts)))
opts))
(defn quickblog
"Alias for `render`"
[opts]
(render opts))
(defn- now []
(.format (java.time.LocalDate/now)
(java.time.format.DateTimeFormatter/ofPattern "yyyy-MM-dd")))
(defn new
"Creates new `file` in posts dir."
{:org.babashka/cli
{:spec
{:file
{:desc "Filename of post (relative to posts-dir)"
:ref "<filename>"
:require true}
:preview
{:desc "Create post as preview (won't be published to index, tags, or feeds)"
:default false}
:title
{:desc "Title of post"
:ref "<title>"
:require true}
:tags
{:desc "List of tags (default: 'clojure'; example: --tags tag1 tag2 \"tag3 has spaces\")"
:ref "<tags>"
:coerce []}}}}
[opts]
(let [{:keys [file preview title posts-dir tags default-metadata]
:as opts} (apply-default-opts opts)
tags (cond (empty? tags) (:tags default-metadata)
(= tags [true]) [] ;; `--tags` without arguments
:else tags)]
(doseq [k [:file :title]]
(assert (contains? opts k) (format "Missing required option: %s" k)))
(let [file (if (re-matches #"^.+[.][^.]+$" file)
file
(str file ".md"))
post-file (fs/file posts-dir file)
preview-str (if preview "Preview: true\n" "")]
(when-not (fs/exists? post-file)
(fs/create-dirs posts-dir)
(spit (fs/file posts-dir file)
(format "Title: %s\nDate: %s\nTags: %s\n%s\nWrite a blog post here!"
title (now) (str/join "," tags) preview-str))))))
(defn clean
"Removes cache and output directories"
[opts]
(let [{:keys [cache-dir out-dir]} (apply-default-opts opts)]
(doseq [dir [cache-dir out-dir]]
(println "Removing dir:" dir)
(fs/delete-tree dir))))
(defn migrate
"Migrates from `posts.edn` to post-local metadata"
[opts]
(let [{:keys [posts-file] :as opts} (apply-default-opts opts)]
(if (fs/exists? posts-file)
(do
(doseq [post (->> (slurp posts-file) (format "[%s]") edn/read-string)]
(lib/migrate-post opts post))
(println "If all posts were successfully migrated, you should now delete"
(str posts-file)))
(println (format "Posts file %s does not exist; no posts to migrate"
(str posts-file))))))
(defn refresh-templates
"Updates to latest default templates"
[opts]
(lib/refresh-templates (apply-default-opts opts)))
(defn serve
"Runs file-server on `port`."
{:org.babashka/cli
{:spec
{:port
{:desc "Port for HTTP server to listen on"
:ref "<port>"
:default 1888}}}}
([opts] (serve opts true))
([opts block?]
(let [{:keys [port out-dir]} (merge (get-defaults (meta #'serve))
(apply-default-opts opts))
serve (requiring-resolve 'babashka.http-server/serve)]
(serve {:port port
:dir out-dir})
(when block? @(promise)))))
(def ^:private posts-cache (atom nil))
(defn watch
"Watches posts, templates, and assets for changes. Runs file server using
`serve`."
{:org.babashka/cli
{:spec
{:port
{:desc "Port for HTTP server to listen on"
:ref "<port>"
:default 1888}}}}
[opts]
(let [{:keys [assets-dir assets-out-dir posts-dir templates-dir]
:as opts}
(-> opts
apply-default-opts
(assoc :watch (format "<script type=\"text/javascript\" src=\"%s\"></script>"
lib/live-reload-script))
render)]
(reset! posts-cache (:posts opts))
(serve opts false)
(let [load-pod (requiring-resolve 'babashka.pods/load-pod)]
(load-pod 'org.babashka/fswatcher "0.0.3")
(let [watch (requiring-resolve 'pod.babashka.fswatcher/watch)]
(watch posts-dir
(fn [{:keys [path type]}]
(println "Change detected:" (name type) (str path))
(when (#{:create :remove :rename :write :write|chmod} type)
(let [post-filename (-> (fs/file path) fs/file-name)]
;; skip Emacs backup files and the like
(when-not (str/starts-with? post-filename ".")
(println "Re-rendering" post-filename)
(let [post (lib/load-post opts path)
posts (cond
(contains? #{:remove :rename} type)
(dissoc @posts-cache post-filename)
(:quickblog/error post)
(do
(println (:quickblog/error post))
(dissoc @posts-cache post-filename))
:else
(assoc @posts-cache post-filename post))
opts (-> opts
(assoc :cached-posts @posts-cache
:posts posts)
render)]
(reset! posts-cache (:posts opts))))))))
(watch templates-dir
(fn [{:keys [path type]}]
(println "Template change detected; re-rendering all posts:"
(name type) (str path))
(let [opts (-> opts
(dissoc :cached-posts :posts)
render)]
(reset! posts-cache (:posts opts)))))
(when (fs/exists? assets-dir)
(watch assets-dir
(fn [{:keys [path type]}]
(println "Asset change detected:"
(name type) (str path))
(when (contains? #{:remove :rename} type)
(let [file (fs/file assets-out-dir (fs/file-name path))]
(println "Removing deleted asset:" (str file))
(fs/delete-if-exists file)))
(lib/copy-tree-modified assets-dir assets-out-dir)))))))
@(promise))