# HG changeset patch # User Paul Boddie # Date 1532450713 -7200 # Node ID 851b65d39f8c387b601371a63bfd44a84a82eab6 # Parent 15c19a3d61e91702213e84071c618d3647717fea Introduced macro recording when parsing so that the macros can be obtained for later processing. Changed macro nodes to be containers and the serialisation methods for macros to permit the serialisation of generated macro content. diff -r 15c19a3d61e9 -r 851b65d39f8c moinformat/parsers/common.py --- a/moinformat/parsers/common.py Tue Jul 24 15:43:20 2018 +0200 +++ b/moinformat/parsers/common.py Tue Jul 24 18:45:13 2018 +0200 @@ -209,14 +209,16 @@ region_pattern_names = None - def __init__(self, formats=None): + def __init__(self, formats=None, root=None): """ Initialise the parser with any given 'formats' mapping from region type - names to parser objects. + names to parser objects. An optional 'root' indicates the document-level + parser. """ self.formats = formats + self.root = root def get_parser(self, format_type): @@ -229,7 +231,7 @@ cls = self.formats.get(format_type) if cls: - return cls(self.formats) + return cls(self.formats, self.root or self) else: return None diff -r 15c19a3d61e9 -r 851b65d39f8c moinformat/parsers/moin.py --- a/moinformat/parsers/moin.py Tue Jul 24 15:43:20 2018 +0200 +++ b/moinformat/parsers/moin.py Tue Jul 24 18:45:13 2018 +0200 @@ -36,11 +36,12 @@ "A wiki region parser." - def __init__(self, formats=None): + def __init__(self, formats=None, root=None): """ Initialise the parser with any given 'formats' mapping from region type - names to parser objects. + names to parser objects. An optional 'root' indicates the document-level + parser. """ # Introduce this class as the default parser for the wiki format. @@ -49,7 +50,11 @@ if formats: default_formats.update(formats) - ParserBase.__init__(self, default_formats) + ParserBase.__init__(self, default_formats, root) + + # Record macro occurrences for later evaluation. + + self.macros = [] # Principal parser methods. @@ -464,6 +469,10 @@ macro = Macro(name, arglist) region.append_inline(macro) + # Record the macro for later processing. + + self.root.macros.append(macro) + # Table attribute handlers. diff -r 15c19a3d61e9 -r 851b65d39f8c moinformat/serialisers/html/moin.py --- a/moinformat/serialisers/html/moin.py Tue Jul 24 15:43:20 2018 +0200 +++ b/moinformat/serialisers/html/moin.py Tue Jul 24 18:45:13 2018 +0200 @@ -141,6 +141,29 @@ def end_listitem(self, indent, marker, space, num): self.out("") + def start_macro(self, name, args): + + # Fallback case for when macros are not replaced. + + self.out("") + self.out(escape_text("<<")) + self.out("%s" % escape_text(name)) + if args: + self.out("(") + first = True + for arg in args: + if not first: + self.out(",") + self.out("%s" % escape_text(arg)) + first = False + if args: + self.out(")") + self.out(escape_text(">>")) + self.out("") + + def end_macro(self): + pass + def start_monospace(self): self.out("") @@ -213,32 +236,6 @@ def break_(self): pass - def macro(self, name, args): - - # NOTE: Special case. - - if name == "BR": - self.out("
") - return - - # Fallback case. - - self.out("") - self.out(escape_text("<<")) - self.out("%s" % escape_text(name)) - if args: - self.out("(") - first = True - for arg in args: - if not first: - self.out(",") - self.out("%s" % escape_text(arg)) - first = False - if args: - self.out(")") - self.out(escape_text(">>")) - self.out("") - def rule(self, length): self.out("
" % min(length, 10)) diff -r 15c19a3d61e9 -r 851b65d39f8c moinformat/serialisers/moin/moin.py --- a/moinformat/serialisers/moin/moin.py Tue Jul 24 15:43:20 2018 +0200 +++ b/moinformat/serialisers/moin/moin.py Tue Jul 24 18:45:13 2018 +0200 @@ -102,6 +102,12 @@ def end_listitem(self, indent, marker, space, num): pass + def start_macro(self, name, args): + self.out("<<%s%s>>" % (name, args and "(%s)" % ",".join(args) or "")) + + def end_macro(self): + pass + def start_monospace(self): self.out("`") @@ -177,9 +183,6 @@ def continuation(self, text): self.out(text) - def macro(self, name, args): - self.out("<<%s%s>>" % (name, args and "(%s)" % ",".join(args) or "")) - def rule(self, length): self.out("-" * length) diff -r 15c19a3d61e9 -r 851b65d39f8c moinformat/tree/moin.py --- a/moinformat/tree/moin.py Tue Jul 24 15:43:20 2018 +0200 +++ b/moinformat/tree/moin.py Tue Jul 24 18:45:13 2018 +0200 @@ -453,6 +453,28 @@ out.end_linktext() out.end_link() +class Macro(Container): + + "Macro details." + + def __init__(self, name, args, nodes=None): + Container.__init__(self, nodes or []) + self.name = name + self.args = args + + def __repr__(self): + return "Macro(%r, %r, %r)" % (self.name, self.args, self.nodes) + + def prettyprint(self, indent=""): + l = ["%sMacro: name=%r args=%r" % (indent, self.name, self.args)] + return self._prettyprint(l, indent) + + def to_string(self, out): + out.start_macro(self.name, self.args) + if self.nodes: + self._to_string(out) + out.end_macro() + class Monospace(Inline): "Monospaced text." @@ -531,23 +553,6 @@ def to_string(self, out): out.break_() -class Macro(Node): - - "Macro details." - - def __init__(self, name, args): - self.name = name - self.args = args - - def __repr__(self): - return "Macro(%r, %r)" % (self.name, self.args) - - def prettyprint(self, indent=""): - return "%sMacro: name=%r args=%r" % (indent, self.name, self.args) - - def to_string(self, out): - out.macro(self.name, self.args) - class Rule(Node): "A horizontal rule."