Project layout
Where code belongs in the mathconv repository, what each directory holds, and how the tables, packages and website are built.
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:
- A rule about the syntax of a source format (an entity, a broken tag, a prefix) goes in that reader's parse step.
- 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. - 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. - A rule about how one target format spells something (which command, which braces, which spaces) goes in that writer.
- 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:
| Family | Lowering, in read/mathml/lower/ | Writing, in write/latex/ and write/text/ |
|---|---|---|
| tokens | token.rs: token | mod.rs: token, text, space |
| scripts | script.rs: script | script.rs: script |
| under and over | under_over.rs: under_over | under_over.rs: under_over |
| fractions | fraction.rs: fraction | fraction.rs: fraction |
| roots | root.rs: root | root.rs: root |
| fences | fence.rs: fence | fence.rs: fence |
| tables | table.rs: table | table.rs: table |
| style and the remaining elements | style.rs: style | style.rs: enclosure, phantom |
| content markup | content/: content, with apply.rs, binding.rs, calculus.rs, constructors.rs and tokens.rs | none: 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/
└── 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:
| Command | Description |
|---|---|
check | runs every check listed in Contributing |
render | checks the LaTeX outputs of the test cases, of the style examples and of every combining mark with KaTeX and MathJax |
bless | rewrites the expected fields of the given test cases |
bench | measures conversion speed and instruction counts on the benchmark formulas, and names the command for the CodSpeed benchmarks |
gate | compares instruction counts of the working tree with its base commit and with the last release; see the benchmark gate |
throughput | measures 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 |
eval | converts 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 |
release | cuts a release on dev, as Releasing describes; with --next, sets the development version that follows it |
fuzz | runs the fuzz targets for a bounded time and fails on a finding; see Fuzzing |
tables | regenerates the generated tables; with --check, fails if the committed tables differ |
npm | builds the npm package into target/npm, packs it and runs its tests against the tarball, as Packaging describes; with --dist-tag, prints the dist-tag the version of the tree is published under |
site | builds 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.
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 insite/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; - the documentation:
docs/guide/NN-slug.mdanddocs/internals/NN-slug.mdbecome/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/. A page's first line is its#title and its first paragraph a summary, which is also the meta description. Markdown is rendered with pulldown-cmark, headings get the anchors GitHub gives them, code is highlighted at build time, links between pages are rewritten to their addresses and links to other files of the repository to GitHub at the built commit. A page that breaks these rules fails the build.
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. Until then, the pages
leave it out, and the documentation page about the npm package says
that the release lacks it.
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; the documentation comes from the working tree.
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 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.
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.