-
Notifications
You must be signed in to change notification settings - Fork 4
/
command-line.lisp
142 lines (118 loc) · 4.94 KB
/
command-line.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
; Copyright (c) 2022 Justin Meiners
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, version 2.
;
; This program is distributed in the hope that it will be useful, but
; WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
; General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>.
(in-package :srcweave)
(defun parse-empty-arg-as-null (arg)
(if (and (stringp arg) (= (length arg) 0))
'()
arg))
(opts:define-opts
(:name :help
:description "Show program help."
:long "help"
:short #\h)
(:name :tangle
:description "Generate source code from documents, including creating files for corresponding blocks. Tangle is lazy and uses file modification dates to determine when to rebuild."
:long "tangle"
:short #\t
:arg-parser (lambda (text)
(uiop:ensure-pathname text :ensure-directory t)))
(:name :weave
:description "Generate HTML documentation for documents. One .html is created for each .lit input file."
:long "weave"
:short #\w
:arg-parser (lambda (text)
(uiop:ensure-pathname text :ensure-directory t)))
(:name :md-compiler
:description "Specify a command for converting markdown to HTML. Default: markdown."
:arg-parser #'parse-empty-arg-as-null
:long "markdown-converter")
(:name :styler
:description "Specify a command for converting raw HTML into styled output. Recommended: `srcweave-html-styler`."
:arg-parser #'parse-empty-arg-as-null
:long "styler")
(:name :force-output
:description "Ignore file modification dates and tangle all files. For tangle only."
:short #\f
:long "force-output")
(:name :no-trailing-newline
:description "Do not add a trailing newline to source files. For tangle only."
:long "no-trailing-newline"))
(defun start-command ()
(multiple-value-bind (options free-args)
(opts:get-opts)
(when (getf options :help)
(opts:describe
:prefix "Literate programming system. Write code to be read by humans, not machines."
:usage-of "srcweave"
:args "LITFILE ...")
(write-lines
""
"Styler:"
" A styler program accepts HTML from stdin and outputs to stdout. It will also receive the following env vars:"
" - LIT_TYPES: a space separated list of programming languages used in this file."
" - LIT_TITLE: the title of this document."
" - LIT_MATH: whether typeset math is desired for this file."
"")
(write-lines
"Markdown:"
" A markdown converter accepts markdown (with HTML fragments) from stdin and produces HTML to stdout."
" Gruber's perl implementation or discount are recommended choices."
"")
(write-line "Created by Justin Meiners (2022)")
(opts:exit 0))
(when (null free-args)
(error 'user-error :format-control "must provide at least one literate file"))
(when (and (not (getf options :weave))
(not (getf options :tangle)))
(warn "no tangle or weave command specified."))
(setf *markdown-command*
(getf options :md-compiler *markdown-command*))
(setf *styler-command*
(getf options :styler *styler-command*))
(setf *trailing-newline*
(getf options :trailing-newline *trailing-newline*))
(let ((file-defs (parse-lit-files free-args))
(ignore-dates (if (getf options :force-output) t nil))
(weave-path (getf options :weave))
(tangle-path (getf options :tangle)))
(when tangle-path
(format t "TANGLE~%")
(tangle (alexandria-2:mappend #'cdr file-defs)
tangle-path
:ignore-dates ignore-dates)
(format t "DONE~%"))
(when weave-path
(format t "WEAVE~%")
(weave file-defs
weave-path)
(format t "DONE~%")))))
(defun unknown-option (condition)
(warn "unknown option: ~a" (opts:option condition))
(invoke-restart 'opts:skip-option))
(defun toplevel ()
(handler-case
(handler-bind ((opts:unknown-option #'unknown-option))
(start-command))
(opts:missing-arg (condition)
(format *error-output* "option ~s needs an argument!~%"
(opts:option condition)))
(opts:arg-parser-failed (condition)
(format *error-output* "cannot parse ~s as argument of ~s~%"
(opts:raw-arg condition)
(opts:option condition)))
(opts:missing-required-option (con)
(format *error-output* "error: ~a~%" con))
(user-error (c)
(format *error-output* "error: ~a~%" c)
(opts:exit 2))))