size: 3 KiB

1# Djot.lua
2
3[![GitHub
4CI](https://github.com/jgm/djot.lua/workflows/CI%20tests/badge.svg)](https://github.com/jgm/djot.lua/actions)
5
6This repository contains a Lua parser for
7[djot](https://djot.net), a light markup syntax.
8
9Despite being written in an interpreted language, this
10implementation is very fast (converting a 260K test document in
11141 ms on an M1 mac using the standard `lua` interpreter). It
12can produce an AST, rendered HTML, or a stream of match tokens
13that identify elements by source position, which could be used
14for syntax highlighting or a linting tool.
15
16We also provide a custom pandoc writer for djot (`djot-writer.lua`),
17so that documents in other formats can be converted to djot
18format, and a custom pandoc reader (`djot-reader.lua`), so that
19djot documents can be converted to any format pandoc supports.
20To use these, just put them in your working directory and use
21`pandoc -f djot-reader.lua` to convert from djot, and `pandoc -t
22djot-writer.lua` to convert to djot. You'll need pandoc version
232.18 or higher, and you'll need the djot library to be installed
24in your `LUA_PATH`; see [Installing](#installing), below. If
25you're using the dev version of djot or don't want to worry
26about the djot library being installed, you can create
27self-contained versions of the custom reader and writer
28using the `amalg` tool:
29
30 luarocks install amalg
31 make djot-reader.amalg.lua
32 make djot-writer.amalg.lua
33
34These can be moved anywhere and do not require any Lua libraries
35to be installed.
36
37## Installing
38
39To install djot using [luarocks](https://luarocks.org), just
40
41```
42luarocks install djot
43```
44
45This will install both the library and the executable `djot`.
46
47## Using the Lua library
48
49### Quick start
50
51If you just want to parse some input and produce HTML:
52
53``` lua
54local djot = require("djot")
55local input = "This is *djot*"
56local doc = djot.parse(input)
57local html = djot.render_html(doc)
58```
59
60The AST is available as a Lua table, `doc.ast`.
61
62To render the AST:
63
64``` lua
65local rendered = djot.render_ast_pretty(doc)
66```
67
68Or as JSON:
69
70``` lua
71local rendered = djot.render_ast_json(doc)
72```
73
74To alter the AST with a filter:
75
76``` lua
77local src = "return { str = function(e) e.text = e.text:upper() end }"
78local filter = djot.filter.load_filter(src)
79djot.filter.apply_filter(doc, filter)
80```
81
82For a streaming parser:
83
84``` lua
85for startpos, endpos, annotation in djot.parse_events("*hello there*") do
86 print(startpos, endpos, annotation)
87end
88```
89
90(This will print start and end byte offsets into the input
91for annotated tokens.)
92
93## The code
94
95The code for djot (excluding the test suite) is standard Lua,
96compatible with lua 5.1--5.4 and luajit. Djot has no external
97dependencies. You can run it without installing it using
98`./run.sh`.
99
100`make install` will build the rockspec and install the
101library and executable using luarocks. Once installed,
102the library can be used by Lua programs, and the executable can
103be run using `djot`. `djot -h` will give help output.
104
105If you can't assume that lua or luajit will be installed on
106the target machine, you can use `make djot` in the `clib`
107directory to create a portable binary that bakes in a lua
108interpreter and the necessary scripts.
109
110`make test` will run the tests, and `make testall` will also
111run some tests of pathological cases.
112
113## License
114
115The code and documentation are released under the MIT license.
116