size: 4 KiB

1.\" Automatically generated by Pandoc 2.19.2
2.\"
3.\" Define V font for inline verbatim, using C font in formats
4.\" that render this, and otherwise B font.
5.ie "\f[CB]x\f[]"x" \{\
6. ftr V B
7. ftr VI BI
8. ftr VB B
9. ftr VBI BI
10.\}
11.el \{\
12. ftr V CR
13. ftr VI CI
14. ftr VB CB
15. ftr VBI CBI
16.\}
17.TH "DJOT" "1" "" "djot 0.2.1" ""
18.hy
19.SH NAME
20.PP
21djot \[en] converts djot markup.
22.SH SYNOPSIS
23.PP
24djot options [file..]
25.SH DESCRIPTION
26.PP
27djot is a command-line parser for djot markup (https://djot.net).
28It can produce
29.IP \[bu] 2
30an HMTL document (default behavior)
31.IP \[bu] 2
32a stream of annotated tokens with byte offsets (\f[V]--matches\f[R])
33.IP \[bu] 2
34an AST in either human-readable or JSON form (\f[V]--ast\f[R]).
35.SH OPTIONS
36.TP
37\f[V]--matches, -m\f[R]
38Show matches (annotated tokens with source positions).
39.TP
40\f[V]--ast\f[R], \f[V]-a\f[R]
41Produce and render an abstract syntax tree.
42.TP
43\f[V]--json\f[R], \f[V]-j\f[R]
44Use machine-readable JSON format when used with \f[V]--matches\f[R] or
45\f[V]--ast\f[R].
46.TP
47\f[V]--sourcepos\f[R], \f[V]-p\f[R]
48Include source positions in the AST or HTML document.
49.TP
50\f[V]--filter\f[R] \f[I]FILE\f[R], \f[V]-f\f[R] \f[I]FILE\f[R]
51Run the filter defined in \f[I]FILE\f[R] on the AST between parsing and
52rendering.
53The \f[V]--filter\f[R] option may be used multiple times; filters will
54be applied in the order specified on the command line.
55See FILTERS below for a description of filters.
56.TP
57\f[V]--verbose\f[R], \f[V]-v\f[R]
58Verbose output, including warnings.
59.TP
60\f[V]--version\f[R]
61Print the djot version.
62.TP
63\f[V]--help\f[R], \f[V]-h\f[R]
64Print usage information.
65.SH FILTERS
66.PP
67Filters are small Lua programs that modify the parsed document prior to
68rendering.
69Here is an example of a filter that capitalizes all the content text in
70a document:
71.IP
72.nf
73\f[C]
74return {
75 str = function(e)
76 e.text = e.text:upper()
77 end
78}
79\f[R]
80.fi
81.PP
82Save this as \f[V]caps.lua\f[R] use tell djot to use it using
83.IP
84.nf
85\f[C]
86djot --filter caps input.djot
87\f[R]
88.fi
89.PP
90Note that djot will search your LUA_PATH for the filter if it is not
91found in the working directory, so you can in principle install filters
92using luarocks.
93.PP
94Here\[cq]s a filter that prints a list of all the URLs you link to in a
95document.
96This filter doesn\[cq]t alter the document at all; it just prints the
97list to stderr.
98.IP
99.nf
100\f[C]
101return {
102 link = function(el)
103 io.stderr:write(el.destination .. \[dq]\[rs]n\[dq])
104 end
105}
106\f[R]
107.fi
108.PP
109A filter walks the document\[cq]s abstract syntax tree, applying
110functions to like-tagged nodes, so you will want to get familiar with
111how djot\[cq]s AST is designed.
112The easiest way to do this is to use \f[V]djot --ast\f[R].
113.PP
114By default filters do a bottom-up traversal; that is, the filter for a
115node is run after its children have been processed.
116It is possible to do a top-down travel, though, and even to run separate
117actions on entering a node (before processing the children) and on
118exiting (after processing the children).
119To do this, associate the node\[cq]s tag with a table containing
120\f[V]enter\f[R] and/or \f[V]exit\f[R] functions.
121The following filter will capitalize text that is nested inside
122emphasis, but not other text:
123.IP
124.nf
125\f[C]
126local capitalize = 0
127return {
128 emph = {
129 enter = function(e)
130 capitalize = capitalize + 1
131 end,
132 exit = function(e)
133 capitalize = capitalize - 1
134 end,
135 },
136 str = function(e)
137 if capitalize > 0 then
138 e.text = e.text:upper()
139 end
140 end
141}
142\f[R]
143.fi
144.PP
145For a top-down traversal, you\[cq]d just use the \f[V]enter\f[R]
146functions.
147If the tag is associated directly with a function, as in the first
148example above, it is treated as an \[ga]exit\[cq] function.
149.PP
150It is possible to inhibit traversal into the children of a node, by
151having the \f[V]enter\f[R] function return the value true (or any truish
152value, say \f[V]\[dq]stop\[dq]\f[R]).
153This can be used, for example, to prevent the contents of a footnote
154from being processed:
155.IP
156.nf
157\f[C]
158return {
159 footnote = {
160 enter = function(e)
161 return true
162 end
163 }
164}
165\f[R]
166.fi
167.PP
168A single filter may return a table with multiple tables, which will be
169applied sequentially.
170.SH AUTHORS
171.PP
172John MacFarlane (<jgm@berkeley.edu>).