size: 3 KiB
| 1 | # NAME |
| 2 | |
| 3 | djot -- converts djot markup. |
| 4 | |
| 5 | # SYNOPSIS |
| 6 | |
| 7 | djot [options] [file..] |
| 8 | |
| 9 | # DESCRIPTION |
| 10 | |
| 11 | djot is a command-line parser for [djot markup](https://djot.net). |
| 12 | It 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 | |
| 59 | Filters are small Lua programs that modify the parsed document |
| 60 | prior to rendering. Here is an example of a filter that |
| 61 | capitalizes all the content text in a document: |
| 62 | |
| 63 | ``` |
| 64 | return { |
| 65 | str = function(e) |
| 66 | e.text = e.text:upper() |
| 67 | end |
| 68 | } |
| 69 | ``` |
| 70 | |
| 71 | Save this as `caps.lua` use tell djot to use it using |
| 72 | |
| 73 | ``` |
| 74 | djot --filter caps input.djot |
| 75 | ``` |
| 76 | |
| 77 | Note that djot will search your LUA_PATH for the filter if |
| 78 | it is not found in the working directory, so you can in |
| 79 | principle install filters using luarocks. |
| 80 | |
| 81 | Here's a filter that prints a list of all the URLs you |
| 82 | link to in a document. This filter doesn't alter the |
| 83 | document at all; it just prints the list to stderr. |
| 84 | |
| 85 | ``` |
| 86 | return { |
| 87 | link = function(el) |
| 88 | io.stderr:write(el.destination .. "\n") |
| 89 | end |
| 90 | } |
| 91 | ``` |
| 92 | |
| 93 | A filter walks the document's abstract syntax tree, applying |
| 94 | functions to like-tagged nodes, so you will want to get familiar |
| 95 | with how djot's AST is designed. The easiest way to do this is |
| 96 | to use `djot --ast`. |
| 97 | |
| 98 | By default filters do a bottom-up traversal; that is, the |
| 99 | filter for a node is run after its children have been processed. |
| 100 | It is possible to do a top-down travel, though, and even |
| 101 | to run separate actions on entering a node (before processing the |
| 102 | children) and on exiting (after processing the children). To do |
| 103 | this, associate the node's tag with a table containing `enter` and/or |
| 104 | `exit` functions. The following filter will capitalize text |
| 105 | that is nested inside emphasis, but not other text: |
| 106 | |
| 107 | ``` |
| 108 | local capitalize = 0 |
| 109 | return { |
| 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 | |
| 126 | For a top-down traversal, you'd just use the `enter` functions. |
| 127 | If the tag is associated directly with a function, as in the |
| 128 | first example above, it is treated as an `exit' function. |
| 129 | |
| 130 | It is possible to inhibit traversal into the children of a node, |
| 131 | by having the `enter` function return the value true (or any truish |
| 132 | value, say `"stop"`). This can be used, for example, to prevent |
| 133 | the contents of a footnote from being processed: |
| 134 | |
| 135 | ``` |
| 136 | return { |
| 137 | footnote = { |
| 138 | enter = function(e) |
| 139 | return true |
| 140 | end |
| 141 | } |
| 142 | } |
| 143 | ``` |
| 144 | |
| 145 | A single filter may return a table with multiple tables, which will be |
| 146 | applied sequentially. |
| 147 | |
| 148 | # AUTHORS |
| 149 | |
| 150 | John MacFarlane (<jgm@berkeley.edu>). |
| 151 |