2023-05-22

Pandoc IPYNB to TYPST

Pandoc is a Haskell library that enables conversion between different markup formats. It includes a collection of readers that analyze text in a specific format and generate an internal representation (native) of the document, known as an abstract syntax tree (AST). The first part of the video below shows the AST obtained from a simple Jupyter Notebook.

Pandoc new version 3.1.2 includes a writer for the Typst format.

Between the reader and the writer a filter can be applied to transform the JSON serialized AST. Filters can be created in Python as discussed below.

Panflute-Pannb filter and Templates

The library Panflute defines the corresponding Python objects for Pandoc's AST elements (RawBlock,Header, ...).

Pannb is a Pandoc filter for Jupyter Notebooks based on Panflute, a single file filter can be downloaded from my dotfiles repository as follows:

   curl https://gitlab.com/Sevepy/.dotfiles/-/raw/master/.pandoc/filters/pannb.py -o pannb.py

The full command that generates the Typst source code from the Jupyter notebook used in this example, and which also includes a custom template for the generated document, is:

   pandoc.exe -t typst -F pannb.py --template=https://tessarinseve.pythonanywhere.com/staticpandoc/a4.typ --extract-media ./media "Pandoc IPYNB to Typst.ipynb" -o ipynb.typ

The output PDF document can be finally obtained with:

   typst.exe compile ipynb.typ