# HG changeset patch # User Paul Boddie # Date 1531761637 -7200 # Node ID 0940fc2a76c2e84a892f88c43df18d9dc043813b # Parent d160ea01a5cf86eef0185d8456c66fe5a0cbad1a# Parent c4440c1768b191f3bf00d1f62fc5b83f1aff99f7 Merged changes from the default branch. diff -r d160ea01a5cf -r 0940fc2a76c2 moinformat/__init__.py --- a/moinformat/__init__.py Mon Jul 16 00:08:09 2018 +0200 +++ b/moinformat/__init__.py Mon Jul 16 19:20:37 2018 +0200 @@ -3,7 +3,7 @@ """ Moin wiki format tools. -Copyright (C) 2017 Paul Boddie +Copyright (C) 2017, 2018 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -19,7 +19,7 @@ this program. If not, see . """ -from moinformat.parsers import parse, parsers -from moinformat.serialisers import serialise, serialisers +from moinformat.parsers import parse, parsers as all_parsers +from moinformat.serialisers import serialise, serialisers as all_serialisers # vim: tabstop=4 expandtab shiftwidth=4 diff -r d160ea01a5cf -r 0940fc2a76c2 moinformat/parsers/moin.py --- a/moinformat/parsers/moin.py Mon Jul 16 00:08:09 2018 +0200 +++ b/moinformat/parsers/moin.py Mon Jul 16 19:20:37 2018 +0200 @@ -411,7 +411,9 @@ region.append_inline(link) def parse_monospace(self, region): - self.parse_inline(region, Monospace, "monospace") + span = Monospace([]) + self.parse_region_details(span, ["monospaceend"]) + region.append_inline(span) def parse_smaller(self, region): self.parse_inline(region, Smaller, "smaller") @@ -552,7 +554,7 @@ "headingend" : join((group("pad", r"\N+"), # ws... group("level", "=+"), # =... - group("extra", r"\N*$"))), # ws (optional) + group("extra", r"\N*\n"))), # ws (optional) nl # List contents: diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test1.txt --- a/tests/test1.txt Mon Jul 16 00:08:09 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -Hello -{{{{#!wiki -A region - {{{ -Another -}}} -End -}}}} -XXX diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test10.txt --- a/tests/test10.txt Mon Jul 16 00:08:09 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -= Level 1 = -Text - == Level 2 Heading == -Text -Not == a heading == -== Not a heading == either -= Mismatched heading == -== Another mismatched heading = -=== Heading __Underlined__ === diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test11.txt --- a/tests/test11.txt Mon Jul 16 00:08:09 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,17 +0,0 @@ -''Some'' emphasised text. ''Emphasised''''text''. - -'''Strong ''and italic'' text.''' - -''Italic and '''strong'''''. '''''Both'', strong'''. '''''Both''', italic''. - -'''Strong''''''text'''. - -Some `monospace` text. `Mono, '''strong'''`, __underlined__, __''under''lined__. - -H,,2,,O + CO,,2,, - -== e = mc^2^ == - -~+Larger...+~ and ~-smaller-~ - -Some --(deleted)-- text. diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test12.txt --- a/tests/test12.txt Mon Jul 16 00:08:09 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -||<20%|2-2)^> Cell 1 || Cell 2 || -|| '''Cell 3''' ||<#FF0000 width="15%"> ''Cell 4'' || - -|| Not a table - || Also not a table -|| Almost a table || ... -|| A table, trailing space || - -||<20%%name="value"> Bad separator || -||<20%xx-2> Strange attributes || diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test13.txt --- a/tests/test13.txt Mon Jul 16 00:08:09 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -Wiki format - -{{{#!table -'''Cell 1''' || Cell 2 -== -Cell 3 || Cell 4 -}}} - -Wiki format again diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test2.txt --- a/tests/test2.txt Mon Jul 16 00:08:09 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,13 +0,0 @@ -XXX - * Item 1 - * Item 1.1 - * Item 2 - . Item 3 - . Item 3.1 - . Item 3.1.1 - * Item A -XXX - * Boring - * Flat - * List -XXX diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test3.txt --- a/tests/test3.txt Mon Jul 16 00:08:09 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ -XXX - a. Appendix - - i. Romanus eunt domus! - I. What did they do for us? - I. {{{Did for us}}} - 1. {{{ -Doing ''for'' us}}} - 1. {{{#!python -motto = "Romanus eunt domus!" -}}} - a.#18 The Romans. - - 1. Starting from one - 1. Two - 1.#3 Three? - 1. Four? - 1. Three - 1.#10 New list at ten - - I.#100 Century - I. Century plus one - -The end! diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test4.txt --- a/tests/test4.txt Mon Jul 16 00:08:09 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ - term:: item -not a term:: nor an item - term:: - :: item - ::non-item diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test5.txt --- a/tests/test5.txt Mon Jul 16 00:08:09 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -Hello -{{{{#!xxx -A region -{{{ -Another -}}} -End -}}}} -XXX diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test6.txt --- a/tests/test6.txt Mon Jul 16 00:08:09 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -Hello -{{{{ -Start -}}} -Still in region -}}}} -End diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test7.txt --- a/tests/test7.txt Mon Jul 16 00:08:09 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -Hello {{{world}}} again diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test8.txt --- a/tests/test8.txt Mon Jul 16 00:08:09 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,3 +0,0 @@ -XXX - -YYY diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test9.txt --- a/tests/test9.txt Mon Jul 16 00:08:09 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -XXX ----- -YYY - ----still a rule -also still a rule---- -EOF diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test_deflists.tree --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_deflists.tree Mon Jul 16 19:20:37 2018 +0200 @@ -0,0 +1,13 @@ +Region + DefTerm + Text + DefItem + Text + Block + Text + DefTerm + Text + DefItem + Text + Block + Text diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test_deflists.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_deflists.txt Mon Jul 16 19:20:37 2018 +0200 @@ -0,0 +1,5 @@ + term:: item +not a term:: nor an item + term:: + :: item + ::non-item diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test_formatting.tree --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_formatting.tree Mon Jul 16 19:20:37 2018 +0200 @@ -0,0 +1,93 @@ +Region + Block + FontStyle + Text + Text + FontStyle + Text + FontStyle + Text + Text + Break + Block + FontStyle + Text + FontStyle + Text + Text + Text + Break + Block + FontStyle + Text + FontStyle + Text + Text + FontStyle + FontStyle + Text + Text + Text + FontStyle + FontStyle + Text + Text + Text + Break + Block + FontStyle + Text + FontStyle + Text + Text + Break + Block + Text + Monospace + Text + Text + Monospace + Text + Text + Monospace + Text + FontStyle + Monospace + Text + Text + Underline + Text + Text + Underline + FontStyle + Text + Text + Text + Break + Block + Text + Subscript + Text + Text + Subscript + Text + Text + Break + Heading + Text + Superscript + Text + Break + Block + Larger + Text + Text + Smaller + Text + Text + Break + Block + Text + Strikethrough + Text + Text diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test_formatting.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_formatting.txt Mon Jul 16 19:20:37 2018 +0200 @@ -0,0 +1,17 @@ +''Some'' emphasised text. ''Emphasised''''text''. + +'''Strong ''and italic'' text.''' + +''Italic and '''strong'''''. '''''Both'', strong'''. '''''Both''', italic''. + +'''Strong''''''text'''. + +Some `monospace` text. `Mono, '''strong'''`, `mono, `'''`strong`''', __underlined__, __''under''lined__. + +H,,2,,O + CO,,2,, + +== e = mc^2^ == + +~+Larger...+~ and ~-smaller-~ + +Some --(deleted)-- text. diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test_headings.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_headings.txt Mon Jul 16 19:20:37 2018 +0200 @@ -0,0 +1,9 @@ += Level 1 = +Text + == Level 2 Heading == +Text +Not == a heading == +== Not a heading == either += Mismatched heading == +== Another mismatched heading = +=== Heading __Underlined__ === diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test_lists.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_lists.txt Mon Jul 16 19:20:37 2018 +0200 @@ -0,0 +1,13 @@ +XXX + * Item 1 + * Item 1.1 + * Item 2 + . Item 3 + . Item 3.1 + . Item 3.1.1 + * Item A +XXX + * Boring + * Flat + * List +XXX diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test_lists2.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_lists2.txt Mon Jul 16 19:20:37 2018 +0200 @@ -0,0 +1,24 @@ +XXX + a. Appendix + + i. Romanus eunt domus! + I. What did they do for us? + I. {{{Did for us}}} + 1. {{{ +Doing ''for'' us}}} + 1. {{{#!python +motto = "Romanus eunt domus!" +}}} + a.#18 The Romans. + + 1. Starting from one + 1. Two + 1.#3 Three? + 1. Four? + 1. Three + 1.#10 New list at ten + + I.#100 Century + I. Century plus one + +The end! diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test_paragraphs.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_paragraphs.txt Mon Jul 16 19:20:37 2018 +0200 @@ -0,0 +1,3 @@ +XXX + +YYY diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test_parser.py --- a/tests/test_parser.py Mon Jul 16 00:08:09 2018 +0200 +++ b/tests/test_parser.py Mon Jul 16 19:20:37 2018 +0200 @@ -1,6 +1,6 @@ #!/usr/bin/env python -from os.path import abspath, join, split +from os.path import abspath, exists, join, split import sys dirname = split(abspath(sys.argv[0]))[0] @@ -12,43 +12,186 @@ if split(parent)[1] == "MoinLight": sys.path.append(parent) -from moinformat import parse, parsers, serialise, serialisers +from moinformat import all_parsers, all_serialisers, parse, serialise +from moinformat.tree import Container from glob import glob -def test_input(s): - d = parse(s, parsers) +def test_input(d, s): + + "Compare serialised output from 'd' with its original form 's'." + o = serialise(d) - print o == s + identical = o == s if quiet: - return + return identical + # Show output versus input comparison result. + + print identical print "-" * 60 print o - if o != s: + if not identical: print "-" * 60 print s print "-" * 60 - print serialise(d, serialisers["html"]) + print serialise(d, all_serialisers["html"]) + print "-" * 60 + print + + return identical + +def test_tree(d, t, ts): + + "Compare tree structure 'd' with simplified, expected form 't' from 'ts'." + + failing = t.test(d) + + if quiet: + return not failing + + # Show tree versus expected forms. + + print not failing print "-" * 60 print d.prettyprint() + if failing: + simple, tree = failing + print "-" * 60 + print tree.prettyprint() + print "-" * 60 + print simple.prettyprint() + print "-" * 60 + print ts + print "-" * 60 print + return not failing + +class Node: + + "A simplified tree node representation." + + def __init__(self, name): + self.name = name + self.nodes = [] + + def __repr__(self): + return "Node(%r, ...)" % self.name + + def prettyprint(self, indent=""): + l = [indent + self.name] + for node in self.nodes: + l.append(node.prettyprint(indent + " ")) + return "\n".join(l) + + def append(self, node): + self.nodes.append(node) + + def test(self, other): + + """ + Test whether this node is considered equivalent to 'other', where + 'other' is a moinparser.tree node. + + Return any failing tree nodes or None. + """ + + if other.__class__.__name__ != self.name: + return self, other + + if isinstance(other, Container): + for node, other_node in map(None, self.nodes, other.nodes): + if node is None or other_node is None: + return self, other + if node.test(other_node): + return node, other_node + + return None + +def parse_tree(s): + + "Parse the tree structure representation in 's'." + + indent = 0 + branches = [] + + for line in s.split("\n"): + line = line.rstrip() + if not line: + continue + + new_indent = line.rfind(" ") + 1 + node = Node(line[new_indent:]) + + # Establish a branch to add nodes to. + + if not branches: + branches.append(node) + else: + # Note the current node as outermost branch. + + if new_indent > indent: + branches.append(node) + else: + # Reduced indent involves obtaining an inner branch again. + + while indent > new_indent: + del branches[-1] + indent -= 2 + + # Note the current node as outermost branch. + + branches[-1] = node + + # Append the current node to the parent branch. + + branches[-2].append(node) + + indent = new_indent + + return branches[0] + +def readfile(filename): + + "Read the contents of 'filename' and return them." + + f = open(filename) + try: + return f.read() + finally: + f.close() + if __name__ == "__main__": args = sys.argv[1:] + quiet = "-q" in args if quiet: del args[args.index("-q")] + filenames = args or glob(join(dirname, "test*.txt")) filenames.sort() for filename in filenames: - f = open(filename) - try: + tree_filename = "%s.tree" % filename.rsplit(".", 1)[0] + + s = readfile(filename) + d = parse(s, all_parsers) + + if exists(tree_filename): + ts = readfile(tree_filename) + t = parse_tree(ts) + else: + ts = None + + if not quiet: print filename - test_input(f.read()) - finally: - f.close() + + identical = test_input(d, s) + tree_identical = ts and test_tree(d, t, ts) + + if quiet: + print "%s %s: %s" % (identical, tree_identical, filename) # vim: tabstop=4 expandtab shiftwidth=4 diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test_region_endings.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_region_endings.txt Mon Jul 16 19:20:37 2018 +0200 @@ -0,0 +1,7 @@ +Hello +{{{{ +Start +}}} +Still in region +}}}} +End diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test_regions.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_regions.txt Mon Jul 16 19:20:37 2018 +0200 @@ -0,0 +1,9 @@ +Hello +{{{{#!wiki +A region + {{{ +Another +}}} +End +}}}} +XXX diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test_regions_inline.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_regions_inline.txt Mon Jul 16 19:20:37 2018 +0200 @@ -0,0 +1,1 @@ +Hello {{{world}}} again diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test_regions_opaque.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_regions_opaque.txt Mon Jul 16 19:20:37 2018 +0200 @@ -0,0 +1,9 @@ +Hello +{{{{#!xxx +A region +{{{ +Another +}}} +End +}}}} +XXX diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test_rules.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_rules.txt Mon Jul 16 19:20:37 2018 +0200 @@ -0,0 +1,6 @@ +XXX +---- +YYY + ----still a rule +also still a rule---- +EOF diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test_table_parser.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_table_parser.txt Mon Jul 16 19:20:37 2018 +0200 @@ -0,0 +1,9 @@ +Wiki format + +{{{#!table +'''Cell 1''' || Cell 2 +== +Cell 3 || Cell 4 +}}} + +Wiki format again diff -r d160ea01a5cf -r 0940fc2a76c2 tests/test_tables.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_tables.txt Mon Jul 16 19:20:37 2018 +0200 @@ -0,0 +1,10 @@ +||<20%|2-2)^> Cell 1 || Cell 2 || +|| '''Cell 3''' ||<#FF0000 width="15%"> ''Cell 4'' || + +|| Not a table + || Also not a table +|| Almost a table || ... +|| A table, trailing space || + +||<20%%name="value"> Bad separator || +||<20%xx-2> Strange attributes ||