-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlp.clj
183 lines (151 loc) · 5.9 KB
/
mlp.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
; # Markdown literary programming for Clojure
; Author: Lin Pengcheng
; My Blog: [The Grand Unified Programming Theory: The Pure Function Pipeline Data Flow with Principle-based Warehouse/Workshop Model](https://github.com/linpengcheng/PurefunctionPipelineDataflow)
; Reference: [Markdown Literary programming that don't break the syntax of any programming language](https://github.com/linpengcheng/PurefunctionPipelineDataflow/blob/master/doc/markdown_literary_programming.md)
; ## Config
(def npp-tools "C:/Notepad++/tools")
; md2html-method:
; - :cmark-gfm (support button, don't support TOC)
; - :MultiMarkdown (support TOC, button display fail)
; - :pandoc (Don't use, Notepad++ preview HTML plugin don't support)
; - :bootleg (don't recommended)
(def md2html-method :cmark-gfm)
(def save-md-file? false)
(def save-html-file? false)
; ## Deps
(use 'babashka.fs
'clojure.java.shell
'clojure.string)
; ## Var
(def src-path-txt (first *command-line-args*))
(def md-path-txt (str src-path-txt ".md"))
(def html-path-txt (str src-path-txt ".html"))
(def npp_mlp_dir (str npp-tools "/MarkdownLiteraryProgramming"))
(def re-blank-line #"(?m)^(\s*)[\r\n]+")
(def re-code-blocks #"(?m)^\s*[^;]([.\s\S\w\r\n]*?)\r\n;") ;"
(def re-line-comment #"(?m)^\s*;[ ]?") ;"
; ## md2html Method
; ### bootleg (don't recommended)
; ```
;; (use 'babashka.pods)
;; (load-pod "bootleg")
;; (use 'pod.retrogradeorbit.bootleg.markdown)
;; (defn bootleg2html [md-txt]
;; (let [html-txt (markdown md-txt :data :html)]
;; (if save-md-file?
;; (spit md-path-txt md-txt :encoding "utf-8")
;; (delete-if-exists md-path-txt))
;; (if save-html-file?
;; (spit html-path-txt html-txt :encoding "utf-8")
;; (delete-if-exists html-path-txt))
;; html-txt))
; ```
; ### MultiMarkdown (option methon)
(defn mmd-fenced-code-blocks [x]
(-> x
first
(#(str "\r\n```language-clojure\r\n" % "\r\n```\r\n; "))))
(defn mmd2html [md-txt]
(let [mmdexe (str npp-tools "/MultiMarkdown/bin/multimarkdown.exe")
_ (spit md-path-txt md-txt :encoding "utf-8")
_ (sh mmdexe "--to=html"
(str "--output=" html-path-txt)
md-path-txt)
html-txt (slurp html-path-txt :encoding "utf-8")]
(when-not save-md-file?
(delete-if-exists md-path-txt))
(when-not save-html-file?
(delete-if-exists html-path-txt))
html-txt))
(def mmd-head-txt
(-> npp_mlp_dir
(str , "/MLP_common_head_mmd.md")
(slurp , :encoding "utf-8")
(clojure.string/replace , #"%npp_mlp_dir%" npp_mlp_dir)))
; ### cmark_gfm (recommended method)
(defn gfm-fenced-code-blocks [x]
(-> x
first
(#(str "\r\n```clojure\r\n" % "\r\n```\r\n; "))))
(defn gfm2html [md-txt]
(let [gfmexe (str npp-tools "/cmark_gfm/cmark-gfm.exe")
_ (spit md-path-txt md-txt :encoding "utf-8")
html-txt (-> (sh gfmexe "--to" "html" md-path-txt)
:out
(clojure.string/replace ,
#"class=\"language-mermaid\"" ;"
"class=\"mermaid\""))]
(when-not save-md-file?
(delete-if-exists md-path-txt))
(if save-html-file?
(spit html-path-txt html-txt :encoding "utf-8")
(delete-if-exists html-path-txt))
html-txt))
(def gfm-head-txt
(-> npp_mlp_dir
(str , "/MLP_common_head_gfm.md")
(slurp , :encoding "utf-8")
(clojure.string/replace , #"%npp_mlp_dir%" npp_mlp_dir)))
; ### pandoc (Don't use, Notepad++ preview HTML plugin don't support)
;```
;;(defn pandoc2html [md-txt]
;; (let [pandocexe (str npp-tools "/pandoc64/pandoc.exe")
;; _ (spit md-path-txt md-txt :encoding "utf-8")
;; _ (sh pandocexe "--from=markdown"
;; "--to=html"
;; "-o" html-path-txt
;; "--toc"
;; md-path-txt)
;; html-txt (slurp html-path-txt :encoding "utf-8")]
;; (when-not save-md-file?
;; (delete-if-exists md-path-txt))
;; (when-not save-html-file?
;; (delete-if-exists html-path-txt))
;; html-txt))
;```
; ### United md2html-method API
;; add Fenced code blocks
(defn fenced-code-blocks [txt md2html-method]
(case md2html-method
:cmark-gfm
(clojure.string/replace txt re-code-blocks gfm-fenced-code-blocks)
:MultiMarkdown
(clojure.string/replace txt re-code-blocks mmd-fenced-code-blocks)
;; :pandoc
;; (clojure.string/replace txt re-code-blocks mmd-fenced-code-blocks)
;; :bootleg
;; (clojure.string/replace , re-code-blocks mmd-fenced-code-blocks)
(clojure.string/replace txt re-code-blocks gfm-fenced-code-blocks)))
(defn common-head-tail [txt md2html-method]
(let [md-tail-txt "\r\n\r\n</body></html>\r\n\r\n"]
(case md2html-method
:cmark-gfm
(str gfm-head-txt txt md-tail-txt)
:MultiMarkdown
(str mmd-head-txt txt md-tail-txt)
;; :pandoc
;; (str gfm-head-txt txt md-tail-txt)
;; :bootleg
;; (str gfm-head-txt txt md-tail-txt)
(str gfm-head-txt txt md-tail-txt))))
(defn md2html [txt md2html-method]
(case md2html-method
:cmark-gfm
(gfm2html txt)
:MultiMarkdown
(mmd2html txt)
;; :pandoc
;; (pandoc2html txt)
;; :bootleg
;; (bootleg2html txt)
(gfm2html txt)))
; ## Main
(-> src-path-txt
(slurp , :encoding "utf-8")
(clojure.string/replace , re-blank-line ";\r\n") ;line comment let blank line to md blank line
(fenced-code-blocks , md2html-method)
(clojure.string/replace , re-line-comment "") ;remove all first line comment chars at line ahead
(common-head-tail , md2html-method)
(md2html , md2html-method)
println)
; ## End