To add a bit more about my design choice in igk: One of the things that makes La...

@david_chisnall@infosec.exchange

To add a bit more about my design choice in igk:

One of the things that makes LaTeX hard to deal with is that, although it looks like a semantic markup language, it's actually a pile of macros on top of TeX, which is an imperative language for driving a typesetter. This means that a LaTeX line like \chapter{This is a section} is not actually semantic markup like an HTML <h2>, it's a command that executes and ships a new page, renders the title, and writes a line to another file that will allow the next run of [pdf]latex to fill in the ToC entry.

This is fine if you just want to use the commands that LaTeX defines but it's awkward if you want to define your own ones because you're not just defining new node types in something like an XML / HTML DOM, you're defining small subroutines that run as soon as your command is processed. What if your command depends on some other command that runs later? You need to redefine that command to store the data in a separate file, then have your command emit a warning if that file doesn't exist, and the second command needs to register something that runs at the end of the document parsing to warn the user if the contents of the other file has changed.

This is all horrible.

AsciiDoctor isn't anywhere near as bad.

But contrast this with a DocBook (XML or SGML) flow. There is a parsing step that reads the XML and builds a DOM. The DOM is exactly the same shape as the DocBook source. You then run a bunch of XSLT things to transform it into other XML, such as XHTML that has some generic semantic markup and replaces the document-specific semantic markup with either divs, spans, or longer sequences, or something like SILE's XML for generating PDFs. This is a nice abstract model, but has two problems:

  • No one wants to write XML without an XML editor
  • No one wants to use the few XML editors that exist.
  • Neither of the people who understand how XSLT is supposed to work want to admit it.

AsciiDoctor is somewhere in the middle. The input text has a load of things where the parser does transforms into sequences of nodes. These transforms are neither documented nor are they obvious. There are then a sequence of tree-to-tree transforms but, if you want to specify one, you can't state where it goes and so you must keep the tree containing exactly the same lowering.

For igk, I followed an XML/SGML-like model, but with a syntax that was designed to be easy for humans to write (more or less the same as SILE's TeX-like syntax).

Every node that you write in the original input is reproduced exactly in the generated tree, any whitespace is also preserved. Then you run tree-to-tree transforms. All processing happens in that layer. The exact order of the transform passes is defined by the user. If you want to transform something bespoke into something generic that's later processed by another pass , make sure that your pass runs before that one. That's it.

Adding a new node type doesn't require any parser modification or special macro expansion because there's no special treatment of any node. You want to create a new node called MySpecialNode? Write \MySpecialNode{...} in your source with whatever you want to be children of that node (text or other nodes) in the ellipsis. Add attributes in [] before the { if you want to provide key-value pairs. The tool doesn't care because the parsing phase is just producing a tree.

Some of the things that SILE treats as special are handled this way. SILE has some syntactic sugar where \begin{foo}...\end{foo} is equivalent to \foo{...}. In igk, this is just a tree transform that recursively visits nodes and, when it finds a \begin node, collects every node between it and a corresponding \end node and moves them to being children of the new new node named from the body of the \begin and \end nodes. \include is initially just parsed as a node, but typically the first pass that you open the files given as the body of an \include node, instruct the parser to parse them, and then replace the \include node with the result. Want to make that work with some special nodes for defining paths relative to something else? Just run the pass that expands them earlier.