/
play.clj
executable file
·497 lines (432 loc) · 16.6 KB
/
play.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
#!/usr/bin/env bb
;; Action - edit tags
;;
;; ./play relations :from :files :to :lines > lines.txt
;; # edit lines.txt - do stuff in batch ...
;; ./play relations :from :lines :to :files < lines.txt
;;
;; Action - create new page
;;
;; ./play page rdf-intro :title "Introduction to linked data with RDF and Datalog"
;; ./play create-page rdf-intro :title "Introduction to linked data with RDF and Datalog"
;;
;; Wanted commands:
;;
;; ./play index > index.html # generate index.html
;; ./play preview
;;
;; Wanted behavior:
;;
;; When I create a new page, I write :author-url and :created-at
(require '[babashka.deps :as deps])
(deps/add-deps '{:deps {org.babashka/cli {:mvn/version "0.6.50"}
eu.teod/pandoc-toolbox {:local/root "../pandoc-toolbox"}}})
(require 'babashka.cli :reload)
(require '[babashka.cli :as cli]
'[babashka.fs :as fs]
'[clojure.string :as str]
'[clojure.java.shell]
'[clojure.string :as str]
'[clojure.edn :as edn]
'[clojure.pprint :refer [pprint]]
'[cheshire.core :as json]
'[teod.pandoc-toolbox :as pandoc])
;; relations example
;;
;; page id -> page metadata
{"emacs" {:slug "emacs"
:title "(Doom) Emacs learning journal"
:form :rambling
:readiness :in-progress}
"feedback-design-impl" {:title "Feedback loops, API design and how stuff works"}}
(defn bash [cmd]
(str/trim (:out (clojure.java.shell/sh "bash" "-c" cmd))))
(defn words [& args] (str/join " " (map str args)))
(defn lines [& args] (str/join "\n" (map str args)))
(defn pages []
(->> (bash "ls **/play.edn | sed 's|/play.edn||g'")
(str/split-lines)
(map (fn [slug] {:slug slug}))))
(defn files->relations
"Read relations from play.edn files on disk
:uuid-from-org - when true, try to crudely find UUIDs from index.org files.
"
[{:keys [uuid-from-org]}]
(let [enrich (fn [page]
(if (and uuid-from-org (not (:builder page))) ;; only normal org thing builders please
(let [uuid-found-on-org (-> (bash (str "cat " (:slug page) "/index.org | grep ID"))
(str/replace #":ID:\s+" "")
(str/split #"\s+")
first)]
(if (str/blank? uuid-found-on-org)
page
(assoc page :uuid uuid-found-on-org)))
page))]
(->> (pages)
(map (fn [{:keys [slug] :as p}]
(merge p (edn/read-string (slurp (str slug "/play.edn"))))))
(map enrich)
(map (juxt :slug identity))
(into {}))))
(defn relations->lines
"Produce one line per page
Example:
:slug emacs, :title \"(Doom) Emacs learning journal\", :form :rambling, :readiness :in-progress"
[rels]
(doseq [l (vals rels)]
(prn l)))
(defn relations->lines2
"Produce one line per relation
Output format:
[:slug \"emacs\"] :title \"(Doom) Emacs learning journal\"
[:slug \"emacs\"] :form :rambling
[:slug \"emacs\"] :readiness :in-progress
Example: how many WTF-pages do we have?
./play.clj relations :from files :to lines2 | grep -i \":readiness :wtf\" | wc -l
30
Example: how many non-WTF-pages do we have?
./play.clj relations :from files :to lines2 | grep :readiness | grep -v \":readiness :wtf\" | wc -l
9
"
[rels]
(doseq [[slug page-meta] rels]
(doseq [[attribute value] (dissoc page-meta :slug)]
(prn [:slug slug] attribute value))))
(defn relations->lines+recent
"Produce one line per relation, recent first
Example: list recently created pages, with page ID and created date:
./play.clj relations :from files :to lines+recent | ./play.clj relations :from lines :to lines2 | grep :created
"
[rels]
(let [recent-lines (->> (vals rels)
(filter :created)
(sort-by :created)
reverse)]
(doseq [l recent-lines]
(prn l))))
(defn relations->pretty
[rels]
(pprint rels))
(defn lines->relations
"Relations from lines on stdin"
[{}]
(->> (str/split-lines (slurp *in*))
(map edn/read-string)
(map (fn [{:keys [slug] :as page}] {slug page}))
(into {})))
(defn relations->files [rels]
(doseq [[id page] rels]
(spit (str id "/play.edn")
(with-out-str
(pprint (dissoc page :slug))))))
(defn page-index-org [{:keys [title trailing-blank-lines uuid]}]
(str/join "\n"
(concat
;; Link for org-roam
(when uuid
[":PROPERTIES:"
(str ":ID: " uuid)
":END:"])
;; Header, title, link up
[(str "#+TITLE: " title)
""
"[[file:..][..]]
DRAFT
"
]
;; Trailing whitespace.
(when trailing-blank-lines
(concat ["#+begin_verse"]
(repeat trailing-blank-lines "")
["#+end_verse"
""])))))
(def relations-config
{:sources {:files files->relations
:lines lines->relations}
:targets {:lines relations->lines
:lines2 relations->lines2
:lines+recent relations->lines+recent
:pretty relations->pretty
:files relations->files}})
(defn relations-helptext []
(let [helptext (lines
"usage: ./play.clj relations :from SOURCE :to TARGET"
""
"valid sources:"
(str " " (apply words (keys (:sources relations-config))))
""
"valid targets:"
(str " " (apply words (keys (:targets relations-config)))))]
helptext))
(defn relations [{:keys [opts]}]
(let [sources {:files files->relations
:lines lines->relations}
targets {:lines relations->lines
:lines2 relations->lines2
:lines+recent relations->lines+recent
:pretty relations->pretty
:files relations->files}
{:keys [from to]} opts
input-is-valid (and (contains? sources from)
(contains? targets to))]
(if-not input-is-valid
(println (relations-helptext))
(let [rels ((sources from) opts)]
((targets to) rels)))))
(defn random-page [{:keys [opts]}]
(if (:dry-run opts)
(do
;; dry run is mostly for dev
(pprint (->> (files->relations {})
vals
(remove #(= :remote-reference
(:form %)))
(take 5))))
(let [n (or (:n opts) 1)]
(println
(str/join "\n"
(->> (files->relations {})
vals
(remove #(= :remote-reference
(:form %)))
shuffle
(take n)
(map :slug)))))))
(defn create-page [{:keys [opts]}]
(let [slug (:slug opts)
title (or (:title opts) slug)
uuid (or (:uuid opts) (bash "uuidgen"))
lang (or (:lang opts) :en)
valid-opts? (and slug title uuid lang)
fake-spit (fn [path content]
(println (str "Would write to " path ":"))
(println)
(doseq [l (str/split-lines content)]
(println " " l))
(println))
fake-bash (fn [& args]
(println "Would run:")
(println)
(println (str " " (pr-str (apply list 'bash args))))
(println)
)
helptext (str/trim "
Usage:
./play.clj create-page [:slug] SLUG [OPT...]
Allowed options:
:title TITLE
:uuid UUID
:lang LANG
:form FORM
")]
(when (or (:h opts) (:help opts))
(println helptext)
(System/exit 0))
(when (not valid-opts?)
(println helptext)
(System/exit 1))
(assert slug "Page slug is required.")
(let [org-file (str slug "/index.org")
play-file (str slug "/play.edn")]
(fs/create-dirs slug)
;; Org file
(when-not (fs/exists? org-file)
(let [org-file-contents (page-index-org {:title title
:uuid uuid})]
(if (:dry-run opts)
(fake-spit org-file org-file-contents)
(spit org-file org-file-contents))))
;; Play file
(when-not (fs/exists? play-file)
(let [page-meta (into (sorted-map)
{:title title
:readiness :wtf-is-this
:uuid uuid
:author-url "https://teod.eu"
:created (str/trim (bash "date -I"))
:lang lang})
page-meta (if (:form opts) (assoc page-meta :form (:form opts))
page-meta)
play-file-content (with-out-str (pprint page-meta))
]
(if (:dry-run opts)
(fake-spit play-file play-file-content)
(spit play-file play-file-content)))
;; Regenerate the makefile since we've added a new target
(if (:dry-run opts)
(fake-bash "./play.clj makefile")
(bash "./play.clj makefile")))
nil)))
(defn builder [rel]
(:builder rel :pandoc-page))
(defmulti makefile-entry builder)
(defmethod makefile-entry :pandoc-page
[rel]
(let [t rel
org (fn [target] (str target "/index.org"))
html (fn [target] (str target "/index.html"))]
(str (html t) ": " (org t)
"\n\t"
(words "pandoc -s --shift-heading-level-by=1 --from=org+smart -i" (org t) "-t json"
"|"
"./play.clj filter resolve-links"
"|"
"pandoc -f json -o" (html t) "--standalone --toc -H header-default-include.html -H scittle/scittle-with-extras.html"))))
(defn makefile [{:keys [opts]}]
(let [{:keys [dry-run]} opts
targets (->> (files->relations {})
vals
;; For now, /only/ make builders for normal pandoc pages (same as default)
(filter (fn [rel]
(let [b (builder rel)]
(or (= b :pandoc-page)
(= b :smart-pandoc-page)))))
(map :slug)
sort)
html (fn [target] (str target "/index.html"))
play-edn (fn [target] (str target "/play.edn"))
indent (fn [s] (str "\t" s))
makefile (lines
"# DO NOT EDIT directly -- THIS MAKEFILE IS GENERATED"
"# SEE `make makefile` TARGET"
""
""
"# Generate target for root index"
;; TODO root index also depends on all the play.edn files found
(str/join " " (concat ["index.html:" "index.clj"] (map html targets) (map play-edn targets) (list "404.html" "header-default-include.html")))
"\t./index.clj"
""
""
"# Generate target for 404"
"404.html: 404.org"
(indent
"pandoc --from=org+smart -i 404.org -t json | cat | pandoc -f json -o 404.html --standalone -H header-default-include.html -H scittle/scittle-with-extras.html")
""
""
"# Generate target for each page"
(str/join "\n\n"
(for [t targets]
;; Here's the pandoc command I'd like for some pages:
(makefile-entry t)))
""
""
"# Note: generating the makefile with the makefile is sometimes problematic."
".PHONY: makefile"
"makefile:"
"\t./play.clj makefile"
""
""
"# Rengenerate the index"
"# Note: we don't remove the makefile, as that gets us ... stuck."
".PHONY: clean"
"clean:"
"\trm -f index.html"
""
""
"# Regenerate everything"
".PHONY: ultraclean"
"ultraclean: clean"
(str "\t"
"rm -f "
;; Here's a pandoc command I'd like for some pages
(str/join " " (concat ["index.html"] (map html targets))))
)]
(if dry-run
(println makefile)
(spit "Makefile" (str (str/trim makefile) "\n")))))
(defn reindex
"Create an index from page uuid to slug and title."
[{:keys [opts]}]
(let [{:keys [dry-run]} opts
uuid-index (->> (files->relations {})
vals
(filter :uuid)
#_
(map #(select-keys % [:uuid :slug :title :id]))
(sort-by :uuid))
big-index (atom [])]
(if dry-run
;; just print pages
(doseq [page uuid-index]
(prn page))
;; otherwise, write to index/by-uuid/{UUID}.edn
(do
;; ensure "index/by-uuid/" exists
(fs/create-dirs "index/by-uuid/")
;; write an EDN file per UUID
(doseq [page uuid-index]
(spit (str "index/by-uuid/" (:uuid page) ".edn")
(with-out-str (pprint page)))
(swap! big-index conj page))
;; write one big EDN file
(spit "index/big.edn" (with-out-str (pprint @big-index)))
;; write one big JSON index
(spit "index/big.json" (json/generate-string @big-index {:pretty true}))
))))
(defn filter-pandoc [{:as cmd-opts}]
;; only supported filter for now is resolve-links
;;
;; Test with:
;;
;; ./play.clj filter resolve-links < ../pandoc-toolbox/pandoc-examples/link.json
(when (:h (:opts cmd-opts))
(println (str/trim "
Usage:
./play.clj filter resolve-links < ../pandoc-toolbox/pandoc-examples/link.json
"))
(System/exit 0))
(when (contains? (set (:rest-cmds cmd-opts))
"resolve-links")
(let [pandoc-json (json/parse-string (slurp *in*))
by-uuid (fn [uuid]
(let [path (str "index/by-uuid/" uuid ".edn")]
(when (fs/regular-file? path)
(edn/read-string (slurp path)))))
uuid->slug (fn [uuid] (:slug (by-uuid uuid)))
replace-link (fn [x]
(if (pandoc/link? x)
(let [link x
uuid (last (str/split (pandoc/link-target link) #":"))
slug (uuid->slug uuid)]
(if slug
(assoc-in link pandoc/link-target-path (str "../" slug "/"))
link))
x))
resolved (pandoc/filter-body-postwalk pandoc-json replace-link)]
(println (json/generate-string resolved)))))
(def dispatch-table
[{:cmds ["create-page"] :fn create-page :cmds-opts [:slug]}
{:cmds ["filter"] :fn filter-pandoc :cmds-opts [:resolve-links]}
{:cmds ["reindex"] :fn reindex}
{:cmds ["makefile"] :fn makefile}
{:cmds ["random-page"] :fn random-page}
{:cmds ["relations"] :fn relations}])
(defn print-subcommands [{}]
(println "usage: ./play.clj COMMAND")
(println "")
(println "Available commands:")
(doseq [{:keys [cmds]} dispatch-table]
(println (str " "
(str/join " " cmds)))))
(defn main [& args]
(cli/dispatch (concat dispatch-table
[{:cmds ["help"] :fn print-subcommands}
{:cmds [] :fn print-subcommands}])
args
{:coerce {;; relations
:from :keyword ; page relation format
:to :keyword ; page relation format
:dry-run :boolean
;; page
:title :string ; Page title
:n :long ; Count - eg random page count
:uuid :string ; UUID for me and Org-roam
:lang :keyword ; Article language, :en or :no
;; filter
:resolve-links :boolean
}}))
(when (= *file* (System/getProperty "babashka.file"))
(apply main *command-line-args*))
;; How to run:
;;
;; ./play.clj relations :from :files :to :lines