/
lunamark
executable file
·427 lines (344 loc) · 12.5 KB
/
lunamark
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/env lua
require("luarocks.loader")
-- lunamark program
--[===[
@startman
# NAME
lunamark - converts markdown to many formats
# SYNOPSIS
lunamark [options] [file..]
# DESCRIPTION
Lunamark is a lua library and command-line program for conversion of markdown
to other textual formats. Currently HTML, Docbook, ConTeXt, LaTeX, and Groff man
are the supported output formats, but it is easy to add new writers or modify
existing ones. The markdown parser is written using a PEG grammar and can also
be modified by the user.
Lunamark's markdown parser currently supports the following extensions (which
can be turned on or off individually):
- Smart typography (fancy quotes, dashes, ellipses)
- Significant start numbers in ordered lists
- Footnotes
- Definition lists
More extensions will be supported in later versions.
The library is as portable as lua and has very good performance.
It is slightly faster than the author's own C library
[peg-markdown](http://github.com/jgm/peg-markdown).
# OPTIONS
`--to,-t` *format*
: Specify format for output.
*format* can be `html`, `html5`, `dzslides`, `docbook`, `latex`, `context`, or `man`.
`--layout,-l` *layout*
: Control whitespace in output.
*layout* can be `default` (blank lines between block-level elements),
`compact` (avoid unnecessary blank lines), or `minimize` (avoid
all unnecessary space).
`--extensions,-X` *extensions*
: Use the specified syntax extensions in parsing markdown.
*extensions* is a comma-separated list of extensions, each optionally
prefixed by `+` (enable) or `-` (disable). See EXTENSIONS, below, for
a list of supported extensions. The keyword 'all' may also be used,
to set all extensions simultaneously.
`--output,-o` *file*
: Write output to *file*.
`--standalone,-s`
: Add header and footer to the document, so that it is a functional
standalone document, not a fragment. Use the default template
for the writer, unless `--template,-T` is used.
`--template,-T` *file*
: Insert converted text and metadata into a template. See TEMPLATES,
below, for template format. Implies `--standalone,-s`.
`--data,-d` *key=value[,key=value..]*
: Set metadata fields to be passed to template. Argument is a list
of *key=value* pairs, separated by commas. If keys are repeated,
an array value will be formed.
`--strict,-0`
: Disable all markdown extensions.
`--version,-V`
: Print version information.
`--help,-h`
: This message
# EXTENSIONS
The following extensions are defined, with the default setting given in parentheses:
`(-) containers`
: Put sections in containers (`div` tags for `html` writer,
`section` tags for `html5` or `dzslides`).
`(-) slides`
: Like `containers`, but do not allow containers to nest.
The container for a section will end before the container for
a subsection begins. This is usually what is wanted
for HTML5 slide shows.
`(-) startnum`
: Start number of an ordered list is significant. In standard
markdown, the starting number is irrelevant. If this
extension is selected, the starting number determines
the starting number of the list in the output. (The
subsequent numbers are ignored, as in standard markdown.)
`(-) smart`
: Smart typography. Straight quotes are turned into curly
quotes, `--` into en dashes, `---` into em dashes, `...`
into an ellipsis character.
`(-) preserve_tabs`
: Don't expand tabs to spaces. Standard markdown expands all
tabs, using a tabstop of 4 spaces. This extension allows
tabs to be preserved in literal code contexts.
`(-) notes`
: Footnotes. A footnote marker is like a markdown bracketed
reference, but beginning with a circumflex (`^`). The note
itself goes in a separate block, which begins with the marker,
followed by a colon and space, and ends with a blank line.
The note can contain multiple paragraphs or other block elements,
but each block must be indented four spaces. Example:
Here is a note.[^mynote]
[^mynote]: This note has two paragraphs.
Here is the second one.
`(-) definition_lists`
: Definition lists. A list item consists of a term, on a single
line, followed by one or more definitions. Each definition
begins with a colon (`:`), but is otherwise indented four spaces.
A definition may contain multiple block elements. The colon
need not be flush-left, but may be preceded by one or two spaces.
Alternatively, a tilde (`~`) may be used instead of a colon.
Example:
rake
: a tool for gathering leaves.
(paragraph two of definition one.)
: a ruby build system.
# TEMPLATES
By default, lunamark will produce a fragment. If the
`--standalone` or `--template` options are specified, it will insert
this fragment into a template, producing a standalone document with
appropriate header and footer. `--standalone` uses the default template
built into the writer, while `--template` specifies a custom template,
which is sought first in the working directory, then in
`templates`, and finally in `$HOME/lunamark/templates`. If no
extension is given, the name of the writer will be used as an
extension. So, for example, one can put the template `letter.html`
in the `$HOME?lunamark/templates` directory, and use it anywhere
with `lunamark --template letter`.
The templates are [cosmo](http://cosmo.luaforge.net/) templates.
Conditionals are enabled, so you can use the `$if` keyword
as follows:
$if{ #people == 1 }[[There is only one.]][[There are many.]]
A `sepby` keyword is also enabled:
$sepby{ people }[[$it]][[ and ]]
will render "`Sid`" if `people == {"Sid"}` and
"`Sid and Nancy`" if `people == {"Sid","Nancy"}`.
The following variables are set by default; others may be set
by the reader (if metadata extensions are used) or through
the `--data` option:
`body`
: the fragment converted from markdown
`sources`
: array of the source files specified on the command line
`timestamp`
: the current time
# EXAMPLES
lunamark
acts as a filter, reading markdown from stdin and writing
HTML to stdout.
lunamark -Xsmart,definition_lists -t latex
acts as a filter, reading markdown with smart typography
and definition list extensions from stdin, and writing
LaTeX to stdout.
lunamark -t latex -s -o mybook.tex ch{1,2,3}.txt references.txt
reads `ch1.txt`, `ch2.txt`, `ch3.txt`, and `references.txt`,
concatenates them, and converts the result from markdown to LaTeX,
using the default template and saving the result as `mybook.tex`.
lunamark -Xall --template letter -d cc=Smith,cc="Jim Jones",sign="yes" \
-t context -o myletter.ctx myletter.txt
produces a ConTeXt file using the template `letter.context`,
and setting the variable `cc` to `{"Smith","Jim Jones"}`
and `sign` to `"yes"`. All lunamark etensions are enabled.
# AUTHORS
Most of lunamark is written by John MacFarlane. Hans Hagen
made some major performance improvements to the markdown
parser. Khaled Hosny added the original ConTeXt writer.
@stopman
--]===]
local lunamark = require("lunamark")
local alt_getopt = require("alt_getopt")
local cosmo = require("cosmo")
local function ensure_one_of(optval,s,ary)
for i=1,#ary do
if ary[i]==s then return true end
end
lunamark.util.err("Illegal value for " .. optval ..
"\nLegal values are: " .. table.concat(ary,", "))
end
local version = [[
lunamark 0.2
Copyright (C) 2009-2011 John MacFarlane
]]
local usage = [[
Usage: lunamark [options] [file..] - convert markdown to other formats
Options:
--to,-t FORMAT Target format
--layout,-l LAYOUT Whitespace in output (default|compact|minimize)
--extensions,-X EXTENSIONS Syntax extensions to use
--output,-o FILE Output file
--standalone,-s Add header and footer
--template,-T FILE Insert output into template
--data,-d K=V[,K=V..] Set metadata to be passed to template
--strict,-0 Disable markdown extensions
--version,-V Version information
--help,-h This message
FORMAT can be html, html5, docbook, latex, context, or man.
LAYOUT can be default, compact (no unnecessary blank lines), or
minimize (no unnecessary blank space).
EXTENSIONS is a comma-separated list of extensions, each optionally prefixed
by + (enable) or - (disable). The following extensions are defined,
with the default setting given in parentheses:
(-) containers Put sections in containers (e.g. div or section tags)
(-) slides Like containers, but do not nest them
(-) startnum Start number of an ordered list is significant
(-) smart Smart typography (quotes, dashes, ellipses)
(-) preserve_tabs Don't expand tabs to spaces
(-) notes Footnotes
(-) definition_lists Definition lists
The keyword 'all' may also be used, to set all extensions simultaneously.
]]
local long_opts = {
to = "t",
layout = "l",
extensions = "X",
output = "o",
standalone = "s",
template = "T",
data = "d",
strict = "0",
version = "V",
help = "h"
}
local short_opts = "t:l:X:o:sT:d:0Vh"
local optarg,optind = alt_getopt.get_opts(arg, short_opts, long_opts)
if optarg.h then
io.write(usage)
os.exit(0)
end
if optarg.V then
io.write(version)
os.exit(0)
end
local to = optarg.t or "html"
ensure_one_of("--to,-t", to,
{"markdown","html","html5","dzslides","docbook","latex","context","man"})
local reader = lunamark.reader.markdown
local extensions = { -- defaults
containers = false,
slides = false,
startnum = false,
smart = false,
preserve_tabs = false,
notes = false,
definition_lists = false,
}
if optarg["0"] then
extensions = {}
end
local extensions_opt = optarg.X or ""
for x in extensions_opt:gmatch("[%+%-]?[%a_]+") do
local val = true
if x:sub(1,1) == "+" then
val = true
x = x:sub(2)
elseif x:sub(1,1) == "-" then
val = false
x = x:sub(2)
end
if x == "all" then
for k,_ in pairs(extensions) do
extensions[k] = val
end
elseif type(extensions[x]) ~= "boolean" then
lunamark.util.err("Unrecognized extension: ", x)
else
extensions[x] = val
end
end
local output = optarg.o
local ok, msg = pcall(function() io.output(output) end)
if not ok then
lunamark.util.err("Could not open '" .. output .. "' for writing.\n" .. msg, 9)
end
local writer_options = extensions
local layout = optarg["l"] or "default"
ensure_one_of("--layout,-l",layout,{"default","compact","minimize"})
writer_options.layout = layout
local writer = lunamark.writer[to].new(writer_options)
if not writer then
lunamark.util.err("Unknown writer: " .. tostring(to), 5)
end
local reader_options = extensions
local parse = reader.new(writer, reader_options)
local args = {}
for i=optind,#arg do
table.insert(args,arg[i])
end
local inp
if #args == 0 then
inp = io.read("*all") .. "\n"
else
inpt = {}
for _,f in ipairs(args) do
local ok, msg = pcall(function() io.input(f) end)
if ok then
table.insert(inpt, io.read("*all"))
else
lunamark.util.err("Could not open file '" .. f .. "'", 7)
end
end
inp = table.concat(inpt, "\n") .. "\n"
end
if inp:find("\r",1,true) then
inp = inp:gsub("\r\n","\n") -- convert DOS line endings
end
local body, metadata = parse(inp)
local standalone = optarg.s
local template = optarg.T
local template_contents
if standalone and not template then
template_contents = writer.template
end
if template then
local template_name = template
-- use writer name as extension if none provided
if not template_name:match("%..+$") then
template_name = template_name .. "." .. to
end
template_contents = lunamark.util.find_template(template_name)
if not template_contents then
lunamark.util.err("Could not find template '" .. template_name .. "'")
end
end
if template_contents then
local data = metadata or {}
data.timestamp = os.date("%Y-%m-%d %X")
data.sources = args
local keys = {}
if optarg.d then
for x in string.gmatch(optarg.d, "[%w_]+=[^,]+") do
local k,v = string.match(x, "([^=]*)=(.*)")
if keys[k] then
if keys[k] == "string" then
data[k] = {data[k], v}
elseif keys[k] == "array" then
data[k] = table.insert(data[k],v)
end
keys[k] = "array"
else
data[k] = writer.string(v)
keys[k] = "string"
end
end
end
data.body = body
data["if"] = cosmo.cif -- this activates the "if" keyword
data.sepby = lunamark.util.sepby -- actives "sepby" keyword
local result = cosmo.fill(template_contents, data)
io.write(result)
else
io.write(body)
end
if not template_contents then
io.write("\n")
end