-
Notifications
You must be signed in to change notification settings - Fork 27
/
conversion.clj
123 lines (105 loc) · 4.4 KB
/
conversion.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
(ns conversion
(:require [clojure.string :as string]))
;; This namespace has been used to convert the old `docs/index.Rmd` documentation to the new `notebooks/index.clj` documentation. Some additional manual editing was needed to make things work afterwards.
(set! *print-length* 1000)
(def example-doc
(slurp "docs/index.Rmd"
#_"https://raw.githubusercontent.com/scicloj/tablecloth/master/docs/index.Rmd"))
(defn chunk-end? [line]
(= line "```"))
(defn clojure-chunk-beginning? [line]
(re-matches #"```\{clojure.*\}" line))
(defn clojure-chunk-options [line]
(-> line
(string/replace #"^```\{clojure" "")
(string/replace #"\}$" "")))
(defn markdown->markdown-with-extracted-clojure-chunks [markdown]
(loop [lines (string/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 clojure-chunk->markdown [{:keys [clojure-chunk? code options]}]
(when clojure-chunk?
(str "```{clojure"
options
"}\n"
(string/join "\n" code)
"\n```")))
(defn markdown-with-extracted-clojure-chunks->markdown [mwecc]
(->> mwecc
(map #(or (clojure-chunk->markdown %)
%))
(string/join "\n")
(format "%s\n")))
(comment
(->> example-doc
(spit "/tmp/0.Rmd"))
(->> example-doc
markdown->markdown-with-extracted-clojure-chunks
markdown-with-extracted-clojure-chunks->markdown
(spit "/tmp/a.Rmd"))
(->> example-doc
markdown->markdown-with-extracted-clojure-chunks
markdown-with-extracted-clojure-chunks->markdown
(= example-doc))
(->> example-doc
markdown->markdown-with-extracted-clojure-chunks
(partition-by string?)))
(defn inner-pr-str [s]
(let [s1 (pr-str s)
n (count s1)]
(subs s1 1 (dec n))))
(defn markdown-with-extracted-clojure-chunks->clojure [mwecc]
(->> mwecc
(partition-by string?)
(mapcat (fn [part]
(cond (-> part first string?) [(->> part
(map inner-pr-str)
(string/join "\n")
((fn [s]
(if (seq s)
(format "(md \"%s\")" s)
""))))]
(-> part first :clojure-chunk?) [(->> part
(mapcat
(fn [{:keys [code options]}]
code
#_(cons (format "^{:chunk-options \"%s\"}[]\n"
options)
code)))
(string/join "\n"))])))
(string/join "\n\n\n")
(format "
(ns index
(:require [scicloj.kindly.v3.kind :as kind]
[scicloj.kindly-default.v1.api :refer [md]]
[tablecloth.api :as tc]
[scicloj.note-to-test.v1.api :as note-to-test]))
%s")))
(->> example-doc
markdown->markdown-with-extracted-clojure-chunks
markdown-with-extracted-clojure-chunks->clojure
(spit "notebooks/index.clj"))