def test_command_toc_exclude_tags(): source = dedent(""" [tags=notoc] = Header 1 == Header 1.1 = Header 2 ::toc:exclude_tags=notoc """) parser = init_parser(source) parser.parse() result = visitlist([node.asdict() for node in parser.nodes], toc=parser.toc) assert result == [ '<h1 id="header-1">Header 1</h1>', '<h2 id="header-1.1">Header 1.1</h2>', '<h1 id="header-2">Header 2</h1>', remove_indentation(""" <div> <ul> <li> <a href="#header-2">Header 2</a> </li> </ul> </div> """), ]
def test_list_unordered(): source = dedent( """ * Item 1 ** Item 1.1 * Item 2 """ ) expected = [ remove_indentation( """ <ul> <li>Item 1 <ul> <li>Item 1.1</li> </ul> </li> <li>Item 2</li> </ul> """ ) ] _test(source, expected)
def test_toc_generation_in_standard_blocks(): source = dedent(""" = Header 1 == Header 1.1 [someblock] ---- == Header 1.2 ::toc: ---- """) parser = init_parser(source) parser.parse() result = visitlist([node.asdict() for node in parser.nodes], toc=parser.toc) assert result == [ '<h1 id="header-1">Header 1</h1>', '<h2 id="header-1.1">Header 1.1</h2>', '<div class="someblock"><div class="content"><h2 id="header-1.2">Header 1.2</h2>\n<div><ul><li><a href="#header-1">Header 1</a><ul><li><a href="#header-1.1">Header 1.1</a></li><li><a href="#header-1.2">Header 1.2</a></li></ul></li></ul></div></div></div>', ]
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_list_mixed(): source = dedent( """ # Item 1 ** Item 1.1 # Item 2 """ ) expected = [ remove_indentation( """ <ol> <li>Item 1 <ul> <li>Item 1.1</li> </ul> </li> <li>Item 2</li> </ol> """ ) ] _test(source, expected)
def test_basic_block_title(header_anchor_mock): header_anchor_mock.return_value = "XXXXXX" source = dedent(""" . Title [sometype] ++++ = A block This contains headers, paragraphs and blocks ++++ """) expected = [ remove_indentation(""" <div class="sometype"> <div class="title">Title</div> <div class="content"> <h1 id="XXXXXX">A block</h1> <p>This contains headers, paragraphs and blocks</p> </div> </div> """) ] _test(source, expected)
def test_toc_generation_from_headers(): source = dedent(""" = Header 1 == Header 1.1 ::toc: """) parser = init_parser(source) parser.parse() result = visitlist([node.asdict() for node in parser.nodes], toc=parser.toc) assert result == [ '<h1 id="header-1">Header 1</h1>', '<h2 id="header-1.1">Header 1.1</h2>', remove_indentation(""" <div> <ul> <li> <a href="#header-1">Header 1</a> <ul> <li> <a href="#header-1.1">Header 1.1</a> </li> </ul> </li> </ul> </div> """), ]
def test_admonition(): source = dedent( """ [admonition,aclass,anicon,alabel] ---- This is my test admonition ---- """ ) expected = [ remove_indentation( """ <div class="admonition aclass"> <i class="anicon"></i> <div class="content"> <div class="title">alabel</div> <div> <p>This is my test admonition</p> </div> </div> </div> """ ) ] _test(source, expected)
def test_document(): parser = init_parser( dedent( """ This is text """ ) ) parser.parse() document = DocumentNode(parser.nodes) node = document.asdict() result = visitlist([node]) assert result == [ remove_indentation( """ <html> <head> </head> <body> <p>This is text</p> </body> </html> """ ) ]
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_macro_footnote_inside_admonition(footnote_anchors_mock): footnote_anchors_mock.return_value = ("refXYZ", "defXYZ") p = init_parser( dedent(""" Some text[footnote](note number 1) [admonition, someclass, someicon, somelabel] ---- Some text[footnote](note number 2) ---- """)) p.parse() assert p.footnotes.asdict() == { "type": "footnotes", "entries": [ { "type": "footnote_def", "refanchor": "refXYZ", "defanchor": "defXYZ", "number": 1, "content": [{ "type": "sentence", "content": [ { "type": "text", "value": "note number 1" }, ], }], }, { "type": "footnote_def", "refanchor": "refXYZ", "defanchor": "defXYZ", "number": 1, "content": [{ "type": "sentence", "content": [ { "type": "text", "value": "note number 2" }, ], }], }, ], }
def test_content_image(): source = dedent(""" << image:/path/to/it.jpg """) expected = ["image::/path/to/it.jpg[]\n"] _test(source, expected)
def test_create_toc_orphan_nodes(header_anchor_mock): header_anchor_mock.side_effect = lambda text, level: f"{text}-XXXXXX" p = init_parser( dedent( """ = Header 1 === Header 1.1.1 == Header 1.2 """ ) ) p.parse() assert p.toc.asdict() == { "type": "toc", "entries": [ { "type": "toc_entry", "header": { "level": 1, "value": "Header 1", "anchor": "Header 1-XXXXXX", "tags": [], "kwargs": {}, "type": "header", }, "children": [ { "type": "toc_entry", "header": { "level": 3, "value": "Header 1.1.1", "anchor": "Header 1.1.1-XXXXXX", "tags": [], "kwargs": {}, "type": "header", }, "children": [], }, { "type": "toc_entry", "header": { "level": 2, "value": "Header 1.2", "anchor": "Header 1.2-XXXXXX", "tags": [], "kwargs": {}, "type": "header", }, "children": [], }, ], }, ], }
def test_content_image_asciidoctor_classes(): source = dedent(""" . Title [alt_text="Some text", asciidoctor_classes=".text-center"] << image:/path/to/it.jpg """) expected = ["[.text-center]\n.Title\nimage::/path/to/it.jpg[Some text]\n"] _test(source, expected)
def test_content_image_with_title_and_alt_text(): source = dedent(""" . Title [alt_text="Some text"] << image:/path/to/it.jpg """) expected = [".Title\nimage::/path/to/it.jpg[Some text]\n"] _test(source, expected)
def test_admonition(): source = dedent(""" [admonition,warning,anicon,alabel] ---- This is my test admonition ---- """) expected = ["[WARNING.anicon]\n====\nThis is my test admonition\n====\n"] _test(source, expected)
def test_parse_open_multi_line_comments(): p = init_parser( dedent(""" //// This is a multiline comment """)) with pytest.raises(ParseError): p.parse()
def test_source(): source = dedent(""" [source,language] ---- Some {} source a:=5 code ---- """) expected = ["[source,language]\n----\nSome {} source a:=5 code\n----\n"] _test(source, expected)
def test_source(): source = dedent(""" [myblock, engine=source, language=somelang] ---- Some {} source a:=5 code ---- """) expected = ["``` somelang\nSome {} source a:=5 code\n```"] _test(source, expected)
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_admonition_not_known(): ast = init_ast( dedent(""" [admonition,something,anicon,alabel] ---- This is my test admonition ---- """)) with pytest.raises(ValueError): visitlist(ast)
def test_toc_generation_without_headers(): source = dedent(""" ::toc: """) parser = init_parser(source) parser.parse() result = visitlist([node.asdict() for node in parser.nodes], toc=parser.toc) assert result == ["<div></div>"]
def test_parse_multi_line_comments(): p = init_parser( dedent(""" //// This is a multiline comment //// """)) p.parse() assert listasdict(p.nodes) == []
def test_no_document(): parser = init_parser(dedent(""" This is text """)) parser.parse() document = DocumentNode(parser.nodes, no_document=True) node = document.asdict() result = visitlist([node]) assert result == ["<p>This is text</p>"]
def test_document(): parser = init_parser(dedent(""" This is text """)) parser.parse() document = DocumentNode(parser.nodes) node = document.asdict() result = visitlist([node]) assert result == ["This is text\n"]
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_raw(): source = dedent(""" [raw] ---- <div class="someclass"> <p>This is HTML</> </div> ---- """) expected = ["""<div class="someclass">\n <p>This is HTML</>\n</div>"""] _test(source, expected)
def test_admonition(): source = dedent(""" [admonition,warning,anicon,alabel] ---- This is my test admonition ---- """) expected = [ "{blurb, class: warning}\n**alabel**\n\nThis is my test admonition\n{/blurb}\n\n" ] _test(source, expected)
def test_quote(): source = dedent(""" [quote,"Star Wars, 1977"] ---- Learn about the Force, Luke. ---- """) expected = [ "{blurb, icon: quote-right}\nLearn about the Force, Luke.\n\nStar Wars, 1977\n{/blurb}\n\n" ] _test(source, expected)
def test_quote(): source = dedent(""" [quote,"Star Wars, 1977"] ---- Learn about the Force, Luke. ---- """) expected = [ '[quote, "Star Wars, 1977"]\n____\nLearn about the Force, Luke.\n____\n' ] _test(source, expected)