size: 3 KiB

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