I’m looking for a way to access the parsed representation of a Typst doc from Python. Specifically I want to inspect templates to identify what data they require for hydration. I expect this can be boiled down to locating the first function defined in the document and enumerating its parameters and their types.
I could write a custom parser for this, but it would be much more robust to use the AST produced by typst-syntax. Are there any Python libraries that expose the Typst AST?
I assume you have to write Python bindings for Rust shared library. Otherwise, you have to rewrite Rust code in Python… Some people that wanted to use Typst from PL usually just need the output, so a CLI as a subprocess is used.
Yes, that’s what’s needed. I’m trying to figure out who’s working on that in the Typst ecosystem.
typst-py is the most mature effort I’ve found. It has a query module which seems like it’ll do the trick, but there are no docs for it. I’ve opened an issue there.
Are there other folks working on this that I should be aware of? I’d rather contribute to an existing effort that go it alone.
I have used it and honestly not really noticed the lack of docs. It should be documented, but I’d take a look at Query Function – Typst Documentation. The typst-pyquery function mirrors typst query in the same way that compile mirrors typst compile.
Looks like query is specifically useful for retrieving locatable elements of the compiled document. Functions don’t appear to be locatable, so they can’t be retrieved by query. Functions might even be eliminated entirely by the time query sees the document?
It sounds like you are not interested in a compiled or laid out document, instead your focus is on the programming structure of the source file (I had to look up AST).
In the off chance it is useful to you (or others finding this), there are a couple of tools for having Typst compile a document and using the intermediate representation of that document before it gets its final layout.