LaTeX.js icon indicating copy to clipboard operation
LaTeX.js copied to clipboard

Document AST format

Open mgreenbe opened this issue 8 years ago • 1 comments

Great tool!

I would like the option to output into formats other than HTML, or maybe even into HTML with a different mapping from latex environments to HTML tags. Any you could publish a summary of the AST format produced by the PEG.js parser, so others (like me) can write associated transformers?

mgreenbe avatar Dec 19 '17 20:12 mgreenbe

Hi, good question! This is fun :) I don't use a classic AST yet, although you could say that the DOM tree serves as the AST. The PEG.js actions directly create the DOM nodes.

But a few days ago I realized that this is a bit of a limitation: it seems impossible to implement conditionals without an AST, because if the condition is false, you'd have to continue after the next \else or \fi. But how do you know where the proper one is without having parsed all of the following code? And if I parse it, the actions will execute - PEG.js doesn't allow to disable them temporarily. So an AST is the only option. I'm not planning to implement conditionals for quite a while, so at the moment the code will have to do without a proper intermediate AST.

However, if you want, you can still easily change the output format! Just have a look at the HtmlGenerator class, line 139ff. There you can alter the tags and attributes. It's not perfect because children are added with Node.appendChild(), so it cannot be a hierarchy of tags (yet) without changing those members to a function - which I did it for link already.

If you want a completely different output than HTML or semistructured data, you'll have to implement a class like HtmlGenerator with the same interface but something else as output. For plain text, you could copy all the symbols, for instance. But I am not yet finished with the interface (the generator API), so things may change in the future.

michael-brade avatar Dec 20 '17 19:12 michael-brade