-
Notifications
You must be signed in to change notification settings - Fork 38
/
perun.clj
1325 lines (1198 loc) · 58.3 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
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(ns io.perun
{:boot/export-tasks true}
(:require [boot.core :as boot :refer [deftask]]
[boot.pod :as pod]
[boot.util :as u]
[clojure.data :as data]
[clojure.java.io :as io]
[clojure.set :as set]
[clojure.string :as string]
[io.perun.core :as perun]
[io.perun.meta :as pm]))
(def ^:private ^:deps global-deps
'[])
(defn- create-pod' [deps]
(-> (boot/get-env)
(update-in [:dependencies] into global-deps)
(update-in [:dependencies] into deps)
pod/make-pod))
(defn- create-pod
[deps]
(future (create-pod' deps)))
(defn- commit [fileset tmp]
(-> fileset
(boot/add-resource tmp)
boot/commit!))
(defn tmp-by-ext
"Returns boot tmpfiles from `fileset` that end with `extensions`.
If `extensions` is empty, returns all files."
[fileset extensions]
(cond->> (boot/ls fileset)
(> (count extensions) 0) (boot/by-ext extensions)))
(defn meta-by-ext
"Returns perun metadata from `fileset`, filtered by `extensions`.
If `extensions` is empty, returns metadata for all files."
[fileset extensions]
(map (partial pm/meta-from-file fileset) (tmp-by-ext fileset extensions)))
(defn filter-tmp-by-ext
"Returns boot tmpfiles from `fileset`. `options` selects files
that end with values in the `:extensions` key, filtered by the
`:filterer` predicate. If `:extensions` is empty, returns all files."
[fileset {:keys [filterer extensions]}]
(filter (comp filterer (partial pm/meta-from-file fileset))
(tmp-by-ext fileset extensions)))
(defn filter-meta-by-ext
"Returns perun metadata from `fileset`. `options` selects files
that end with values in the `:extensions` key, filtered by the
`:filterer` predicate. If `:extensions` is empty, returns
metadata for all files."
[fileset {:keys [filterer extensions]}]
(filter filterer (meta-by-ext fileset extensions)))
(def ^:private ^:deps print-meta-deps
'[[mvxcvi/puget "1.2.0"]])
(def print-meta-pod (delay (create-pod' print-meta-deps)))
(def ^:private +print-meta-defaults+
{:map-fn identity
:filterer identity
:extensions []
:content-exts #{}})
(deftask print-meta
"Utility task to print perun metadata"
[m map-fn MAPFN code "function to map over metadata items before printing (default: `identity`)"
_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to include (default: `[]`, aka, all extensions)"
b content-exts CONTENTEXTS #{str} "print content for these extensions, default `#{}`"]
(boot/with-pass-thru fileset
(let [{:keys [content-exts] :as options} (merge +print-meta-defaults+ *opts*)
entries (doall (map (:map-fn options) (filter-meta-by-ext fileset options)))]
(pod/with-call-in @print-meta-pod
(io.perun.print-meta/print-meta ~entries ~content-exts)))))
(defn trace
"Helper function, conj `kw` onto the `:io.perun/trace` metadata
key of each entry in `entries`"
[kw entries]
(map #(update-in % [:io.perun/trace] (fnil conj []) kw) entries))
(deftask base
"Deprecated - metadata based on a files' path is now automatically set when other tasks
access metadata"
[]
(boot/with-pass-thru _
(u/warn (str "The `base` task is deprecated. Metadata based on a files' path is now "
"automatically set when other tasks access metadata\n"))))
(def ^:private ^:deps mime-type-deps
'[[com.novemberain/pantomime "2.11.0"]])
(def ^:private +mime-type-defaults+
{:filterer identity
:extensions []})
(deftask mime-type
"Adds `:mime-type` and `:file-type` keys to each file's metadata"
[_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to include (default: `[]`, aka, all extensions)"]
(let [pod (create-pod mime-type-deps)
options (merge +mime-type-defaults+ *opts*)]
(boot/with-pre-wrap fileset
(let [metas (trace :io.perun/mime-type (filter-meta-by-ext fileset options))
updated-metas (pod/with-call-in @pod (io.perun.mime-type/mime-type ~metas))]
(perun/report-info "mime-type" "set `:mime-type` and `:file-type` on %s files" (count updated-metas))
(pm/set-meta fileset updated-metas)))))
(def ^:private ^:deps images-dimensions-deps
'[[image-resizer "0.1.10"]])
(deftask images-dimensions
"Add images' dimensions to the file metadata:
- width
- height"
[]
;; This prevents a Java icon appearing in the dock on a Mac, and stealing program focus
(System/setProperty "java.awt.headless" "true")
(boot/with-pre-wrap fileset
(let [pod (create-pod images-dimensions-deps)
metas (trace :io.perun/images-dimensions
(meta-by-ext fileset [".png" ".jpeg" ".jpg"]))
updated-metas (pod/with-call-in @pod
(io.perun.contrib.images-dimensions/images-dimensions ~metas {}))]
(pm/set-meta fileset updated-metas))))
(defn apply-out-dir
[path old-out-dir new-out-dir]
(let [path-args (if (= old-out-dir new-out-dir)
[path]
[new-out-dir path])]
(apply perun/create-filepath path-args)))
(defn render-in-pod
"Renders paths in `inputs`, using `render-form-fn` in `pod`
`inputs` should be a map with keys that are fileset paths, and
values that are the map argument that `render-form-fn` will be called with.
Rendered results will be returned, with their `:path`s and `:canonical-url`s
(if there is a valid `:base-url` in global metadata) set, and `tracer`
added to `io.perun/trace`."
[{:keys [task-name inputs tracer pod global-meta render-form-fn]}]
(trace tracer
(for [[path input] inputs]
(let [[f :as render-form] (render-form-fn input)
render-ns (symbol (namespace f))]
(perun/report-debug task-name "rendered page for path" path)
(pod/with-eval-in @pod (require '~render-ns))
(merge (pod/with-call-in @pod ~render-form)
(pm/path-meta path global-meta))))))
(defn diff-inputs-meta
"Returns a map containing the keys and values of `after` that do not exist
or are different in `before`"
[before after]
(let [[x y z] (map (comp set keys) (data/diff before after))]
(select-keys after (set/union y (set/intersection x z)))))
(defn diff-inputs-content
"Returns a map containing the subset of `path-inputs` for which input file
content has changed from `before-fs` to `after-fs`"
[before-fs after-fs path-inputs]
(let [changed-paths (-> (boot/fileset-diff before-fs after-fs :hash)
:tree keys set)]
(->> path-inputs
(filter (fn [[_ input]]
(pos? (count (set/intersection
(:input-paths input)
changed-paths)))))
(into {}))))
(defn get-passthru-meta
"Only relevant in cases where a task's output does not depend on metadata;
takes care of copying possibly-changed incoming metadata, so that actual
parsing can be bypassed when file content is unchanged."
[{:keys [passthru-fn copy-inputs copy-files prev-path-inputs tracer global-meta]}]
(when passthru-fn
(let [copy-meta (into {} (map (juxt :path pm/+meta-key+) (vals copy-files)))
passthru-meta (->> (passthru-fn copy-inputs tracer global-meta)
(map (juxt :path identity))
(into {}))
prev-passthru-meta (->>
(-> prev-path-inputs
(select-keys (keys copy-inputs))
(passthru-fn tracer global-meta))
(map (juxt :path identity))
(into {}))]
(reduce (fn [result [path m]]
(conj result
(merge
(apply dissoc m (keys (prev-passthru-meta path)))
(passthru-meta path))))
[]
copy-meta))))
(def ^:private ^:deps content-deps
'[[org.clojure/tools.namespace "0.3.1"]])
(defn content-task
"Wrapper for input parsing tasks. Calls the return from `render-form-fn` on
new or changed inputs returned from `paths-fn`, adds `tracer` to
`:io.perun/trace` and writes files for subsequent tasks to process, if
desired. Pass `pod` if one is needed for parsing. In cases where parsing only
depends on the content of its input files rather than their metadata, set
`passthru-fn` to handle setting changed metadata on files copied from the
previous fileset. If input files should be removed from the fileset, set
`rm-originals` to `true`."
[{:keys [task-name render-form-fn paths-fn passthru-fn tracer pod tmp rm-originals]}]
(let [prev (atom {})
tmp (or tmp (boot/tmp-dir!))
pod (or pod (create-pod content-deps))]
(fn [next-task]
(fn [fileset]
(let [refreshed (pod/with-call-in @pod (io.perun.render/update!))
path-inputs (paths-fn fileset)
uses-meta (not passthru-fn)
render-inputs (cond
refreshed path-inputs
uses-meta (diff-inputs-meta (:path-inputs @prev)
path-inputs)
:else (diff-inputs-content (:input-fs @prev)
fileset
path-inputs))
global-meta (pm/get-global-meta fileset)
output-meta (render-in-pod {:task-name task-name
:inputs render-inputs
:tracer tracer
:pod pod
:global-meta global-meta
:render-form-fn render-form-fn})
_ (doseq [{:keys [path rendered]} output-meta]
(when rendered
(perun/report-debug task-name "writing page for path" path)
(perun/create-file tmp path rendered)))
rm-files (if rm-originals
(->> (vals path-inputs)
(keep :input-paths)
(apply set/union)
(keep #(boot/tmp-get fileset %)))
[])
copy-inputs (apply dissoc path-inputs (keys render-inputs))
copy-files (select-keys (:tree (:output-fs @prev)) (keys copy-inputs))
passthru-meta (get-passthru-meta {:passthru-fn passthru-fn
:copy-inputs copy-inputs
:copy-files copy-files
:prev-path-inputs (:path-inputs @prev)
:tracer tracer
:global-meta global-meta})
final-meta (into (map #(dissoc % :rendered :original) output-meta) passthru-meta)
output-fs (-> fileset
(boot/rm rm-files)
(update-in [:tree] merge copy-files)
(commit tmp)
(pm/set-meta final-meta))]
(reset! prev {:input-fs fileset :output-fs output-fs :path-inputs path-inputs})
(perun/report-debug task-name "removed files" rm-files)
(case (count render-inputs)
0 nil
1 (perun/report-info task-name "rendered new or changed file %s" (first (keys render-inputs)))
(perun/report-info task-name "rendered %s new or changed files" (count render-inputs)))
(case (count copy-files)
0 nil
1 (perun/report-info task-name "copied unchanged file %s" (first (keys copy-files)))
(perun/report-info task-name "copied %s unchanged file(s)" (count copy-files)))
(boot/empty-dir! tmp)
(let [result-fs (next-task output-fs)]
(pod/with-call-in @pod (io.perun.render/reset-refreshed!))
result-fs))))))
(defn content-paths
"Returns a map of path -> parser input for basic content tasks"
[fileset {:keys [out-dir extensions out-ext meta] :as options}]
(let [global-meta (pm/get-global-meta fileset)]
(reduce (fn [result {:keys [path] :as entry}]
(let [ext-pattern (re-pattern (str "(" (string/join "|" extensions) ")$"))
ext-path (if out-ext
(string/replace path ext-pattern out-ext)
path)
new-path (apply-out-dir ext-path (:out-dir entry) out-dir)
path-meta (pm/path-meta path
global-meta
(boot/tmp-file (boot/tmp-get fileset path)))]
(assoc result
new-path {:entry (merge {:original-path path}
entry
meta
(when out-dir
{:out-dir out-dir})
path-meta)
:input-paths #{path}})))
{}
(filter-meta-by-ext fileset options))))
(defn content-passthru
"Returns a map of path -> simulated parser output for basic content tasks;
only used when file content hasn't changed, and so parsing can be skipped"
[inputs tracer global-meta]
(trace tracer
(for [[path {:keys [entry]}] inputs]
(merge entry (pm/path-meta path global-meta)))))
(defn resize-paths
"Returns a map of path -> input for images-resize"
[fileset {:keys [out-dir parent-path meta resolutions] :as options} tmp-dir]
(let [global-meta (pm/get-global-meta fileset)
files (boot/ls fileset)]
(reduce
(fn [result {:keys [slug path extension] :as entry}]
(reduce
(fn [result* resolution]
(let [new-filename (str slug "_" resolution "." extension)
new-path (-> (perun/create-filepath parent-path new-filename)
(apply-out-dir (:out-dir entry) out-dir))
input-file (first (boot/by-path [path] files))
img-meta (assoc (pm/path-meta new-path global-meta)
:resolution resolution
:input-paths #{path}
:input-meta (merge (pm/meta-from-file fileset input-file)
(select-keys input-file [:hash]))
:tmp-dir tmp-dir)]
(assoc result*
new-path (merge entry
meta
(when out-dir
{:out-dir out-dir})
img-meta))))
result
resolutions))
{}
(filter-meta-by-ext fileset options))))
(def ^:private ^:deps images-resize-deps
'[[org.clojure/tools.namespace "0.3.1"]
[image-resizer "0.1.10"]])
(def ^:private +images-resize-defaults+
{:out-dir "public"
:resolutions #{3840 2560 1920 1280 1024 640}
:filterer identity
:extensions [".png" ".jpeg" ".jpg"]})
(deftask images-resize
"Resize images to the provided resolutions.
Each image file would have resolution appended to it's name:
e.x. san-francisco.jpg would become san-francisco_3840.jpg"
[o out-dir OUTDIR str "the output directory"
r resolutions RESOLUTIONS #{int} "resolutions to which images should be resized"
_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to include (default: `[]`, aka, all extensions)"
m meta META edn "metadata to set on each entry"]
;; This prevents a Java icon appearing in the dock on a Mac, and stealing program focus
(System/setProperty "java.awt.headless" "true")
(let [pod (create-pod images-resize-deps)
tmp (boot/tmp-dir!)
options (merge +images-resize-defaults+ *opts*)]
(content-task
{:render-form-fn (fn [data] `(io.perun.contrib.images-resize/image-resize ~data))
:paths-fn #(resize-paths % options (.getPath tmp))
:passthru-fn content-passthru
:task-name "images-resize"
:tracer :io.perun/images-resize
:pod pod
:tmp tmp})))
(def ^:private ^:deps yaml-metadata-deps
'[[org.clojure/tools.namespace "0.3.1"]
[circleci/clj-yaml "0.6.0"]])
(def ^:private +yaml-metadata-defaults+
{:filterer identity
:extensions []})
(deftask yaml-metadata
"Parse YAML metadata at the beginning of files
This task is primarily intended for composing with other tasks.
It will extract and parse any YAML data from the beginning of
a file, and then overwrite that file with the YAML removed, and
with the parsed data added as perun metadata."
[_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to include (default: `[]`, aka, all extensions)"
r keep-yaml bool "if `true`, remove the yaml header from files"]
(let [pod (create-pod yaml-metadata-deps)
options (merge +yaml-metadata-defaults+ *opts*)]
(content-task
{:render-form-fn (fn [data] `(io.perun.yaml/parse-yaml ~data ~keep-yaml))
:paths-fn #(content-paths % options)
:passthru-fn content-passthru
:task-name "yaml-metadata"
:tracer :io.perun/yaml-metadata
:pod pod})))
(def ^:private ^:deps markdown-deps
'[[org.clojure/tools.namespace "0.3.1"]
[com.vladsch.flexmark/flexmark "0.40.16"]
[com.vladsch.flexmark/flexmark-profile-pegdown "0.40.16"]])
(def ^:private +markdown-defaults+
{:out-dir "public"
:out-ext ".html"
:filterer identity
:meta {:original true
:include-rss true
:include-atom true}})
(deftask markdown*
"Parse markdown files
This task will look for files ending with `md` or `markdown`
and writes an HTML file that contains the result from
processing the markdown file's content. It will _not_ parse
YAML metadata at the head of the file."
[d out-dir OUTDIR str "the output directory"
x out-ext OUTEXT str "the output extension"
_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
m meta META edn "metadata to set on each entry; keys here will be overridden by metadata in each file"
e md-exts MDEXTS edn "parsing extensions to be used by the markdown parser"]
(let [pod (create-pod markdown-deps)
options (merge +markdown-defaults+ *opts*)]
(content-task
{:render-form-fn (fn [data] `(io.perun.markdown/process-markdown ~data ~md-exts))
:paths-fn #(content-paths % (assoc options :extensions [".md" ".markdown"]))
:passthru-fn content-passthru
:task-name "markdown"
:tracer :io.perun/markdown
:rm-originals true
:pod pod})))
(deftask markdown
"Parse markdown files with yaml front matter
This task will look for files ending with `md` or `markdown`
and writes an HTML file that contains the result from
processing the markdown file's content. It will parse YAML
metadata at the head of the file, and add any data found to
the output's metadata."
[d out-dir OUTDIR str "the output directory"
_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
m meta META edn "metadata to set on each entry; keys here will be overridden by metadata in each file"
e md-exts MDEXTS edn "parsing extensions to be used by the markdown parser"]
(let [{:keys [out-dir filterer meta md-exts]} (merge +markdown-defaults+ *opts*)]
(comp (yaml-metadata :filterer filterer :extensions [".md" ".markdown"])
(markdown* :out-dir out-dir :filterer filterer :meta meta :md-exts md-exts))))
(def ^:private +pandoc-defaults+
{:out-dir "public"
:out-ext ".html"
:filterer identity
:cmd-opts ["-f" "markdown" "-t" "html5"] ;; convert markdown to html5
:extensions [".md" ".markdown"]
:keep-yaml false})
(deftask pandoc*
"Parse files with pandoc
`pandoc` must be installed and on your PATH. YAML metadata at the
head of files will only be available to `pandoc`, and will not be
added to Perun metadata. By default, looks for markdown files and
parses them into HTML."
[d out-dir OUTDIR str "the output directory"
x out-ext OUTEXT str "extension output"
_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to process"
m meta META edn "metadata to set on each entry"
o cmd-opts CMDOPTS [str] "command line options to send to pandoc"]
(let [options (merge +pandoc-defaults+ *opts*)]
(content-task
{:render-form-fn (fn [data] `(io.perun.pandoc/process-pandoc ~data ~cmd-opts))
:paths-fn #(content-paths % options)
:passthru-fn content-passthru
:task-name "pandoc"
:tracer :io.perun/pandoc
:rm-originals true})))
(deftask pandoc
"Parse files with pandoc
`pandoc` must be installed and on your PATH. YAML metadata at the
head of files will be added to Perun metadata, as well as available
to `pandoc`. By default, looks for markdown files and parses them
into HTML."
[d out-dir OUTDIR str "the output directory"
x out-ext OUTEXT str "extension output"
_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to process"
m meta META edn "metadata to set on each entry"
o cmd-opts CMDOPTS [str] "command line options to send to pandoc"
r keep-yaml bool "if `true`, remove the yaml header from files"]
(let [{:keys [out-dir out-ext filterer
extensions meta cmd-opts]} (merge +pandoc-defaults+ *opts*)]
(comp (yaml-metadata :filterer filterer :extensions extensions :keep-yaml keep-yaml)
(pandoc* :out-dir out-dir
:out-ext out-ext
:filterer filterer
:extensions extensions
:meta meta
:cmd-opts cmd-opts))))
(def ^:private ^:deps asciidoctor-deps
'[[org.clojure/tools.namespace "0.3.1"]
[org.asciidoctor/asciidoctorj "2.0.0"]])
(def ^:private +asciidoctor-defaults+
{:out-dir "public"
:out-ext ".html"
:filterer identity
:extensions [".ad" ".asc" ".adoc" ".asciidoc"]
:meta {:original true
:include-rss true
:include-atom true}})
(deftask asciidoctor*
"Parse asciidoc files using Asciidoctor
Asciidoctor has basic support for markdown, and can therefore also be used
for parsing `.md` files."
[d out-dir OUTDIR str "the output directory"
_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to process"
m meta META edn "metadata to set on each entry"]
(let [pod (create-pod asciidoctor-deps)
options (merge +asciidoctor-defaults+ *opts*)]
(content-task
{:render-form-fn (fn [data] `(io.perun.asciidoctor/process-asciidoctor ~data))
:paths-fn #(content-paths % options)
:passthru-fn content-passthru
:task-name "asciidoctor"
:tracer :io.perun/asciidoctor
:rm-originals true
:pod pod})))
(deftask asciidoctor
"Parse asciidoc files with yaml front matter using Asciidoctor
Asciidoctor has basic support for markdown, and can therefore also be used
for parsing `.md` files."
[d out-dir OUTDIR str "the output directory"
_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to process"
m meta META edn "metadata to set on each entry"]
(let [{:keys [out-dir filterer extensions meta]} (merge +asciidoctor-defaults+ *opts*)]
(comp (yaml-metadata :filterer filterer :extensions extensions)
(asciidoctor* :out-dir out-dir
:filterer filterer
:extensions extensions
:meta meta))))
(def ^:private ^:deps highlight-deps
'[[org.clojure/tools.namespace "0.3.1"]
[enlive "1.1.6"]
[clygments "1.0.0"]])
(def ^:private +highlight-defaults+
{:filterer identity
:class "highlight"
:extensions [".html"]})
(deftask highlight
"Syntax highlighting for code blocks using Pygments.
Pygments CSS styles must be provided, see https://github.com/richleland/pygments-css."
[_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to process"
c class CLASS str "CSS class to wrap code blocks with (default: `highlight`)"]
(let [pod (create-pod highlight-deps)
options (merge +highlight-defaults+ *opts*)]
(content-task
{:render-form-fn (fn [data] `(io.perun.highlight/highlight-code-blocks ~data ~(:class options)))
:paths-fn #(content-paths % options)
:passthru-fn content-passthru
:task-name "highlight"
:tracer :io.perun/highlight
:rm-originals true
:pod pod})))
(deftask global-metadata
"Read global metadata from `perun.base.edn` or configured file.
The global metadata will be attached to fileset where it can be
read and manipulated by the tasks. Render tasks will pass this
as the first argument to render functions."
[n filename NAME str "filename to read global metadata from"]
(boot/with-pre-wrap fileset
(let [meta-file (or filename "perun.base.edn")
global-meta (some->> fileset
boot/ls
(boot/by-name [meta-file])
first
boot/tmp-file
slurp
read-string)]
(perun/report-info "global-metadata" "read global metadata from %s" meta-file)
(pm/set-global-meta fileset global-meta))))
(def ^:private ^:deps ttr-deps
'[[time-to-read "0.1.0"]])
(def ^:private +ttr-defaults+
{:filterer identity
:extensions [".html"]})
(deftask ttr
"Calculate time to read for each file. Add `:ttr` key to the files' meta"
[_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to include"]
(let [pod (create-pod ttr-deps)
options (merge +ttr-defaults+ *opts*)]
(boot/with-pre-wrap fileset
(let [meta-contents (->> (filter-tmp-by-ext fileset options)
(map (juxt (partial pm/meta-from-file fileset)
(comp slurp boot/tmp-file))))
updated-metas (trace :io.perun/ttr
(pod/with-call-in @pod
(io.perun.ttr/calculate-ttr ~meta-contents)))]
(perun/report-debug "ttr" "generated time-to-read" (map :ttr updated-metas))
(pm/set-meta fileset updated-metas)))))
(def ^:private ^:deps word-count-deps
'[[org.clojure/tools.namespace "0.3.1"]
[org.apache.lucene/lucene-analyzers-common "8.4.1"]])
(def ^:private +word-count-defaults+
{:filterer identity
:extensions [".html"]})
(deftask word-count
"Count words in each file. Add `:word-count` key to the files' meta"
[_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to include"]
(let [pod (create-pod word-count-deps)
options (merge +word-count-defaults+ *opts*)]
(content-task
{:render-form-fn (fn [data] `(io.perun.word-count/word-count ~data))
:paths-fn #(content-paths % options)
:passthru-fn content-passthru
:task-name "word-count"
:tracer :io.perun/word-count
:pod pod})))
(def ^:private ^:deps gravatar-deps
'[[gravatar "1.1.1"]])
(def ^:private +gravatar-defaults+
{:filterer identity
:extensions [".html"]})
(deftask gravatar
"Find gravatar urls using emails"
[s source-key SOURCE-PROP kw "email property used to lookup gravatar url"
t target-key TARGET-PROP kw "property name to store gravatar url"
_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to include"]
(let [pod (create-pod gravatar-deps)
options (merge +gravatar-defaults+ *opts*)]
(boot/with-pre-wrap fileset
(let [metas (filter-meta-by-ext fileset options)
updated-metas (trace :io.perun/gravatar
(pod/with-call-in @pod
(io.perun.gravatar/find-gravatar ~metas ~source-key ~target-key)))]
(perun/report-debug "gravatar" "found gravatars" (map target-key updated-metas))
(pm/set-meta fileset updated-metas)))))
;; Should be handled by more generic filterer options to other tasks
(deftask draft
"Exclude draft files"
[]
(boot/with-pre-wrap fileset
(let [meta-fn (partial pm/meta-from-file fileset)
draft-files (filter #(-> % meta-fn :draft) (boot/ls fileset))]
(perun/report-info "draft" "removed %s draft files" (count draft-files))
(boot/rm fileset draft-files))))
(def ^:private +build-date-defaults+
{:filterer identity
:extensions [".html"]})
(deftask build-date
"Add :date-build attribute to each file metadata and also to the global meta"
[_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to include"]
(boot/with-pre-wrap fileset
(let [options (merge +build-date-defaults+ *opts*)
global-meta (pm/get-global-meta fileset)
now (java.util.Date.)
updated-metas (->> (filter-meta-by-ext fileset options)
(map #(assoc % :date-build now))
(trace :io.perun/build-date))
new-global-meta (assoc global-meta :date-build now)
updated-fs (pm/set-meta fileset updated-metas)]
(perun/report-debug "build-date" "added :date-build" (map :date-build updated-metas))
(perun/report-info "build-date" "added date-build to %s files" (count updated-metas))
(pm/set-global-meta updated-fs new-global-meta))))
(defn mv-pre-wrap
"Abstraction for tasks that move files in the fileset"
[{:keys [task-name path-fn tracer options]}]
(boot/with-pre-wrap fileset
(let [global-meta (pm/get-global-meta fileset)
metas (filter-meta-by-ext fileset options)
new-fs (reduce #(let [old-path (:path %2)
new-path (path-fn global-meta %2)]
(perun/report-debug task-name "Moved" [old-path new-path])
(-> %1
(boot/mv old-path new-path)
(pm/set-meta (trace tracer [(assoc %2 :path new-path)]))))
fileset
metas)]
(perun/report-info task-name "Moved %s files" (count metas))
(boot/commit! new-fs))))
(def ^:private +slug-defaults+
{; Parses `slug` portion out of the filename in the format: YYYY-MM-DD-slug-title.ext
; Jekyll uses the same format by default.
:slug-fn (fn [_ m] (->> (string/split (:filename m) #"[-\.]")
(drop 3)
drop-last
(string/join "-")
string/lower-case))
:filterer identity
:extensions [".html"]})
(deftask slug
"Renames a file so that the part before the extension matches the result of `slug-fn`"
[s slug-fn SLUGFN code "function to build slug from file metadata"
_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to include"]
(let [{:keys [slug-fn] :as options} (merge +slug-defaults+ *opts*)
path-fn (fn [global-meta m]
(let [{:keys [path filename]} m
slug (slug-fn global-meta m)]
(str (perun/parent-path path filename) slug "." (perun/extension filename))))]
(mv-pre-wrap {:task-name "slug"
:path-fn path-fn
:tracer :io.perun/slug
:options options})))
(def ^:private +permalink-defaults+
{:permalink-fn (fn [global-meta m]
(-> (str (:parent-path m) (:slug m))
perun/path-to-url
(str "/")
(string/replace (re-pattern (str "^" (:doc-root global-meta))) "")
perun/absolutize-url))
:filterer identity
:extensions [".html"]})
(deftask permalink
"Moves a file so that its location matches the result of `permalink-fn`"
[p permalink-fn PERMALINKFN code "function to build permalink from TmpFile metadata"
_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to include"]
(let [{:keys [permalink-fn] :as options} (merge +permalink-defaults+ *opts*)
path-fn (fn [global-meta m]
(let [permalink (permalink-fn global-meta m)]
(str (:doc-root global-meta)
"/"
(perun/url-to-path (string/replace permalink #"/$" "/index.html")))))]
(mv-pre-wrap {:task-name "permalink"
:path-fn path-fn
:tracer :io.perun/permalink
:options options})))
(deftask canonical-url
"Deprecated - The `:canonical-url` key will now automatically be set in the `entry` map passed
to your render functions, based on the location of the file in the fileset"
[_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"]
(boot/with-pass-thru _
(u/warn (str "The `canonical-url` task is deprecated. The `:canonical-url` key will now "
"automatically be set in the `entry` map passed to your render functions, "
"based on the location of the file in the fileset\n"))))
(def ^:private ^:deps sitemap-deps
'[[sitemap "0.4.0"]
[clj-time "0.15.2"]])
(def ^:private +sitemap-defaults+
{:filename "sitemap.xml"
:filterer identity
:extensions [".html"]
:out-dir "public"})
(deftask sitemap
"Generate sitemap"
[f filename FILENAME str "generated sitemap filename"
_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to include"
o out-dir 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 [metas (filter-meta-by-ext fileset options)]
(pod/with-call-in @pod
(io.perun.sitemap/generate-sitemap ~(.getPath tmp) ~metas ~(dissoc options :filterer)))
(commit fileset tmp)))))
(def ^:private ^:deps render-deps
'[[org.clojure/tools.namespace "0.3.1"]])
(def render-pod (delay (create-pod' render-deps)))
(defn- assert-renderer [sym]
(assert (and (symbol? sym) (namespace sym))
"Renderer must be a fully qualified symbol, i.e. 'my.ns/fun"))
(defn render-task
"Handles common rendering task orchestration
`paths-fn` takes a fileset as its only argument"
[{:keys [task-name paths-fn renderer tracer rm-originals pod]}]
(assert-renderer renderer)
(content-task
{:render-form-fn (fn [meta] `(io.perun.render/render ~renderer ~meta))
:paths-fn paths-fn
:task-name task-name
:tracer tracer
:pod (or pod render-pod)
:rm-originals rm-originals}))
(def ^:private +render-defaults+
{:out-dir "public"
:filterer identity
:extensions [".html"]})
(deftask render
"Render individual pages from input files
The symbol supplied as `renderer` should resolve to a function
which will be called with a map containing the following keys:
- `:meta`, global perun metadata
- `:entries`, all entries
- `:entry`, the entry to be rendered
Entries can optionally be filtered by supplying a function
to the `filterer` option."
[o out-dir OUTDIR str "the output directory (default: \"public\")"
_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to include"
r renderer RENDERER sym "page renderer (fully qualified symbol which resolves to a function)"
m meta META edn "metadata to set on each entry"]
(let [{:keys [renderer out-dir] :as options} (merge +render-defaults+ *opts*)]
(letfn [(render-paths [fileset]
(let [entries (filter-meta-by-ext fileset options)]
(reduce
(fn [result {:keys [path] :as entry}]
(let [content (slurp (boot/tmp-file (boot/tmp-get fileset path)))
new-path (apply-out-dir path (:out-dir entry) out-dir)
new-entry (merge entry
meta
{:content content
:out-dir out-dir})]
(assoc result new-path {:meta (pm/get-global-meta fileset)
:entries entries
:entry new-entry
:input-paths #{path}})))
{}
entries)))]
(render-task {:task-name "render"
:paths-fn render-paths
:renderer renderer
:tracer :io.perun/render
:rm-originals true}))))
(def ^:private +static-defaults+
{:out-dir "public"
:page "index.html"
:meta {}})
(deftask static
"Render an individual page solely from a render function
The symbol supplied as `renderer` should resolve to a function
which will be called with a map containing the following keys:
- `:meta`, global perun metadata
- `:entry`, the entry to be rendered"
[o out-dir OUTDIR str "the output directory"
r renderer RENDERER sym "page renderer (fully qualified symbol resolving to a function)"
p page PAGE str "static result page path"
m meta META edn "metadata to set on the static entry"]
(let [{:keys [out-dir renderer page meta]} (merge +static-defaults+ *opts*)
path (perun/create-filepath out-dir page)
static-path (fn [fileset]
{path {:meta (pm/get-global-meta fileset)
:entry (assoc meta :path path :out-dir out-dir)}})]
(render-task {:task-name "static"
:paths-fn static-path
:renderer renderer
:tracer :io.perun/static})))
(defn- grouped-paths
"Produces path maps of the shape required by `render-to-paths`, based
on the provided `fileset` and `options`."
[task-name fileset {:keys [grouper sortby comparator out-dir] :as options}]
(let [global-meta (pm/get-global-meta fileset)
paths (grouper (filter-meta-by-ext fileset options))]
(if (seq paths)
(reduce
(fn [result [path {:keys [entry entries]}]]
(let [sorted (->> entries
(sort-by sortby comparator)
(map #(assoc % :content (->> (:path %)
(boot/tmp-get fileset)
boot/tmp-file
slurp))))
new-path (apply-out-dir path (:out-dir entry) out-dir)
new-entry (merge entry
{:out-dir out-dir}
(pm/path-meta path global-meta))]
(assoc result new-path {:meta global-meta
:entry new-entry
:entries (vec sorted)
:input-paths (set sorted)})))
{}
paths)
(do
(perun/report-info task-name (str task-name " found nothing to render"))
[]))))
(defn assortment-task
"Handles common assortment task orchestration
`task-name` is used for log messages. `tracer` is a keyword that gets added
to the `:io.perun/trace` metadata. `grouper` is a function that takes a seq
of entries and returns a map of paths to render data (see docstring for
`assortment` for more info)"
[{:keys [task-name comparator filterer sortby grouper meta renderer tracer pod] :as options*}]
(cond (not (fn? comparator))
(u/fail (str task-name " task :comparator option should implement Fn\n"))
(not (ifn? filterer))
(u/fail (str task-name " task :filterer option value should implement IFn\n"))
(not (ifn? sortby))
(u/fail (str task-name " task :sortby option value should implement IFn\n"))
(not (ifn? grouper))
(u/fail (str task-name " task :grouper option value should implement IFn\n"))
:else
(let [;; Make sure task-level metadata gets added to each entry
meta-grouper (fn [entries]
(->> entries
grouper
(map (fn [[path data]]
[path (update-in data [:entry] #(merge meta %))]))
(into {})))
options (assoc options* :grouper meta-grouper)]
(render-task {:task-name task-name
:paths-fn #(grouped-paths task-name % options)
:renderer renderer
:tracer tracer
:pod pod}))))
(def ^:private +assortment-defaults+
{:out-dir "public"
:filterer identity
:extensions [".html"]
:sortby :date-published
:comparator (fn [i1 i2] (compare i2 i1))
:grouper #(-> {"index.html" {:entries %}})})
(deftask assortment
"Render multiple collections
The symbol supplied as `renderer` should resolve to a function
which will be called with a map containing the following keys:
- `:meta`, global perun metadata
- `:entry`, the metadata for this collection
- `:entries`, all entries
The `grouper` function will be called with a seq containing the
entries to be grouped, and it should return a map with keys that
are filenames and values that are maps with the keys:
- `:entries`: the entries for each collection
- `:entry`: (optional) page metadata for this collection
Entries can optionally be filtered by supplying a function
to the `filterer` option.
The `sortby` function can be used for ordering entries before rendering."
[o out-dir OUTDIR str "the output directory"
r renderer RENDERER sym "page renderer (fully qualified symbol resolving to a function)"
g grouper GROUPER code "group posts function, keys are filenames, values are to-be-rendered entries"
_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
e extensions EXTENSIONS [str] "extensions of files to include"
s sortby SORTBY code "sort entries by function"
c comparator COMPARATOR code "sort by comparator function"
m meta META edn "metadata to set on each collection entry"]
(let [options (merge +assortment-defaults+
*opts*
{:task-name "assortment"
:tracer :io.perun/assortment
:grouper (or grouper #(-> {"index.html" {:entries %}}))})]
(assortment-task options)))
(def ^:private +collection-defaults+