Repository navigation
fix: render math as native MathML at scrape time - #2752
Open
ridge-kimani wants to merge 1 commit into
Open
ridge-kimani wants to merge 1 commit into
ridge-kimani wants to merge 1 commit into
Conversation
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the missing math rendering closes #1264 #750
Problem
Sphinx-based docs such as scikit-learn, NumPy, SciPy and statsmodels ship their equations as raw TeX inside
span.math/div.mathnodes and rely on MathJax to render them in the browser. MathJax never runs in DevDocs, so pages show\(...\)and\[...\]verbatim.Approach
Render the math to native MathML at scrape time, which is what the PyTorch pages already ship (their KaTeX output includes MathML, and the PyTorch filter kept it). MathML renders in every browser DevDocs supports without any runtime JavaScript or fonts, works offline, and follows the dark theme since it inherits the text color.
Docs::MathRenderer(lib/docs/core/math_renderer.rb) wraps KaTeX, run through ExecJS. It renders all the expressions of a page in a single JavaScript call, keeps the TeX source as an<annotation>, and falls back to showing the escaped source when KaTeX can't parse an expression. Bare underscores in\text{...}, which MathJax tolerates, are escaped so KaTeX accepts them.Docs::MathFilter(lib/docs/filters/core/math.rb) is a core filter, registered beforeclean_htmlso MathJax<script>sources survive. It only touches explicit markup: Sphinx/MathJax delimiters, MathJax 2script[type="math/tex"], MathJax 3mjx-container(keeps the assistive MathML) and pre-rendered KaTeX (re-rendered from its TeX source, so the KaTeX HTML/CSS are no longer needed). Existing<math>is left alone.$...$delimiters are opt-in per scraper viaoptions[:math_dollars], because dollar signs are too common in shell and PHP docs to be safe by default.clean_textno longer removes empty MathML nodes (mspace,mtext,mtd,mrow), and display math gets a margin in the stylesheet..katex-htmlremoval was dropped from the PyTorch filter.The
katexgem goes in thedocsBundler group, so the app itself doesn't load it. Scraping already requires a JavaScript runtime supported by ExecJS (see the README), so nothing new is needed to run it.Testing
bundle exec rake: 829 runs, 0 failures, including new tests for the renderer and the filter (every supported markup form, the$opt-in, the currency false-positive case, and the malformed-TeX fallback).Follow-ups
This PR adds the core filter and regenerates nothing by itself, to keep the review small. Regenerating scikit-learn and the other Sphinx docs (NumPy, SciPy, statsmodels, ...) can follow in separate PRs.
Screenshots
Example in scikit_learn
