# HG changeset patch # User Paul Boddie # Date 1532985560 -7200 # Node ID 429c8546ae9f1099b9152b93dfc7f25d58be89ca # Parent 8d437cdf23810bd673f6b5caeccda0996a187456 Added line break syntax, a corresponding tree node, and a Moin-compatible macro. diff -r 8d437cdf2381 -r 429c8546ae9f moinformat/macros/br.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/moinformat/macros/br.py Mon Jul 30 23:19:20 2018 +0200 @@ -0,0 +1,48 @@ +#!/usr/bin/env python + +""" +Line break macro for Moin compatibility. + +Copyright (C) 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 +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +from moinformat.macros.common import Macro +from moinformat.tree.moin import LineBreak + +class LineBreakMacro(Macro): + + "A line break macro." + + name = "BR" + + def evaluate(self): + + "Evaluate the macro, producing a line break node." + + macro = self.node + nodes = macro.parent.nodes + + # Replace the macro node with the line break. + + try: + i = nodes.index(macro) + nodes[i] = LineBreak() + except ValueError: + pass + +macro = LineBreakMacro + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 8d437cdf2381 -r 429c8546ae9f moinformat/parsers/moin.py --- a/moinformat/parsers/moin.py Mon Jul 30 17:18:52 2018 +0200 +++ b/moinformat/parsers/moin.py Mon Jul 30 23:19:20 2018 +0200 @@ -36,8 +36,8 @@ # Document tree nodes. from moinformat.tree.moin import Break, DefItem, DefTerm, FontStyle, Heading, \ - Larger, Link, List, ListItem, Macro, \ - Monospace, Region, Rule, Smaller, \ + Larger, LineBreak, Link, List, ListItem, \ + Macro, Monospace, Region, Rule, Smaller, \ Strikethrough, Subscript, Superscript, Table, \ TableAttr, TableAttrs, TableCell, TableRow, \ Text, Underline @@ -479,6 +479,9 @@ # Complete inline pattern handlers. + def parse_linebreak(self, region): + region.append_inline(LineBreak()) + def parse_link(self, region): target = self.match_group("target") text = self.match_group("text") @@ -493,7 +496,7 @@ # interpret the individual arguments. arglist = args and args.split(",") or [] - macro = Macro(name, arglist) + macro = Macro(name, arglist, region.append_point()) region.append_inline(macro) # Record the macro for later processing. @@ -614,6 +617,8 @@ # Complete inline patterns are for markup features that do not support # arbitrary content within them: + "linebreak" : r"\\\\", # \\ + "link" : join((r"\[\[", # [[ group("target", ".*?"), # target optional(join((r"\|", group("text", ".*?")))), # | text (optional) @@ -697,8 +702,8 @@ ] inline_pattern_names = [ - "fontstyle", "larger", "link", "macro", "monospace", "regionstart", - "smaller", "strike", "sub", "super", "underline", + "fontstyle", "larger", "linebreak", "link", "macro", "monospace", + "regionstart", "smaller", "strike", "sub", "super", "underline", ] list_pattern_names = [ @@ -745,6 +750,7 @@ "headingend" : parse_heading_end, "larger" : parse_larger, "largerend" : end_region, + "linebreak" : parse_linebreak, "link" : parse_link, "macro" : parse_macro, "listitemend" : end_region, diff -r 8d437cdf2381 -r 429c8546ae9f moinformat/serialisers/html/moin.py --- a/moinformat/serialisers/html/moin.py Mon Jul 30 17:18:52 2018 +0200 +++ b/moinformat/serialisers/html/moin.py Mon Jul 30 23:19:20 2018 +0200 @@ -247,6 +247,9 @@ def break_(self): pass + def linebreak(self): + self.out("
") + def rule(self, length): self.out("
" % min(length, 10)) diff -r 8d437cdf2381 -r 429c8546ae9f moinformat/serialisers/moin/moin.py --- a/moinformat/serialisers/moin/moin.py Mon Jul 30 17:18:52 2018 +0200 +++ b/moinformat/serialisers/moin/moin.py Mon Jul 30 23:19:20 2018 +0200 @@ -185,6 +185,9 @@ def continuation(self, text): self.out(text) + def linebreak(self): + self.out(r"\\") + def rule(self, length): self.out("-" * length) diff -r 8d437cdf2381 -r 429c8546ae9f moinformat/tree/moin.py --- a/moinformat/tree/moin.py Mon Jul 30 17:18:52 2018 +0200 +++ b/moinformat/tree/moin.py Mon Jul 30 23:19:20 2018 +0200 @@ -40,12 +40,32 @@ add = append - append_inline = append + def append_inline(self, node): + + "Append 'node' inline within the appropriate container." + + n = self.append_point() + + # Redirect the addition if another container is to accept the node. + + if n is not self: + n.append_inline(node) + + # Otherwise, append within this container. + + else: + n.append(node) def append_inline_many(self, nodes): for node in nodes: self.append_inline(node) + def append_point(self): + + "Return the container to which inline nodes are added." + + return self + def empty(self): return not self.nodes @@ -117,11 +137,14 @@ else: self.append(node) - def append_inline(self, node): + def append_point(self): + + "Return the container to which inline nodes are added." + if self.transparent: - self.nodes[-1].append(node) + return self.nodes[-1] else: - self.append(node) + return self def have_end(self, s): return self.level and s.startswith("}") and self.level == len(s) @@ -453,13 +476,14 @@ "Macro details." - def __init__(self, name, args, nodes=None): + def __init__(self, name, args, parent, nodes=None): Container.__init__(self, nodes or []) self.name = name + self.parent = parent self.args = args def __repr__(self): - return "Macro(%r, %r, %r)" % (self.name, self.args, self.nodes) + return "Macro(%r, %r, %r, %r)" % (self.name, self.args, self.parent, self.nodes) def prettyprint(self, indent=""): l = ["%sMacro: name=%r args=%r" % (indent, self.name, self.args)] @@ -549,6 +573,19 @@ def to_string(self, out): out.break_() +class LineBreak(Node): + + "A line break within a block." + + def __repr__(self): + return "LineBreak()" + + def prettyprint(self, indent=""): + return "%sLineBreak" % indent + + def to_string(self, out): + out.linebreak() + class Rule(Node): "A horizontal rule." diff -r 8d437cdf2381 -r 429c8546ae9f tests/test_paragraphs.tree --- a/tests/test_paragraphs.tree Mon Jul 30 17:18:52 2018 +0200 +++ b/tests/test_paragraphs.tree Mon Jul 30 23:19:20 2018 +0200 @@ -4,3 +4,10 @@ Break Block Text + Break + Block + Text + LineBreak + Text + Macro + Text diff -r 8d437cdf2381 -r 429c8546ae9f tests/test_paragraphs.txt --- a/tests/test_paragraphs.txt Mon Jul 30 17:18:52 2018 +0200 +++ b/tests/test_paragraphs.txt Mon Jul 30 23:19:20 2018 +0200 @@ -1,3 +1,5 @@ XXX YYY + +AAA\\BBB<
>CCC