Skip to content

Commit e49b70f

Browse files
committed
🔧 Add myst-docutils-demo CLI
1 parent 37a830d commit e49b70f

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

myst_parser/parsers/docutils_.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515

1616
import yaml
1717
from docutils import frontend, nodes
18-
from docutils.core import default_description, publish_cmdline
18+
from docutils.core import default_description, publish_cmdline, publish_string
19+
from docutils.frontend import filter_settings_spec
1920
from docutils.parsers.rst import Parser as RstParser
21+
from docutils.writers.html5_polyglot import HTMLTranslator, Writer
2022

2123
from myst_parser._compat import Literal, get_args, get_origin
2224
from myst_parser.config.main import (
@@ -321,6 +323,27 @@ def parse(self, inputstring: str, document: nodes.document) -> None:
321323
self.finish_parse()
322324

323325

326+
class SimpleTranslator(HTMLTranslator):
327+
def stylesheet_call(self, *args, **kwargs):
328+
return ""
329+
330+
331+
class SimpleWriter(Writer):
332+
333+
settings_spec = filter_settings_spec(
334+
Writer.settings_spec,
335+
"template",
336+
)
337+
338+
def apply_template(self):
339+
subs = self.interpolation_dict()
340+
return "%(body)s\n" % subs
341+
342+
def __init__(self):
343+
self.parts = {}
344+
self.translator_class = SimpleTranslator
345+
346+
324347
def _run_cli(writer_name: str, writer_description: str, argv: Optional[List[str]]):
325348
"""Run the command line interface for a particular writer."""
326349
publish_cmdline(
@@ -343,6 +366,44 @@ def cli_html5(argv: Optional[List[str]] = None):
343366
_run_cli("html5", "HTML5 documents", argv)
344367

345368

369+
def cli_html5_demo(argv: Optional[List[str]] = None):
370+
"""Cmdline entrypoint for converting MyST to simple HTML5 demonstrations.
371+
372+
This is a special case of the HTML5 writer,
373+
that only outputs the body of the document.
374+
"""
375+
publish_cmdline(
376+
parser=Parser(),
377+
writer=SimpleWriter(),
378+
description=(
379+
f"Generates body HTML5 from standalone MyST sources.\n{default_description}"
380+
),
381+
settings_overrides={
382+
"doctitle_xform": False,
383+
"sectsubtitle_xform": False,
384+
"initial_header_level": 1,
385+
},
386+
argv=argv,
387+
)
388+
389+
390+
def to_html5_demo(inputstring: str, **kwargs) -> str:
391+
"""Convert a MyST string to HTML5."""
392+
overrides = {
393+
"doctitle_xform": False,
394+
"sectsubtitle_xform": False,
395+
"initial_header_level": 1,
396+
"output_encoding": "unicode",
397+
}
398+
overrides.update(kwargs)
399+
return publish_string(
400+
inputstring,
401+
parser=Parser(),
402+
writer=SimpleWriter(),
403+
settings_overrides=overrides,
404+
)
405+
406+
346407
def cli_latex(argv: Optional[List[str]] = None):
347408
"""Cmdline entrypoint for converting MyST to LaTeX."""
348409
_run_cli("latex", "LaTeX documents", argv)

tests/test_docutils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
attr_to_optparse_option,
1313
cli_html,
1414
cli_html5,
15+
cli_html5_demo,
1516
cli_latex,
1617
cli_pseudoxml,
1718
cli_xml,
19+
to_html5_demo,
1820
)
1921

2022

@@ -54,6 +56,18 @@ def test_cli_html5(monkeypatch, capsys):
5456
assert "text" in captured.out
5557

5658

59+
def test_cli_html5_demo(monkeypatch, capsys):
60+
monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"text")))
61+
cli_html5_demo([])
62+
captured = capsys.readouterr()
63+
assert not captured.err
64+
assert "text" in captured.out
65+
66+
67+
def test_to_html5_demo():
68+
assert to_html5_demo("text").strip() == "<p>text</p>"
69+
70+
5771
def test_cli_latex(monkeypatch, capsys):
5872
monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"text")))
5973
cli_latex([])

0 commit comments

Comments
 (0)