Documentation version dev
Documentation
On this page

Project layout

Where code belongs in the mathconv repository, what each directory holds, and how the tables, packages and website are built.

You are reading the documentation of the development version on the dev branch, which is not released. The latest release is 0.1.0a4: this page in 0.1.0a4.

Where new code goes#

Paths in this section are relative to crates/mathconv/src/ in the engine crate, shown in the tree below. New code goes in the stage that can express it in its own terms:

  1. A rule about the syntax of a source format (an entity, a broken tag, a prefix) goes in that reader's parse step.
  2. A rule about what an element or construct of a source format means (a MathML attribute default, the meaning of mfenced) goes in that reader's lower step.
  3. A rule that normalises structure and can be stated in terms of the math tree alone (pairing fences, recognising function names) goes in rewrite/, as a rule of its own or as a change to an existing rule.
  4. A rule about how one target format spells something (which command, which braces, which spaces) goes in that writer.
  5. A capability the math tree lacks goes in tree/, and every reader and writer is updated in the same change.

Prefer the earliest stage that can state the rule and serve every case of it. A normalisation that holds for every source format goes in rewrite/, so that every reader benefits.

The MathML reader and both writers are split by element family, one file per family, so that changes to different families touch different files:

FamilyLowering, in read/mathml/lower/Writing, in write/latex/ and write/text/
tokenstoken.rs: tokenmod.rs: token, text, space
scriptsscript.rs: scriptscript.rs: script
under and overunder_over.rs: under_overunder_over.rs: under_over
fractionsfraction.rs: fractionfraction.rs: fraction
rootsroot.rs: rootroot.rs: root
fencesfence.rs: fencefence.rs: fence
tablestable.rs: tabletable.rs: table
style and the remaining elementsstyle.rs: stylestyle.rs: enclosure, phantom
content markupcontent/: content, with apply.rs, binding.rs, calculus.rs, constructors.rs and tokens.rsnone: it lowers to the nodes above

lower/mod.rs dispatches each element name to its family's function. write/mod.rs dispatches each node kind to its format's function and holds what the writers share: the walk, the spacing between items and the output.

Each function lives with the thing it serves: code shared by several stages lives in tree/ when it concerns the math tree, and otherwise in the module of the stage that owns it. There are no general-purpose modules such as utils or helpers.

All conversion logic lives in the engine. The Python binding, the type stubs and the Python tests follow its interface and change in the same commit as it.

The tree#

mathconv/
├── Cargo.toml                 Rust workspace: members, shared lints, test and release profiles
├── pyproject.toml             Python package metadata and build settings
├── README.md
├── CONTRIBUTING.md            how to report problems and contribute changes
├── RELEASING.md               how maintainers cut and publish a release
├── SECURITY.md                how to report a vulnerability privately
├── CHANGELOG.md               the release notes of every released version
├── changes/                   change fragments: the release notes of the next version
├── LICENSE                    MIT licence of mathconv
├── NOTICE                     notices of third-party material: unicode.xml data, the website's fonts and renderers
├── docs/                      the documentation: guide/ for users, internals/ for contributors
├── crates/
│   ├── mathconv/              the engine crate
│   │   ├── src/
│   │   │   ├── lib.rs         public interface: converter, formats, options, result
│   │   │   ├── workspace.rs   the workspace and its buffers
│   │   │   ├── ledger.rs      per-character accounting, enabled by the `ledger` feature
│   │   │   ├── tree/          the math tree: node kinds, storage, the only code that builds and walks it
│   │   │   ├── read/
│   │   │   │   └── mathml/    the MathML reader
│   │   │   │       ├── names.rs       element and attribute names
│   │   │   │       ├── parse.rs       parser and MathML tree
│   │   │   │       ├── annotation.rs  annotation selection and carried TeX
│   │   │   │       ├── operators.rs   the operator table of content markup
│   │   │   │       └── lower/         lowering into the math tree, one file per element family
│   │   │   ├── rewrite/       one file per rewrite rule; mod.rs holds the walker and lists the rules in running order
│   │   │   ├── write/         the shared walk and spacing
│   │   │   │   ├── latex/     the LaTeX writer, one file per element family
│   │   │   │   └── text/      the text writer, one file per element family
│   │   │   └── tables/        generated tables: symbols, styled letters, entities, operator dictionary
│   ├── mathconv-cases/        the test case format, read by the case runner and xtask
│   │   ├── src/
│   │   │   ├── lib.rs         the case format
│   │   │   └── checks.rs      the conversion checks, behind the `checks` feature
│   │   └── tests/
│   │       ├── cases.rs            the test case runner
│   │       └── arbitrary_input.rs  conversion checks on random, broken and long input
│   ├── mathconv-python/       the Python binding crate: Python and engine types, GIL handling
│   │   └── src/lib.rs
│   └── mathconv-wasm/         the WebAssembly binding crate and the npm package
│       ├── src/lib.rs         the exports that the package's JavaScript module calls
│       ├── npm/               the package's JavaScript module and Node.js entry, TypeScript declarations, README and package.json
│       └── tests/             tests of the installed package, run by node --test and tsc
├── python/
│   └── mathconv/
│       ├── __init__.py        re-exports the compiled module
│       ├── __init__.pyi       type stubs, changed with the interface they describe
│       └── py.typed           marks the package as typed
├── tests/
│   ├── cases/                 test cases: inputs with expected outputs, one TOML file each
│   └── python/                tests of the installed package
├── bench/                     the benchmark crate
│   ├── src/                   the driver that cargo xtask bench and gate run, and the formula loader
│   ├── benches/               the divan benchmarks that cargo bench and CodSpeed run
│   ├── python/                the pytest-codspeed benchmarks and the throughput script of the installed package
│   ├── js/                    the throughput script of the npm package
│   ├── tests/                 the allocation test and the manifest check
│   ├── formulas/              the benchmark set: formula files, manifest.toml, ATTRIBUTION.md
│   └── throughput.json        the throughput measurement that the README states
├── fuzz/                      the fuzz crate, a workspace of its own that cargo fuzz builds
│   ├── fuzz_targets/          one file per target: parse_and_convert.rs and structured.rs
│   ├── src/                   the grammar of the structured target and the check the targets share
│   └── mathml.dict            names and characters for the byte target
├── tools/
│   └── render-check/          Node.js program that runs KaTeX and MathJax on LaTeX and compares MathJax's drawings
│       ├── render-check.mjs   reads JSON Lines requests, answers each on standard output
│       ├── visual.mjs         flattens MathJax's SVG into primitives and pairs those of two drawings
│       ├── package.json       the pinned KaTeX and MathJax versions
│       └── package-lock.json
├── site/                      the website at mathconv.dev, generated by cargo xtask site
│   ├── engine-release         the tag of the release whose engine the site runs
│   ├── layout.html            the head, header and footer of every page
│   ├── pages/                 the home, playground, benchmarks and not-found pages
│   ├── conversions/           the content of the page of each conversion
│   ├── examples/              the example formulas, one MathML file each
│   ├── llms.txt               the summary for language models; the build adds the pages
│   └── assets/                files copied as they are: styles, scripts, fonts, logo and icons
├── xtask/                     project commands, run as cargo xtask <command>
└── .github/
    ├── npm-publish.sh         publishes the npm package to npm, or checks the command with --dry-run
    └── workflows/             continuous integration: checks, wheels and throughput, CodSpeed benchmarks, fuzzing, daily fuzzing and the website

Unit tests live in the Rust module they test. Each dependency of the engine crate must be fast, free of unsafe code in the parts used, and justified in the commit that adds it.

The commands in xtask/ are:

