def test_block_quotes(renderer): renderer.render( Document.read( dedent( """\ ```{epigraph} a b*c* -- a**b** """ ) ) ) assert renderer.document.pformat() == dedent( """\ <document source="notset"> <block_quote classes="epigraph"> <paragraph> a b <emphasis> c <attribution> a <strong> b """ )
def test_footnotes(renderer): renderer.render( Document.read( dedent( """\ [^a] [^a]: footnote*text* """ ) ) ) print(renderer.document.pformat()) assert renderer.document.pformat() == dedent( """\ <document source="notset"> <paragraph> <footnote_reference auto="1" ids="id1" refname="a"> <transition> <footnote auto="1" ids="a" names="a"> <paragraph> footnote <emphasis> text """ )
def text_to_tokens(text: str): """Convert some text to the MyST base AST.""" from myst_parser.block_tokens import Document from myst_parser.json_renderer import JsonRenderer # this loads the MyST specific token parsers with JsonRenderer(): return Document.read(text)
def test_table(json_renderer, data_regression): string = dedent( """\ | abc | d | e | | --- | --- | :---: | | hjk | *y* | z | """ ) document = Document.read(string) data_regression.check(json_renderer.render(document, as_string=False))
def test_link_reference_no_key(renderer): renderer.render( Document.read(["[name]", "", '[name]: https://www.google.com "a title"', ""]) ) assert renderer.document.pformat() == dedent( """\ <document source="notset"> <paragraph> <reference refuri="https://www.google.com" title="a title"> name """ )
def test_walk(json_renderer, data_regression): doc = Document.read( dedent( """\ a **b** c [*d*](link) """ ) ) tree = [(repr(t.node), repr(t.parent), t.depth) for t in doc.walk()] data_regression.check(tree)
def test_comment(renderer_mock): renderer_mock.render(Document.read(["line 1", r"% a comment", "line 2"])) assert renderer_mock.document.pformat() == dedent( """\ <document source="notset"> <paragraph> line 1 <comment xml:space="preserve"> a comment <paragraph> line 2 """ )
def test_commonmark(entry): if entry["example"] == 14: # This is just a test that +++ are not parsed as thematic breaks pytest.skip("Expects '+++' to be unconverted (not block break).") if entry["example"] in [65, 67]: # Front matter is supported by numerous Markdown flavours, # but not strictly CommonMark, # see: https://talk.commonmark.org/t/metadata-in-documents/721/86 pytest.skip( "Thematic breaks on the first line conflict with front matter syntax" ) test_case = entry["markdown"].splitlines(keepends=True) with HTMLRenderer() as renderer: output = renderer.render(Document.read(test_case)) assert output == entry["html"]
def test_directive_options(renderer, type, text): renderer.render( Document.read(["```{restructuredtext-test-directive}"] + list(text) + ["```"])) assert renderer.document.pformat() == dedent("""\ <document source="notset"> <system_message level="1" line="1" source="notset" type="INFO"> <paragraph> Directive processed. Type="restructuredtext-test-directive", arguments=[], options={'option1': 'a', 'option2': 'b'}, content: <literal_block xml:space="preserve"> content """ # noqa: E501 )
def test_cross_referencing(sphinx_renderer, file_regression): string = dedent( """\ (target)= Title ----- [alt1](target) [](target2) [alt2](https://www.google.com) [alt3](#target3) """ ) sphinx_renderer.render(Document.read(string)) file_regression.check(sphinx_renderer.document.pformat(), extension=".xml")
def test_full_run(sphinx_renderer, file_regression): string = dedent( """\ --- a: 1 --- (target)= # header 1 ## sub header 1 a *b* **c** `abc` \\* ## sub header 2 x y [a](http://www.xyz.com) z --- # header 2 ```::python {a=1} a = 1 ``` > abc - a - b - c 1. a 2. b 1. c {ref}`target` """ ) sphinx_renderer.render(Document.read(string)) file_regression.check(sphinx_renderer.document.pformat(), extension=".xml")
def parse_text(text: str, output_type: str, **kwargs): """Convert MyST text to another format. :param text: the text to convert :param output_type: one of 'dict', 'html', 'docutils', 'sphinx' :param kwargs: parsed to the render initiatiation """ if output_type == "dict": from myst_parser.ast_renderer import AstRenderer as renderer_cls elif output_type == "html": from myst_parser.html_renderer import HTMLRenderer as renderer_cls elif output_type == "docutils": from myst_parser.docutils_renderer import DocutilsRenderer as renderer_cls elif output_type == "sphinx": from myst_parser.docutils_renderer import SphinxRenderer as renderer_cls else: raise ValueError("output_type not recognised: {}".format(output_type)) from myst_parser.block_tokens import Document with renderer_cls(**kwargs) as renderer: return renderer.render(Document.read(text))
def test_directive_arguments(renderer, name, arguments, body): content = ([ "```{restructuredtext-test-directive}" + (" " if arguments else "") + arguments ] + (["content"] if body else []) + ["```"]) renderer.render(Document.read(content)) expected = [ '<document source="notset">', ' <system_message level="1" line="1" source="notset" type="INFO">', " <paragraph>", (' Directive processed. Type="restructuredtext-test-directive", ' "arguments={args}, options={{}}, content:{content}".format( args=[arguments] if arguments else [], content="" if body else " None")), ] if body: expected.extend([ ' <literal_block xml:space="preserve">', " content" ]) expected.append("") assert renderer.document.pformat() == "\n".join(expected)
def parse(self, inputstring: str, document: nodes.document): """Parse source text. :param inputstring: The source string to parse :param document: The root docutils node to add AST elements to """ # TODO add conf.py configurable settings self.config = self.default_config.copy() try: new_cfg = self.document.settings.env.config.myst_config self.config.update(new_cfg) except AttributeError: pass renderer = SphinxRenderer(document=document) with renderer: # Log to sphinx (e.g. to warn of duplicate link/footnote definitions) renderer.parse_context.logger = SPHINX_LOGGER lines = SourceLines(inputstring, uri=document["source"], standardize_ends=True) doc = Document.read(lines) renderer.render(doc)
def test_link_def_in_directive_nested(renderer, file_regression): # TODO document or 'fix' the fact that [ref2] here isn't resolved renderer.render( Document.read( dedent( """\ ```{note} [ref1]: link ``` ```{note} [ref1] [ref2] ``` ```{note} [ref2]: link ``` """ ) ) ) file_regression.check(renderer.document.pformat(), extension=".xml")
def test_link_def_in_directive(renderer): renderer.render( Document.read( dedent( """\ ```{note} [a] ``` [a]: link """ ) ) ) assert renderer.document.pformat() == dedent( """\ <document source="notset"> <note> <paragraph> <pending_xref refdomain="True" refexplicit="True" reftarget="link" reftype="any" refwarn="True"> <literal classes="xref any"> a """ # noqa: E501 )
def test_link_references(name, strings, json_renderer, data_regression): document = Document.read(strings) data_regression.check(json_renderer.render(document, as_string=False))
def test_comment(name, json_renderer, data_regression, strings): document = Document.read(strings) data_regression.check(json_renderer.render(document, as_string=False))
def test_directive_options_error(renderer, type, text, file_regression): renderer.render( Document.read(["```{restructuredtext-test-directive}"] + list(text) + ["```"])) file_regression.check(renderer.document.pformat(), extension=".xml")