def test_end_with_empty_line(self): doc = dedent(r""" \tag """) parser = QqParser(allowed_tags={"tag"}) tree = parser.parse(doc)
def test_inlinetag_with_multiple_arguments(self): doc = r"""\blocktag Some \inlinetag[Hello][world]""" parser = QqParser(allowed_tags={'inlinetag', 'blocktag'}) tree = parser.parse(doc) self.assertEqual(tree.as_list(), ["_root", ["blocktag", "Some ", ["inlinetag", ["_item", "Hello"], ["_item", "world"]]]])
def test_inline_tag1(self): doc = r"""Hello, \tag{inline} tag! """ parser = QqParser(allowed_tags={'tag'}) tree = parser.parse(doc) self.assertEqual(tree[0], 'Hello, ') self.assertEqual(tree.tag_.value, 'inline') self.assertEqual(tree[2], ' tag!')
def test_inline_tag2(self): doc = r"""Hello, \othertag{\tag{inline} tag}! """ parser = QqParser(allowed_tags={'tag', 'othertag'}) tree = parser.parse(doc) # self.assertEqual(tree._othertag._tag.value, 'inline') self.assertEqual(tree.as_list(), ['_root', 'Hello, ', ['othertag', ['tag', 'inline'], ' tag'], '!'])
def test_multiline_inline_with_attribute(self): doc = "\\tag{hello \\tag world \n this is \n a \\tag test}" parser = QqParser(allowed_tags={"tag"}) tree = parser.parse(doc) self.assertEqual(tree.as_list(), ['_root', ['tag', 'hello ', ['tag', 'world \n this is \n a'], ['tag', 'test']]])
def test_alias2tag(self): doc = r"""\# Heading 1 \## Heading 2 Hello """ parser = QqParser(allowed_tags={'h1', 'h2'}, alias2tag={"#": 'h1', "##": 'h2'}) tree = parser.parse(doc) self.assertEqual(tree.as_list(), ["_root", ["h1", "Heading 1"], ["h2", "Heading 2"], "Hello"])
def test_block_additional_indent(self): doc = r"""Hello \tag First Second Third End""" parser = QqParser(allowed_tags={'tag'}) tree = parser.parse(doc) self.assertEqual(tree.tag_._children, ['First\n Second\nThird'])
def test_empty_square_bracket_tag(self): doc = r"""\blocktag Some \empty[ ] tag """ parser = QqParser(allowed_tags={'blocktag', 'empty'}) tree = parser.parse(doc) self.assertEqual(tree.as_list(),["_root", ['blocktag', 'Some ', ['empty', ['_item', '\n']], ' tag']])
def test_blocktag_inside_inlinetag(self): doc = r"""\blocktag Some \inlinetag[Hello \blocktag test]""" parser = QqParser(allowed_tags={'inlinetag', 'blocktag'}) tree = parser.parse(doc) self.assertEqual(tree.as_list(), ['_root', ['blocktag', 'Some ', ['inlinetag', ['_item', 'Hello ', ['blocktag', 'test']]]]]) doc = r"""Some \inlinetag[Hello \blocktag test \blocktag another test]""" parser = QqParser(allowed_tags={'inlinetag', 'blocktag'}) tree = parser.parse(doc) print(tree.as_list()) self.assertEqual(tree.as_list(), ['_root', 'Some ', ['inlinetag', ['_item', 'Hello ', ['blocktag', 'test'], ['blocktag', 'another test']]]])
def test_newline(self): doc = dedent(r""" \tag Hello Stop. """) parser = QqParser(allowed_tags={'tag'}) tree = parser.parse(doc) self.assertEqual(tree.as_list(), ['_root', '', ['tag', 'Hello'], '\nStop.'])
def test_block_tags1(self): doc = r"""Hello \tag World """ parser = QqParser(allowed_tags={'tag'}) tree = parser.parse(doc) print(tree.as_list()) self.assertEqual(tree[0], "Hello") self.assertEqual(tree.tag_.name, 'tag') self.assertEqual(tree.tag_.value, 'World')
def test_inline_tag4(self): doc = r"Hello, \tag{I'm [your{taggy}] tag} okay" parser = QqParser(allowed_tags={'tag', 'othertag'}) tree = parser.parse(doc) self.assertEqual(tree.as_list(), [ '_root', 'Hello, ', [ 'tag', "I'm [your{taggy}] tag" ], " okay" ]) def test_block_and_inline_tags(self): doc = r"""Hello, \tag I'm your \othertag{tag} \tag { \tag { this \tag{is a {a test} okay} } } """ parser = QqParser(allowed_tags={'tag', 'othertag'}) tree = parser.parse(doc) self.assertEqual(tree.as_list(), [ '_root', 'Hello,\n', [ 'tag', "I'm your ", ['othertag', 'tag'], '\n', [ 'tag', '{\n', [ 'tag', '{\nthis ', [ 'tag', 'is a {a test}\nokay', ], '\n' ], '}\n' ], '}\n' ] ])
def test_mixed_brackets_inline(self): doc = r"Some inline \tag[with]{multiple}[arguments]" parser = QqParser(allowed_tags={"tag"}) tree = parser.parse(doc) self.assertEqual(tree.as_list(), [ "_root", "Some inline ", ["tag", ["_item", "with"], "multiple", ["_item", "arguments"] ] ])
def test_blank_line_after_tag(self): doc = dedent(r""" \tag otherline \tag othertag """) parser = QqParser(allowed_tags={"tag"}) tree = parser.parse(doc) self.assertEqual(tree.as_list(), ['_root', '', ['tag', '\notherline', ['tag', 'othertag']]])
def test_as_etree(self): doc = dedent(r""" \tag some content \tag other content more text here """) parser = QqParser(allowed_tags={"tag"}) tree = parser.parse(doc) self.assertEqual(et.tostring(tree.as_etree()), b'<_root><tag>some content' b'<tag>other content' b'</tag>more text here' b'</tag></_root>')
def test_inline_tag_at_the_beginning_of_the_line(self): doc = r"""\tag some content here here and here and we have some inline \tag{here and \othertag{there}} """ parser = QqParser(allowed_tags={'tag', 'othertag'}) tree = parser.parse(doc) self.assertEqual(tree.as_list(), ['_root', ['tag','some content ' 'here here and ' 'here and we ' 'have some ' 'inline\n', ['tag', 'here and ',['othertag', 'there']], '']])
def test_children_tags(self): doc = dedent(r""" \tag some content \tag other content more text here \tag some other tag """) parser = QqParser(allowed_tags={"tag"}) tree = parser.parse(doc) childrens = (list(tree.tag_.children_tags())) self.assertEqual(childrens[0].as_list(), ["tag", "other content"]) self.assertEqual(childrens[1].as_list(), ["tag", "some other tag"])
def test_multiple_arguments2(self): doc = r"""\proof By \ref[existence and uniqueness theorem\nonumber][thm:4:eu] there exists """ parser = QqParser(allowed_tags={"proof", "ref", "nonumber"}) tree = parser.parse(doc) print(tree.as_list()) self.assertEqual(tree.as_list(), ['_root', ['proof', 'By ', ['ref', ['_item', 'existence\nand uniqueness theorem', ['nonumber']], ['_item', 'thm:4:eu']], ' there exists']])
def test_inline_tag3(self): doc = r"""Hello, \tag{ this is a continuation of inline tag on the next line the next one\othertag{okay}} """ parser = QqParser(allowed_tags={'tag', 'othertag'}) tree = parser.parse(doc) self.assertEqual(tree.as_list(), [ '_root', 'Hello, ', [ 'tag', ('\nthis is a continuation of inline tag on the next line' '\n\nthe next one'), [ 'othertag', 'okay' ] ], '' ])
def test_block_tags_nested(self): doc = r"""Hello \tag World \othertag This Is A test The end Blank line before the end """ parser = QqParser(allowed_tags={'tag', 'othertag'}) tree = parser.parse(doc) print(tree.as_list()) self.assertEqual(tree[0], "Hello") self.assertEqual(tree.tag_[0], "World") self.assertEqual(tree.tag_.othertag_._children, ["This\nIs"]) self.assertEqual(tree.tag_[2], 'A test') self.assertEqual(tree[2], 'The end\n\nBlank line before the end') self.assertEqual(tree.tag_.parent, tree) self.assertEqual(tree.tag_.othertag_.parent, tree.tag_)
def test_escape_unescape(self): doc = r"""Hello \sometag test \\sometag test \sometag \ here we are we are here some \inline{tag with \{ curve bracket inside} okay some \inline[square bracket \[ inside] okay """ parser = QqParser(allowed_tags={'sometag', 'inline'}) tree = parser.parse(doc) self.assertEqual(tree.as_list(), [ "_root", "Hello", ["sometag", "test"], "\\sometag test", ["sometag", " here we are\nwe are here"], "some ", ["inline", "tag with { curve bracket inside"], " okay\nsome ", ["inline", ["_item", "square bracket [ inside"]], " okay" ])
def test_sameline_tags(self): self.maxDiff = None doc = r""" Hello! \h1 Intro to qqmbr \h2 Fresh documentation system **qqmbr** is a documentation system intended to be extremely simple and extremely extensible. It was written to allow writing rich content that can be compiled into different formats. One source, multiple media: HTML, XML, LaTeX, PDF, eBooks, any other. Look below to see it in action. \h3 This is nice level-3 header Some paragraph text. See also \ref{sec:another} (reference to different header). There are LaTeX formulas here: \eq x^2 + y^2 = z^2 `\\eq` is a qqtag. It is better than tag, because it is auto-closing (look at the indent, like Python). Here is formula with the label: \equation \label eq:Fermat x^n + y^n = z^n, \quad n>2 Several formulas with labels: \gather \item \label eq:2x2 2\times 2 = 4 \item \label eq:3x3 3\times 3 = 9 We can reference formula \eqref{eq:Fermat} and \eqref{eq:2x2} just like we referenced header before. \h3 Another level-3 header \label sec:another Here is the header we referenced. \h3 More interesting content \figure \source http://example.com/somefig.png \caption Some figure \width 500px \question Do you like qqmbr? \quiz \choice \correct false No. \comment You didn't even try! \choice \correct true Yes, i like it very much! \comment And so do I! """ parser = QqParser( allowed_tags={'h1', 'h2', 'h3', 'eq', 'equation', 'label', 'gather', 'inlne', 'item', 'ref', 'eqref', 'source', 'caption', 'width', 'question', 'quiz', 'choice', 'comment', 'correct', 'figure'}) tree = parser.parse(doc) print(tree.as_list()) self.assertEqual(tree.as_list(), ['_root', 'Hello!', ['h1', 'Intro to qqmbr'], '', ['h2', 'Fresh documentation system'], '\n**qqmbr** is a documentation system intended to be extremely simple and extremely extensible.\nIt was written to allow writing rich content that can be compiled into different formats.\nOne source, multiple media: HTML, XML, LaTeX, PDF, eBooks, any other. Look below to see it in action.\n', ['h3', 'This is nice level-3 header'], '\nSome paragraph text. See also ', ['ref', 'sec:another'], ' (reference to different header).\n\nThere are LaTeX formulas here:\n', ['eq', 'x^2 + y^2 = z^2'], '\n`\\eq` is a qqtag. It is better than tag, because it is auto-closing (look at the indent, like Python).\n\nHere is formula with the label:\n', ['equation', ['label', 'eq:Fermat'], 'x^n + y^n = z^n, \\quad n>2'], '\nSeveral formulas with labels:\n', ['gather', ['item', ['label', 'eq:2x2'], '2\\times 2 = 4'], ['item', ['label', 'eq:3x3'], '3\\times 3 = 9']], '\nWe can reference formula ', ['eqref', 'eq:Fermat'], ' and ', ['eqref', 'eq:2x2'], ' just like we referenced header before.\n', ['h3', 'Another level-3 header ', ['label', 'sec:another']], '\nHere is the header we referenced.\n', ['h3', 'More interesting content'], '', ['figure', ['source', 'http://example.com/somefig.png'], ['caption', 'Some figure'], ['width', '500px']], '', ['question', 'Do you like qqmbr?', ['quiz', ['choice', ['correct', 'false'], 'No.', ['comment', "You didn't even try!"]], ['choice', ['correct', 'true'], 'Yes, i like it very much!', ['comment', 'And so do I!']]]]])
def test_non_allowed_tag_with_bracket(self): doc = r"""Hello \inlinetag{some \forbiddentag{here} okay} this""" parser = QqParser(allowed_tags={'inlinetag'}) tree = parser.parse(doc) self.assertEqual(tree.as_list(), ["_root", "Hello ", ["inlinetag", "some \\forbiddentag{here} okay"], " this"])