CommandDescription
checkruns every check listed in Contributing
renderchecks the LaTeX outputs of the test cases, of the style examples and of every combining mark with KaTeX and MathJax
blessrewrites the expected fields of the given test cases
benchmeasures conversion speed and instruction counts on the benchmark formulas, and names the command for the CodSpeed benchmarks
gatecompares instruction counts of the working tree with its base commit and with the last release; see the benchmark gate
throughputmeasures the formulas converted per second by the Rust core, the installed wheel and the npm package, as the throughput job does; with --readme, writes the speed sentence of the README from bench/throughput.json
evalconverts the pinned evaluation sample of the corpus, measures fallbacks, renderer acceptance and agreement with reference TeX, and writes the README coverage table; with --check, fails if the table is out of date; with --split, evaluates the whole development or test split; with --corpus, evaluates a local file or directory; with --report, also writes an HTML report of the outputs that look different from their reference
releasecuts a release on dev, as Releasing describes; with --next, sets the development version that follows it
fuzzruns the fuzz targets for a bounded time and fails on a finding; see Fuzzing
tablesregenerates the generated tables; with --check, fails if the committed tables differ
npmbuilds the npm package into target/npm, packs it and runs its tests against the tarball, as Packaging describes; with --release <tag>, prints the npm version of the release that the tag names and the dist-tag it is published under
sitebuilds the website into target/site with the engine of the release it runs, as Website describes; with --working-tree, with the engine of the working tree; with --serve, serves it on a local port

Generated tables#

The symbol table, the table of styled letters, the table of HTML entity names and the operator dictionary in tables/ are generated from unicode.xml, the W3C list of characters with their LaTeX names, published in the W3C xml-entities repository. They are never edited by hand; each starts with a comment naming the command that generates it.

cargo xtask tables downloads unicode.xml, identified in xtask/ by the commit of xml-entities it comes from and its SHA-256 checksum, and fails if the checksum differs. The file is not committed. Where it offers several commands for one character, the list of choices committed in xtask/ picks one, and the generator fails for a character that offers several accepted commands and has no choice. The choice is the command that authors type most, measured on a large sample of formulas, and the list records the counts. The list can also map a character to any LaTeX fragment that both renderers accept, such as {}^\circ, and a character that is written differently as an identifier to its own fragment. It maps a character to none when the character is written as text. A combining mark maps to its accent command on an empty group, as in \tilde{}. Only fragments that both KaTeX and MathJax accept on their own as a formula in math mode are kept. The generator finds them by querying the pinned renderers, through the render check, for every command that unicode.xml offers, and writes the result to xtask/tables/renderer-commands.txt; it also confirms that each known function name in Concepts is an operator command in both renderers. The mathematical alphanumeric symbols, such as 𝐱, are left out of the symbol table: the table of styled letters maps each to its plain letter and variant. The generator fails when the operator dictionary lists an operator twice in one form. The table of known function names, each with its control word, is generated from the list in the concepts document. A second committed list names the accents, which unicode.xml does not mark; the generator gives each of them the accent property in the operator dictionary. A third list names the punctuation marks, with the space after each; the generator gives them no space before, that space after and the separator property. The generated tables and the lists are committed, and a check confirms that running the generator again gives the same files. Offline, without a cached copy of unicode.xml, a local check reports this and is skipped; where the CI variable is set, it fails instead.

Licence and notices#

LICENSE holds the MIT licence of mathconv. NOTICE holds the W3C Software Notice and License that covers the data derived from unicode.xml and names its source. NOTICE and the generated tables change together, and every package ships both LICENSE and NOTICE. NOTICE also names the fonts of the website, with their SIL Open Font License, and the renderers it copies from the render check, whose licence files the build copies next to them; the website serves NOTICE as NOTICE.txt.

Packaging#

The binding crate uses PyO3, a Rust library for writing Python extension modules. It is built for abi3, the stable Python interface, so one compiled module serves every supported Python version. maturin builds the package: a wheel, the prebuilt package that pip installs without compiling, for each platform listed under Installation, and a source distribution for the others.

The version is set once, in [workspace.package] of the root Cargo.toml. The engine crate and the binding crate inherit it, and maturin gives it to the Python package, which writes the Rust pre-release 0.1.0-dev0 as 0.1.0.dev0 and 0.1.0-a1 as 0.1.0a1. cargo xtask release is the only thing that changes it. LICENSE and NOTICE go into every wheel and into the source distribution.

