-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathreadme_generation.clj
85 lines (76 loc) · 2.93 KB
/
readme_generation.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
(ns readme-generation
(:require [clojure.string :as str]
[clojure.pprint :as pp]))
(set! *print-length* 1000)
(defn chunk-end? [line]
(= line "```"))
(defn clojure-chunk-beginning? [line]
(re-matches #"```\{clojure.*\}" line))
(defn clojure-chunk-options [line]
(-> line
(str/replace #"^```\{clojure" "")
(str/replace #"\}$" "")))
(defn markdown->markdown-with-extracted-clojure-chunks [markdown]
(loop [lines (str/split markdown #"\n")
current-clojure-chunk-code nil
current-clojure-chunk-options nil
result []]
(if-let [current-line (first lines)]
(if current-clojure-chunk-code
(if (chunk-end? current-line)
(recur (rest lines)
nil
nil
(conj result {:clojure-chunk? true
:code current-clojure-chunk-code
:options current-clojure-chunk-options}))
(recur (rest lines)
(conj current-clojure-chunk-code current-line)
current-clojure-chunk-options
result))
(if (clojure-chunk-beginning? current-line)
(recur (rest lines)
[]
(clojure-chunk-options current-line)
result)
(recur (rest lines)
nil
nil
(conj result current-line))))
result)))
(defn markdown-with-extracted-clojure-chunks->markdown-evaluated [mwecc]
(->> mwecc
(map (fn [element]
(if (string? element)
element
(let [{:keys [code options]} element
options-map (-> options
str/trim
(str/split #"=")
(->> (map read-string)
(apply hash-map)))]
(str (format "```{clojure}\n%s\n```\n"
(->> code
(str/join "\n")))
(if (-> options-map
(get 'eval)
(= 'FALSE))
""
(let [result (->> code
(str/join "\n")
load-string)]
(case (-> options-map
(get 'results))
"hide" ""
"asis" (-> result
pp/pprint
with-out-str)))))))))
(str/join "\n")))
(defn generate! []
(->> "README-source.md"
slurp
markdown->markdown-with-extracted-clojure-chunks
markdown-with-extracted-clojure-chunks->markdown-evaluated
(spit "README.md")))
(comment
(generate!))