File: docutils_html_base.py

package info (click to toggle)
python-memray 1.17.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,396 kB
  • sloc: python: 28,451; ansic: 16,507; sh: 10,586; cpp: 8,494; javascript: 1,474; makefile: 822; awk: 12
file content (59 lines) | stat: -rw-r--r-- 1,508 bytes parent folder | download
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
"""
Convert Docutils' documentation from reStructuredText to <format>.
"""

import contextlib
from pathlib import Path
import time

import docutils
from docutils import core
import pyperf
import memray
import contextlib

try:
    from docutils.utils.math.math2html import Trace
except ImportError:
    pass
else:
    Trace.show = lambda message, channel: ...  # don't print to console

DOC_ROOT = (Path(__file__).parent / "docutils_data" / "docs").resolve()


def build_html(doc_root):
    elapsed = 0
    for file in doc_root.rglob("*.txt"):
        file_contents = file.read_text(encoding="utf-8")
        t0 = pyperf.perf_counter()
        with contextlib.nullcontext():
            with contextlib.suppress(docutils.ApplicationError):
                core.publish_string(
                    source=file_contents,
                    reader_name="standalone",
                    parser_name="restructuredtext",
                    writer_name="html5",
                    settings_overrides={
                        "input_encoding": "unicode",
                        "output_encoding": "unicode",
                        "report_level": 5,
                    },
                )
        elapsed += pyperf.perf_counter() - t0
    return elapsed


def bench_docutils(loops, doc_root):
    runs_total = 0
    for _ in range(loops):
        runs_total += build_html(doc_root)
    return runs_total


def run_benchmark():
    bench_docutils(1, DOC_ROOT)


if __name__ == "__main__":
    run_benchmark()