Skip to content

Render the formulas locally, the Wikimedia Math API is gone - #2847

Merged
BoboTiG merged 2 commits into
reader-dict:mainfrom
monosans:fix/local-formula-rendering
Nov 22, 2026
Merged

BoboTiG merged 2 commits into
reader-dict:mainfrom
monosans:fix/local-formula-rendering

Conversation

@monosans

@monosans monosans commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Wikimedia has shut down the Math API (Mathoid). It was deprecated in August and the endpoints were planned to be off after September (announcement, changelog). Now every POST /media/math/check/... gets an empty 403 from their HAProxy, on en.wikipedia.org and on wikimedia.org, whatever the User-Agent, while the other REST endpoints still answer. So every formula missing from svg.gz ends up as raw TeX in the dictionaries. In the RU dump of 2026-11-01 that's 37 errors, e.g. y = kx + b in "линейная функция" or d(x_{n}, x_{m}) < \varepsilon\ in "фундаментальная последовательность". A build I did on 2026-11-16 still had them as SVG.

This renders them locally with ziamath (pure Python, MIT, ships the STIX Two Math font):

  • sizes are in em, and vertical-align puts the baseline of the formula on the one of the text, like the Mathoid SVGs did with ex
  • display style, like Mathoid
  • plain paths with fill="currentColor": no <symbol> and no IDs, so several formulas in one article can't clash
  • the cache stays as is: cached formulas keep their Mathoid SVG, and new ones still go through svg.set() and the [new SVG] log

ziamath has no mhchem support, so <chem> goes through a small chem_to_tex() first. It covers the plain formulas: molecules, charges, bonds, hydrates, plus signs and arrows. Anything else (states like (l), braces, other arrows...) raises, so those formulas still only work from the cache and log the same error as before. I checked it against mhchemParser 4.2.1, the parser MathJax uses: the same TeX (minus its �phantom spacing) for the 172 chemical-looking formulas of svg.gz. The 3 <chem> errors of the RU dump ((CH3)2SO, (CH3)2S, Mg2+) render now.

The dead WIKIMEDIA_URL_MATH_* constants are removed, and the tests no longer mock the API.

While at it: scour ends every SVG with a newline, which shows as a space in the HTML, so a formula followed by a comma read "... formula , ..." (e.g. диметилсульфоксид). svg.optimize() strips it now.

Testing

  • Host: Windows 11, Docker Desktop 4.100.0, image python:3.13-slim with this branch installed from requirements.txt (ziamath 0.13, ziafont 0.11, latex2mathml 3.81.1)
  • Built the full RU dictionary from the 2026-11-01 dump: no <math> or <chem> errors left, [new SVG] x36 (33 math, 3 chem)
  • Compared the articles side by side in a browser with the 2026-11-16 build rendered through Mathoid (фундаментальная последовательность, линейная функция, напряжённость электрического поля, тета-ряд, единичная матрица), and chemical formulas from the cache next to the ziamath ones: same size and baseline, the glyphs are close. \big(...\big) comes out a bit taller than in MathJax
  • pytest tests/test_2_utils.py -k "formula or convert", the utils.py doctests, ruff format --check, ruff check and mypy pass

@BoboTiG

BoboTiG commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Thank you @monosans. That's a shame the chemical formulas will now be left behind. Maybe at some point, a Python module will come out, who knows.

ziamath has no mhchem support, so convert the plain formulas to TeX first:
molecules, charges, bonds, hydrates, plus signs and arrows. The others still
only work from the cache.
@monosans

Copy link
Copy Markdown
Contributor Author

I gave it a go anyway: most <chem> formulas are plain molecules like (CH3)2SO or Na2CO3.10H2O, so I added a small chem_to_tex() that turns those into TeX for ziamath (charges, bonds, hydrates, + and -> too). Anything fancier still raises and falls back to the cache like before. I checked it against mhchemParser, the parser MathJax uses, on the chemical formulas in svg.gz, and it gives the same TeX for all of them. The 3 RU ones render now. The description is updated.

@BoboTiG

BoboTiG commented Nov 22, 2026

Copy link
Copy Markdown
Member

Super cool!

@BoboTiG
BoboTiG merged commit 81771ef into reader-dict:main Nov 22, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants