def test_attributes_with_variables(): p = init_parser( dedent( """ :attrs:value1,someattr1=somevalue1,someattr2=somevalue2 [{attrs}] This is text """ ) ) p.parse() assert listasdict(p.nodes) == [ { "type": "paragraph", "args": ["value1"], "kwargs": {"someattr1": "somevalue1", "someattr2": "somevalue2"}, "content": { "type": "sentence", "content": [ { "type": "text", "value": "This is text", } ], }, }, ]
def test_parse_paragraphs_starting_with_a_macro(): p = init_parser("[link](http://some.where,text) is the link I want") p.parse() assert listasdict(p.nodes) == [ { "type": "paragraph", "args": [], "kwargs": {}, "content": { "type": "sentence", "content": [ { "type": "link", "text": "text", "target": "http://some.where", }, { "type": "text", "value": " is the link I want", }, ], }, }, ]
def test_create_toc_ignore_headers(header_anchor_mock): header_anchor_mock.side_effect = lambda text, level: f"{text}-XXXXXX" p = init_parser( dedent(""" = Header 1 ==! Header 1.1 ==! Header 1.2 = Header 2 ==! Header 2.1 ===! Header 2.1.1 """)) p.parse() assert listasdict(p.toc) == [ { "type": "toc_entry", "level": 1, "text": "Header 1", "anchor": "Header 1-XXXXXX", "children": [], }, { "type": "toc_entry", "level": 1, "text": "Header 2", "anchor": "Header 2-XXXXXX", "children": [], }, ]
def test_curly_braces_in_escaped_verbatim(): p = init_parser(r"This is \`{attr}\`", {"attr": "5"}) p.parse() assert listasdict(p.nodes) == [{ "type": "text", "value": "This is \\`5\\`\n", }]
def test_plain_text(): p = init_parser("This is text", {"attr": "5"}) p.parse() assert listasdict(p.nodes) == [{ "type": "text", "value": "This is text\n", }]
def test_escape_backtick(): p = init_parser(r"This is `\``") p.parse() assert listasdict(p.nodes) == [{ "type": "text", "value": "This is `\``\n", }]
def test_replace_variable(): p = init_parser("This is number {attr}", {"attr": "5"}) p.parse() assert listasdict(p.nodes) == [{ "type": "text", "value": "This is number 5\n", }]
def test_replace_variable_with_namespace(): p = init_parser("This is number {config.attr}", {"config": {"attr": "5"}}) p.parse() assert listasdict(p.nodes) == [{ "type": "text", "value": "This is number 5\n", }]
def test_replace_boolean(): p = init_parser("This flag is {flag}", {"flag": True}) p.parse() assert listasdict(p.nodes) == [{ "type": "text", "value": "This flag is True\n", }]
def test_open_verbatim(): p = init_parser("This is `{attr}", {"attr": "5"}) p.parse() assert listasdict(p.nodes) == [{ "type": "text", "value": "This is `5\n", }]
def test_escape_curly_braces(): p = init_parser(r"This is \{attr\}", {"attr": "5"}) p.parse() assert listasdict(p.nodes) == [{ "type": "text", "value": "This is {attr}\n", }]
def test_open_curly(): p = init_parser("This is {attr", {"attr": "5"}) p.parse() assert listasdict(p.nodes) == [{ "type": "text", "value": "This is {attr\n", }]
def test_escape_curly_braces_in_verbatim(): p = init_parser(r"This is `\{attr\}`", {"attr": "5"}) p.parse() assert listasdict(p.nodes) == [{ "type": "text", "value": "This is `\\{attr\\}`\n", }]
def test_escape_other_chars(): p = init_parser(r"This \_is\_ \text", {"attr": "5"}) p.parse() assert listasdict(p.nodes) == [{ "type": "text", "value": "This \\_is\\_ \\text\n", }]
def test_parse_variable_definition_value_can_be_any_text(): p = init_parser(":attr:[footnote](http://some.domain/path)") p.parse() assert listasdict(p.nodes) == [] assert p.variables == { "mau": {}, "attr": "[footnote](http://some.domain/path)" }
def test_command_without_arguments(): p = init_parser("::somecommand:") p.parse() assert listasdict(p.nodes) == [ { "type": "command", "name": "somecommand", "kwargs": {}, }, ]
def test_footnote_ref(): p = init_parser( dedent(""" This is a sentence[footnote](with a note) """)) p.parse() ast = listasdict(p.nodes) result = visitlist(ast, footnotes=p.footnotes) assert result == ["This is a sentence[^footnote1]\n"]
def test_parse_multi_line_comments(): p = init_parser( dedent(""" //// This is a multiline comment //// """)) p.parse() assert listasdict(p.nodes) == []
def test_command(): p = init_parser("::somecommand:arg1, arg2, name1=value1, name2=value2") p.parse() assert listasdict(p.nodes) == [ { "type": "command", "name": "somecommand", "args": ["arg1", "arg2"], "kwargs": {"name1": "value1", "name2": "value2"}, }, ]
def test_footnote(): p = init_parser( dedent(""" This is a sentence[footnote](with a note) """)) p.parse() ast = listasdict(p.nodes) result = visitlist(ast, footnotes=[i.asdict() for i in p.footnotes]) assert result == ["This is a sentencefootnote:[with a note]\n"]
def test_parse_macro_footnote_with_other_text_around(footnote_anchors_mock): footnote_anchors_mock.return_value = ("refXYZ", "defXYZ") source = "text[footnote](Some text) other text" expected = [{ "type": "sentence", "content": [ { "type": "text", "value": "text" }, { "type": "footnote_ref", "number": 1, "refanchor": "refXYZ", "defanchor": "defXYZ", }, { "type": "text", "value": " other text" }, ], }] p = _test(source, expected) assert listasdict(p.footnotes) == [{ "type": "footnote_def", "number": 1, "refanchor": "refXYZ", "defanchor": "defXYZ", "content": [{ "type": "sentence", "content": [ { "type": "text", "value": "Some text" }, ], }], }]
def test_parse_macro_footnote_includes_round_brakets(footnote_anchors_mock): footnote_anchors_mock.return_value = ("refXYZ", "defXYZ") source = r'text[footnote]("Some code(\)") other text' expected = [{ "type": "sentence", "content": [ { "type": "text", "value": "text" }, { "type": "footnote_ref", "number": 1, "refanchor": "refXYZ", "defanchor": "defXYZ", }, { "type": "text", "value": " other text" }, ], }] p = _test(source, expected) assert listasdict(p.footnotes) == [{ "type": "footnote_def", "number": 1, "refanchor": "refXYZ", "defanchor": "defXYZ", "content": [{ "type": "sentence", "content": [ { "type": "text", "value": '"Some code()"' }, ], }], }]
def test_parse_header_not_in_toc(header_anchor_mock): header_anchor_mock.return_value = "XXXXXX" source = """ =! Title of the section """ expected = [{ "type": "header", "kwargs": {}, "value": "Title of the section", "level": 1, "anchor": "XXXXXX", }] p = _test(source, expected) assert listasdict(p.toc) == []
def test_variables_can_contain_markers(): p = init_parser( dedent(""" A very {bold} text Some code: {dictdef} """), { "bold": "*bold*", "dictdef": "`adict = {'a':5}`" }, ) p.parse() assert listasdict(p.nodes) == [{ "type": "text", "value": "A very *bold* text\nSome code: `adict = {'a':5}`\n", }]
def test_parse_macro_footnote_can_start_with_different_number( footnote_anchors_mock): footnote_anchors_mock.return_value = ("refXYZ", "defXYZ") p = init_parser( "[footnote](Some text _and style_ and *more style* here)", footnotes_start_with=6, ) result = p.parse() assert listasdict(p.nodes) == [{ "type": "sentence", "content": [{ "type": "footnote_ref", "number": 6, "refanchor": "refXYZ", "defanchor": "defXYZ", }], }]
def test_parse_paragraphs_is_a_macro_only(): p = init_parser("[link](http://some.where,text)") p.parse() assert listasdict(p.nodes) == [ { "type": "paragraph", "args": [], "kwargs": {}, "content": { "type": "sentence", "content": [ { "type": "link", "text": "text", "target": "http://some.where", }, ], }, }, ]
def test_macro_footnote(footnote_anchors_mock): footnote_anchors_mock.return_value = ("refXYZ", "defXYZ") source = "[footnote](Some text _and style_ and *more style* here)" expected = [{ "type": "sentence", "content": [{ "type": "footnote_ref", "number": 1, "refanchor": "refXYZ", "defanchor": "defXYZ", "content": [{ "type": "sentence", "content": [ { "type": "text", "value": "Some text " }, { "type": "style", "value": "underscore", "content": { "type": "sentence", "content": [ { "type": "text", "value": "and style" }, ], }, }, { "type": "text", "value": " and " }, { "type": "style", "value": "star", "content": { "type": "sentence", "content": [ { "type": "text", "value": "more style" }, ], }, }, { "type": "text", "value": " here" }, ], }], }], }] p = _test(source, expected) assert listasdict(p.footnote_defs) == [{ "type": "footnote_def", "number": 1, "refanchor": "refXYZ", "defanchor": "defXYZ", "content": [{ "type": "sentence", "content": [ { "type": "text", "value": "Some text " }, { "type": "style", "value": "underscore", "content": { "type": "sentence", "content": [ { "type": "text", "value": "and style" }, ], }, }, { "type": "text", "value": " and " }, { "type": "style", "value": "star", "content": { "type": "sentence", "content": [ { "type": "text", "value": "more style" }, ], }, }, { "type": "text", "value": " here" }, ], }], }]
def test_parse_variable_definition_with_namespace(): p = init_parser(":meta.attr:42") p.parse() assert listasdict(p.nodes) == [] assert p.variables == {"meta": {"attr": "42"}}
def test_parse_variable_definition_without_value_is_loaded_as_boolean(): p = init_parser(":attr:") p.parse() assert listasdict(p.nodes) == [] assert p.variables == {"attr": True}
def test_parse_macro_footnote_commas_mau_v2(footnote_anchors_mock, ): footnote_anchors_mock.return_value = ("refXYZ", "defXYZ") source = r'text[footnote]("Some text, that should not, be split") other text' _test = parser_test_factory(TextParser) expected = [{ "type": "sentence", "content": [ { "type": "text", "value": "text" }, { "type": "footnote_ref", "number": 1, "refanchor": "refXYZ", "defanchor": "defXYZ", "content": [{ "type": "sentence", "content": [ { "type": "text", "value": "Some text, that should not, be split", }, ], }], }, { "type": "text", "value": " other text" }, ], }] p = _test(source, expected) assert listasdict(p.footnote_defs) == [{ "type": "footnote_def", "number": 1, "refanchor": "refXYZ", "defanchor": "defXYZ", "content": [{ "type": "sentence", "content": [ { "type": "text", "value": "Some text, that should not, be split", }, ], }], }]