cargo xtask check builds the wheel with uv, installs it into a fresh environment, and runs the Python tests against it, so the tests exercise the package as users install it. It also compares the type stubs with the compiled module. The wheels workflow builds the wheels of every platform and the source distribution on each push to dev or main and on each pull request, installs each wheel on its own platform and runs the same tests. For a tag that starts with v, it first verifies the tag against the tree, and after the tests it uploads the files to PyPI through trusted publishing, which needs no stored token, and creates the GitHub Release. Releasing describes the release steps.

The npm package is the binding crate mathconv-wasm built for WebAssembly with the wasm profile of the workspace, which optimises for size. wasm-bindgen writes the module and its JavaScript glue, and wasm-opt -Oz from binaryen shrinks the module further when it is installed; CI always installs it, so published modules are optimised. The JavaScript module in crates/mathconv-wasm/npm/index.js is the public interface and the only caller of the glue. It encodes the inputs of a call as UTF-8 directly into one buffer in WebAssembly memory, and the binding returns all outputs of the call in one string with their lengths, so a batch crosses the boundary a fixed number of times and each string is copied once in each direction. Formats and options are parsed by name in the engine, by Converter::from_names, which both bindings and the test case runner call.

The exports map of package.json gives Node.js, Deno and other runtimes that follow the node export condition the entry node.js, which reads the module from disk and instantiates it synchronously on import; in Node.js, require loads the same ES module. Every other environment gets index.js, where init fetches the module from next to the glue, the address that bundlers and browsers resolve. The package's version is the version of the tree as a semantic version, 0.1.0-alpha.4 for 0.1.0a4, which cargo xtask npm writes into package.json and the module.

cargo xtask npm builds the package into target/npm/mathconv, packs it with npm pack, installs the tarball next to the tests in crates/mathconv-wasm/tests and runs them: the API under Node.js as an ES module and through require, the entry for browsers with explicit loading, the files and the manifest of the package, and the TypeScript declarations, which tsc checks against an example. The wheels workflow builds and tests the package with the current Node.js LTS release on every push and pull request, then tests the same tarball with every supported LTS release and, apart from the require test and the declarations, with Deno. For a tag, it publishes the tarball to npm through trusted publishing and attaches it to the GitHub Release. Started by hand on dev with a release tag, it publishes the tarball attached to that release instead, as Releasing describes.

Website#

The website at mathconv.dev is static HTML, CSS and JavaScript modules, generated by cargo xtask site from site/ with no framework and no Node.js build step. The generator is the module xtask/src/site/. Each page has a clean address ending in /, such as /playground/, and the addresses between pages are relative.

Every page gets its head, header and footer from site/layout.html: the title, the meta description, the canonical address, the Open Graph and Twitter card tags with the one social image, and the Content Security Policy. The pages are:

  • the home page, the playground, the benchmarks and the not-found page, written in site/pages/ with a metadata comment and {{name}} placeholders that the build fills;
  • one page for each conversion, such as /mathml-to-latex/. The conversions are the rows of the coverage table, and each needs a content file in site/conversions/, so a new reader or writer adds its page with one file. The build fills the worked examples, the Python calls and the measured figures by running the engine. These pages share the frame of the documentation: a side navigation, here of the conversions and the pages about the one shown, the text column and the outline;
  • the documentation, as Versioned documentation describes: docs/guide/NN-slug.md and docs/internals/NN-slug.md become /docs/guide/slug/ and /docs/internals/slug/, with the side navigation, an outline of the page, links to the previous and next page, and an index at /docs/. Markdown is rendered with pulldown-cmark, headings get the anchors GitHub gives them, and code is highlighted at build time.

The site runs a released engine, so that the playground converts as pip install mathconv and the npm package of that release do. site/engine-release names the release by its tag. The build extracts the tree of that tag into target/site-engine/, puts the working tree's binding crate crates/mathconv-wasm in place of the tag's, and builds the npm package there, as Packaging describes, into pkg/: a binding that does not fit the released engine fails the build. The pages import the package's index.js and convert through its public JavaScript API. cargo xtask site --working-tree builds the package from the working tree instead, for development. When the tree of the release holds the npm package, the pages also offer it: its install line, JavaScript usage on the home page and the conversion pages, and a link in the footer and in llms.txt.

