forked from Dumaiu/om-manual
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-files.lisp
278 lines (228 loc) · 8.86 KB
/
convert-files.lisp
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
(in-package :om-manual-conversion)
(export 'skip-file)
;; (defparameter *manual.md.html* file)
;; (defparameter *orig-manual.md.ystok* (parse-html *manual*))
(defun html->sexp+ (file)
"Convert, with modifications."
(let-1 sexp (parse-html file :callbacks `((:div . ,(λ div-form
"Remove 'googleSearchFrom' `div` element."
;; (break "Arg: ~A" div-form)
(match div-form
((list* (list* :div (plist :class "googleSearchFrom")) _)
;; (break "class: ~A" class)
;; (break "Arg: ~A" div-form)
(values nil t) ; delete element
))))
(:a . ,(λ anchor-form
"* Remove 'Scenari' `<a>` at end of page.
* Change .html extensions to .md.
"
(match anchor-form
((list* (list* :a (plist :href "http://scenari-platform.org")) _)
(values nil t)) ; delete element
((guard (list* (list* :a (and plist
(plist :href href))) _)
(string-equal "html"
(pathname-type href)))
(let-1 md-anchor (namestring (make-pathname :type "md"
:defaults href))
(setf (getf plist :href) md-anchor)
;; (break "HTML anchor: ~S" anchor-form)
(values anchor-form t))))))
(:img . ,(λ img-form
"Add alt-text to `<img>` tags."
(match img-form
((guard (list* (list* :img (and plist-form
(plist :src src :alt alt-text)))
_)
(or (null alt-text)
(string-equal alt-text "")))
;; (break "Null-text img: ~A" img-form)
;; TODO: Plist setter; no-copy lens:
(let-1 alt-text~ (namestring
(make-pathname :name (pathname-name src)
:type (pathname-type src)))
(setf (getf plist-form :alt) alt-text~)
;; (rplacd (last plist-form) (list :alt alt-text~))
)
;; (break "Null-text img: ~S" plist-form)
(values img-form t)))))
))
sexp))
;; ''(
;; (parse-html
;; (with-input-file (f *manual*)
;; (slurp-stream-string f)))
;; ;; (slurp-stream-string *manual.md*) XXX
;; slurp-input-stream
;; )
;; (parse-html *manual*)
;; ''(
;; ;; 1.
;; (defparameter *manual.ystok* (html->sexp+ *manual*)
;; "Parse HTML to Lisp, making modifications.")
;; ;; 2.
;; (defparameter *manual.ystok.html*
;; (to-html *manual.ystok*)
;; "Convert back to HTML. NB: the URLs still point to Markdown files!")
;; ;; 3. Generate Markdown file
;; (html-string->md *manual.ystok.html* :output *manual.md*) ; *side-effect*
;; )
(defun html-file->md+-file (file &key verbose
&allow-other-keys)
"Write to new file.
* [2023-06-29 Thu] Added `skip-file` restart.
"
(let+ (((&values result skipped?) (with-simple-restart (skip-file "Skip file ~S" file)
(let* ((sexp (html->sexp+ file))
(html+ (to-html sexp)))
(let-1 newfile (make-pathname :type "md" :defaults file)
(html-string->md html+ :output newfile)
(when verbose
(format t "~&Converted ~S -> ~S.~%" file newfile))
newfile)))))
(if skipped?
(values)
result)))
(defun html-directory->md+ (dir &rest *keys
&key (verbose t)
&allow-other-keys
&aux (pattern "*.html"))
(setq dir (ensure-directory-pathname dir))
(assert (directory-exists-p dir))
;; (collect-sub*directories dir )
(let-1 files (directory-files dir pattern)
(loop for f in files
do (apply #'html-file->md+-file f :verbose verbose *keys)))
dir)
;; TODO: (export-from 'html-directory->md+ *project-pkg*)
(defun generate-html-from-markdown-file (pathname &key
(verbose t)
(output-directory *default-directory*)
&allow-other-keys)
"
If the `skip-file` restart is used, the primary retval will be NIL, with the failed input file as a second val.
TODO: Use :log4cl to handle `verbose`.
"
(let ((html (md-file->html pathname))
(output-pathname (make-pathname :type "html" :defaults pathname
:directory (pathname-directory output-directory)))
#|(sexp (parse-html html))
(html~ (to-html sexp))|#)
(let-1 success? (with-simple-restart (skip-file "Skip file ~S" pathname)
(modify-html html :callbacks `((:a . ,(λ anchor-form
" * Change '.md' extensions to '.html'.
"
(match anchor-form
((guard (list* (list* :a (and plist
(plist :href href))) _)
(string-equal "md"
(pathname-type href)))
(let-1 new-href (namestring (make-pathname :type "html"
:defaults href))
(setf (getf plist :href) new-href)
;; (break "HTML anchor: ~S" anchor-form)
(values anchor-form t)))))))
:output output-pathname)
(assert (file-exists-p output-pathname))
(when verbose
(format t "~&Converted ~S -> ~S.~%" pathname output-pathname))
t)
(if success?
output-pathname
(progn
;; (break)
(values nil pathname))))))
(defun generate-html-from-markdown (&optional (pathname *default-directory*) &rest *keys
&key
output-directory
(verbose t)
&allow-other-keys
&aux
(md-wildcard "*.md"))
"
Args:
- pathname: If a directory, iterate over Markdownfiles recursively.
"
(declare (type pathname-designator pathname))
(let-1 pathname (ensure-pathname pathname)
(cond
((directory-pathname-p pathname)
(assert (directory-exists-p pathname))
(unless output-directory
(setq output-directory pathname))
(loop with files = (directory-files pathname md-wildcard)
with failures! = ()
with successes! = ()
with n = (length files)
for i from 1
for f in files
;; Recurse:
do (when verbose
(format t "~&~D/~D~%" i n))
do (assert (not (directory-pathname-p f)))
do (let+ (((&values result skipped) (apply #'generate-html-from-markdown f
:output-directory output-directory
:verbose verbose
*keys)))
(declare (type (or pathname null) result skipped))
(nconcf successes! (ensure-list result))
(nconcf failures! (ensure-list skipped)))
finally (progn
(when verbose
(format t "~2& Failures: ~S~%" failures!))
(return (values successes! `(:failures ,failures!))))
))
(t
;; (break)
(assert (file-exists-p pathname))
(assert (string-equal "md" (pathname-type pathname)))
(unless output-directory
(setq output-directory *default-directory*))
(apply #'generate-html-from-markdown-file pathname
:verbose verbose
:output-directory output-directory
*keys)))))
''(
(html-file->md+-file *manual*) ; * side-effect*--convert one file
;; Dealing with [Dumaiu/om-manual#9]:
(html-file->md+-file #p"/mnt/c/Users/Jonathan/Documents/openmusic/support.ircam.fr/docs/om/om6-manual/co/OM-Documentation_3.html")
(html-directory->md+ *default-directory*)
)
(generate-html-from-markdown *manual.md*) ; *side-effect*
''(
;; 4,5,6. Convert to Lisp, editing anchors; then return to HTML.
(generate-html-from-markdown *default-directory*)
)
;; (let* ((html (md-file->html *manual.md*))
;; #|(sexp (parse-html html))
;; (html~ (to-html sexp))|#)
;; (modify-html html :callbacks `((:a . ,(λ anchor-form
;; " * Change '.md' extensions to '.html'.
;; "
;; (match anchor-form
;; ((guard (list* (list* :a (and plist
;; (plist :href href))) _)
;; (string-equal "md"
;; (pathname-type href)))
;; (let-1 new-href (namestring (make-pathname :type "html"
;; :defaults href))
;; (setf (getf plist :href) new-href)
;; ;; (break "HTML anchor: ~S" anchor-form)
;; (values anchor-form t)))))))
;; :output (make-pathname :name "OM-User-Manual.md" :type "html" :defaults *manual*
;; :directory (pathname-directory *default-directory*))))
;; (md-file->html *manual.md* :output (make-pathname :name "OM-User-Manual.md" :type "html" :defaults *manual*)) ; *side-effect*
''(
(html-string->md *manual.ystok.html*)
(html-file->md *manual*) ; ✓
(html-file->md *manual* :output :file) ; ✓
(md-string->html (md-file->html "00-Sommaire.md"))
(md-file->html *manual.md*) ; ✓
(md-file->html *manual.md* :output (make-pathname :name "OM-User-Manual.md" :type "html" :defaults *manual*)) ; ✓
;; (md-file->html "01-Presentation.md" :output :file)
(pandoc-command :input-file *manual.md*
:in-fmt "gfm"
:out-fmt "html"
:standalone t)
)