-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpandoc.lisp
135 lines (118 loc) · 3.88 KB
/
pandoc.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
(in-package :om-manual-conversion)
(defun pandoc-command (&key input
input-file
in-fmt
out-fmt
in-type
standalone
(lang "en")
(out-type out-fmt)
(output :string))
"Returns a list for use by (pandoc), which doesn't yet exist.
* [2023-06-29 Thu] TODO: Use Screamer to unify parameters per a consistency rubric. Make sense?
"
(declare (boolean standalone)
(string lang))
(cond
(input-file
(when input (assert (equalp input-file input)))
(check-type input-file (or string pathname)))
(input
(assert (null input-file))
(when (pathnamep input)
(setf input-file input))))
(assert out-fmt (out-fmt) "The :out-fmt key is required.")
(check-type out-fmt string)
(unless in-type
(setq in-type (or in-fmt
(pathname-type input-file))))
(check-type in-type string)
(check-type out-fmt string)
(check-type out-type string)
(when (string-equal out-fmt "gfm")
(assert (string-equal out-type "md")))
(when (eq :file output)
(setq output (make-pathname :type out-type :defaults input-file))
;; (break "Modified output: ~A" output)
)
(when standalone
(assert (equalp out-fmt "html")))
(let ((command `("pandoc"
,@(when lang
`("-V" ,(format nil "lang=~A" lang)))
,@(when standalone
`("-s"))
,@(when in-fmt
`("-f" ,in-fmt))
"-t" ,out-fmt
,@(ensure-list
(when input-file
(let* ((input-file-pathname (ensure-pathname input-file))
(input-file~ (probe-file input-file-pathname))
#|(expr (format nil "pandoc XXX")|#)
(unless (file-exists-p input-file~)
(error 'file-error :pathname input-file-pathname))
(namestring input-file~)))))))
(values command
(list :output output))))
(defun html-string->md (input &rest *keys
&key (output :string)
(in-fmt "html")
(out-fmt "gfm")
(out-type "md")
&allow-other-keys)
(declare (string input))
"TODO [parameter-subtyping]: Inherit from (pandoc-command).
"
(let-1 command (apply #'pandoc-command :input t :output output
:in-fmt in-fmt :out-fmt out-fmt
:out-type out-type
*keys)
(with-input (*standard-input* input)
(run-program command :input t :output output))))
(defun html-file->md (input-file &rest *keys
&key (output :string)
(in-fmt "html")
(out-fmt "gfm")
(out-type "md")
&allow-other-keys)
"By default, returns output as a string. To write to a file, use `:output`, e.g.::
(html-file->md \"00-Sommaire.html\" :output :file)
This creates a file with the same base name, substituting 'md' for the extension. Pass a pathname-designator to `:output` to override the naming.
"
(let+ (((&values command plist) (apply #'pandoc-command :input-file input-file :output output
:in-fmt in-fmt :out-fmt out-fmt
:out-type out-type
*keys))
((&plist-r/o (output~ :output)) plist))
(run-program command :output output~)))
(defun md-file->html (file
&key (output :string)
(standalone t)
&aux
(in-fmt "gfm")
(in-type "md")
(out-fmt "html")
;; (file
;; (probe-file
;; (make-pathname :name (pathname-name file~)
;; :directory (pathname-directory file~)
;; :type "md")))
)
"By default, returns output as a string. To write to a file, e.g.::
(md-file->html \"00-Sommaire.md\" :output :file)
"
(let+ (((&values command plist) (pandoc-command :input-file file :output output
:in-fmt in-fmt :out-fmt out-fmt
:in-type in-type
:standalone standalone))
((&plist-r/o (output~ :output)) plist))
(run-program command :output output~)))
(defun md-string->html (input
&key (output :string))
(with-input (*standard-input* input)
(run-program (list "pandoc"
"-f" "gfm"
"-t" "html")
:input t
:output output)))