Every output that a page shows, the examples of the conversion pages and the outputs of the Python calls, is converted at build time by the same module under Node.js, by xtask/src/site/convert.mjs, and LaTeX is drawn with KaTeX there too, so the pages read without JavaScript. The coverage table, the speed and the install line come from the README and bench/throughput.json of the released tree, so they describe the release the site runs, and the pages link to that release's documentation at /docs/. vendor/ holds KaTeX, MathJax and MathJax's font, copied from the render check's installed packages, so the site renders with the versions the render check pins and makes no request to another host. The build also writes sitemap.xml and llms.txt from the list of pages, and ends by checking that every address a page links to or loads from the site exists with the anchor it names, that the README's links to the site resolve, and that the sitemap lists every page that may be indexed.

The playground converts on every change in the browser. It shows the LaTeX output rendered by KaTeX or MathJax, in the mode the result's display flag gives, next to the browser's own drawing of the input. The home page and the conversion pages have a compact converter, which loads the WebAssembly module only when the input is edited. The input is drawn from a copy that site/assets/sanitize.js rebuilds: the input is parsed by DOMParser into an inert document, and only MathML elements from an allowlist, with attributes from an allowlist, and text are copied into the page; annotations and the raw-text elements of HTML are dropped. A Content Security Policy in every page allows scripts, styles, fonts and connections from the site's own origin only, as a second barrier, and KaTeX runs with trust: false and MathJax with its safe extension. The state of the playground, its input, options and renderer, travels in the URL fragment of /playground/, as JSON compressed with raw DEFLATE when that is shorter; the home page forwards a fragment of that form to the playground. Share copies such a link and puts it in the address bar, and the report button opens a GitHub issue with the input, the output, the options, the version and the link. Links stay under 8,000 characters: a longer input cannot be shared, and a report cuts the input and the output until its address fits.

Versioned documentation#

The documentation of every release is on the site. /docs/ shows that of the release the site runs, read from its tag; these addresses are stable, and the README, the package pages, llms.txt and the other pages of the site link to them. /docs/<version>/, such as /docs/0.1.0a4/guide/options/, is the documentation of each release, read from its tag, and /docs/dev/ that of the commit the site is built from. The releases are the v* tags up to the one the site runs whose tree has docs/guide/ and docs/internals/; older tags, from before that layout, are left out. A version starts with a digit and the sections are words other than dev, so the addresses cannot collide. cargo xtask site --working-tree renders only /docs/ and /docs/dev/, which needs only the tag that site/engine-release names.

A version menu, in the header and at the top of the documentation's navigation, lists the latest release, each older release and dev. It is a list of links in a details element, so it works without JavaScript. On a page of the documentation each link leads to the same page in that version when the version has it, and to the version's index otherwise; the build knows every version's pages, so no link is broken. Elsewhere the links lead to each version's index. A page of an older release or of dev says so at its top and links to its counterpart in the latest release.

Search engines see /docs/ as the documentation. A release's page names its counterpart in /docs/ as its canonical address, or itself when the latest release does not have the page. The sitemap lists the pages of /docs/ and the release pages that are their own canonical address, and /docs/dev/ is not indexed. The link at the end of a page edits its source on dev for /docs/ and /docs/dev/, and views it at the tag for older releases.

Every release's tree is rendered by the current generator, so the contract between the documentation and the generator stays small, and xtask/src/site/docs.rs states it: docs/ holds README.md and the section directories and nothing else; a page is NN-slug.md, starts with its # title and a summary paragraph and has no other # heading; and its links to pages, anchors and files resolve in the same tree, links to files leading to GitHub at the tag or commit. A change to the generator must keep every published release rendering: the build renders them all, and a release whose documentation breaks the contract fails it with the tag and the file named.

Publishing#

The site is released apart from the package: the pages workflow, started by hand on dev, builds it and deploys it to GitHub Pages, as Releasing describes. The check workflow builds it on every push and pull request as part of cargo xtask check.

Edit this page on GitHub