File size: 2,862 Bytes
93e3ef1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
"""Extract MathML from the MathJax test-suite snapshots (Apache-2.0).

Usage: mathjax_tests.py --work DIR --out DIR

Makes a shallow sparse clone of github.com/mathjax/MathJax-src limited to
`testsuite/` and `LICENSE` (into `DIR/mathjax-src` if absent).  The jest
snapshot files (`*.snap`) hold MathML produced by MathJax's TeX input
(attributes `data-latex`, `data-mjx-texclass`) as JavaScript template
literals; the literal escapes (`\\\\` and "\\`") are undone, then every
`<math>` element is taken.  The TeX source stored in the root's `data-latex`
attribute is recorded as `tex` (written by the test authors).
"""
import argparse
import html
import os
import re
import subprocess
from datetime import date

import corpus_common as cc

REPO = "https://github.com/mathjax/MathJax-src"


def main():
    ap = argparse.ArgumentParser(description=__doc__.split("\n")[0])
    ap.add_argument("--work", required=True)
    ap.add_argument("--out", required=True)
    a = ap.parse_args()
    dest = os.path.join(a.work, "mathjax-src")
    if not os.path.isdir(dest):
        cc.sparse_clone(REPO, dest, ["/testsuite/", "/LICENSE"])
    commit = subprocess.run(["git", "-C", dest, "rev-parse", "HEAD"], capture_output=True,
                            text=True, check=True).stdout.strip()
    lurl = "%s/blob/%s/LICENSE" % (REPO, commit)
    w = cc.Writer("mathjax-tests")
    docs = []
    for root, _, files in sorted(os.walk(os.path.join(dest, "testsuite"))):
        for fn in sorted(files):
            if not fn.endswith(".snap"):
                continue
            path = os.path.relpath(os.path.join(root, fn), dest)
            src = open(os.path.join(root, fn), encoding="utf-8").read()
            src = src.replace("\\\\", "\x00").replace("\\`", "`").replace("\x00", "\\")
            n = 0
            for s, e in cc.find_spans(src):
                m = src[s:e]
                n += 1
                tex = cc.root_attrs(m).get("data-latex")
                w.add(m, producer="MathJax (test-suite snapshots)", licence="Apache-2.0",
                      licence_url=lurl, origin="%s/blob/%s/%s" % (REPO, commit, path),
                      context=cc.context_of(m), tex=tex, tex_kind="author" if tex else None,
                      markup=cc.classify_markup(m))
            if n:
                docs.append(dict(origin="%s/blob/%s/%s" % (REPO, commit, path), title=path,
                                 authors="MathJax contributors", licence="Apache-2.0",
                                 licence_url=lurl, access_url="%s/raw/%s/%s" % (REPO, commit, path),
                                 retrieved=date.today().isoformat(), formulas=n))
    w.stats["distinct_found"] = len(w)
    w.write(a.out, docs)
    cc.log("mathjax-tests rows", len(w), "found", w.found, "files", len(docs))


if __name__ == "__main__":
    main()