-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish.clj
More file actions
executable file
·92 lines (82 loc) · 3.57 KB
/
publish.clj
File metadata and controls
executable file
·92 lines (82 loc) · 3.57 KB
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
#!/usr/bin/env bb
(require '[babashka.process :as process]
'[babashka.fs :as fs]
'[clojure.string :as str])
(def base-url "https://icot.github.io/")
(defn org-to-html [filename]
"""Convert org file to html with provided template and style"""
(let [contents (:out
(process/shell {:out :string}
(str "pandoc "
"--standalone "
"--template=static/template.html "
"--from org "
"--to html "
"--toc=true --toc-depth=3 "
"--include-before-body=static/preamble.html "
"--include-after-body=static/postamble.html "
"--number-sections "
"--css=static/style.css "
filename)))]
(str contents)))
(defn html-to-html [filename]
"""Generate html file from html with provided template and style"""
(let [contents (:out
(process/shell {:out :string}
(str "pandoc "
"--standalone "
"--template=static/template.html "
"--from html "
"--to html "
"--toc=true --toc-depth=3 "
"--include-before-body=static/preamble.html "
"--include-after-body=static/postamble.html "
"--number-sections "
"--css=static/style.css "
filename)))]
(str contents)))
(defn extract-org-metadata [file-path]
(let [content (slurp file-path)
lines (str/split-lines content)]
(->> lines
(take-while #(or (re-matches #"^\#\+.*" %)
(str/blank? %)))
(remove str/blank?)
(keep (fn [line]
(when-let [[_ k v] (re-matches #"^\#\+(\w+):\s*(.+)" line)]
[(keyword (str/lower-case k)) v])))
(into {}))))
(defn process [folder]
"""Converts a folder of org files to html.
Returns list of metadata hashes for each processed file."""
(let [file-paths (map str (fs/glob folder "**{.org}"))]
(doall
(for [file-path file-paths]
(let* [file-name (fs/file-name file-path)
target-file (str/replace file-name #"org$" "html")
contents (org-to-html file-path)]
(do
(spit target-file contents)
(conj {:filename target-file} (extract-org-metadata file-path))))))))
(defn generate-index [metadata]
"""Generate index page"""
(let [title "<h1 class=\"title\">Archive</h1>"
entry (fn [metadata]
(str "<div class=\"post-date\">"
(:date metadata)
"</div><h2 class=\"post-title\"><a href="
base-url
(:filename metadata)
">"
(:title metadata)
"</a></h2>"))]
(str/join "\n" (map str (conj (for [m (reverse metadata)] (entry m)) title)))))
;;; Main
;; Convert posts
(def metadata (process "posts"))
;; Generate Index
(println (generate-index metadata))
;; Generate Tag files and Taglist
(defn -main [args])
;; Execute
;(apply -main *command-line-args*)