1.1 --- a/moinformat/parsers/common.py Tue Jul 24 15:43:20 2018 +0200
1.2 +++ b/moinformat/parsers/common.py Tue Jul 24 18:45:13 2018 +0200
1.3 @@ -209,14 +209,16 @@
1.4
1.5 region_pattern_names = None
1.6
1.7 - def __init__(self, formats=None):
1.8 + def __init__(self, formats=None, root=None):
1.9
1.10 """
1.11 Initialise the parser with any given 'formats' mapping from region type
1.12 - names to parser objects.
1.13 + names to parser objects. An optional 'root' indicates the document-level
1.14 + parser.
1.15 """
1.16
1.17 self.formats = formats
1.18 + self.root = root
1.19
1.20 def get_parser(self, format_type):
1.21
1.22 @@ -229,7 +231,7 @@
1.23
1.24 cls = self.formats.get(format_type)
1.25 if cls:
1.26 - return cls(self.formats)
1.27 + return cls(self.formats, self.root or self)
1.28 else:
1.29 return None
1.30
2.1 --- a/moinformat/parsers/moin.py Tue Jul 24 15:43:20 2018 +0200
2.2 +++ b/moinformat/parsers/moin.py Tue Jul 24 18:45:13 2018 +0200
2.3 @@ -36,11 +36,12 @@
2.4
2.5 "A wiki region parser."
2.6
2.7 - def __init__(self, formats=None):
2.8 + def __init__(self, formats=None, root=None):
2.9
2.10 """
2.11 Initialise the parser with any given 'formats' mapping from region type
2.12 - names to parser objects.
2.13 + names to parser objects. An optional 'root' indicates the document-level
2.14 + parser.
2.15 """
2.16
2.17 # Introduce this class as the default parser for the wiki format.
2.18 @@ -49,7 +50,11 @@
2.19 if formats:
2.20 default_formats.update(formats)
2.21
2.22 - ParserBase.__init__(self, default_formats)
2.23 + ParserBase.__init__(self, default_formats, root)
2.24 +
2.25 + # Record macro occurrences for later evaluation.
2.26 +
2.27 + self.macros = []
2.28
2.29 # Principal parser methods.
2.30
2.31 @@ -464,6 +469,10 @@
2.32 macro = Macro(name, arglist)
2.33 region.append_inline(macro)
2.34
2.35 + # Record the macro for later processing.
2.36 +
2.37 + self.root.macros.append(macro)
2.38 +
2.39
2.40
2.41 # Table attribute handlers.
3.1 --- a/moinformat/serialisers/html/moin.py Tue Jul 24 15:43:20 2018 +0200
3.2 +++ b/moinformat/serialisers/html/moin.py Tue Jul 24 18:45:13 2018 +0200
3.3 @@ -141,6 +141,29 @@
3.4 def end_listitem(self, indent, marker, space, num):
3.5 self.out("</li>")
3.6
3.7 + def start_macro(self, name, args):
3.8 +
3.9 + # Fallback case for when macros are not replaced.
3.10 +
3.11 + self.out("<span class='macro'>")
3.12 + self.out(escape_text("<<"))
3.13 + self.out("<span class='name'>%s</span>" % escape_text(name))
3.14 + if args:
3.15 + self.out("(")
3.16 + first = True
3.17 + for arg in args:
3.18 + if not first:
3.19 + self.out(",")
3.20 + self.out("<span class='arg'>%s</span>" % escape_text(arg))
3.21 + first = False
3.22 + if args:
3.23 + self.out(")")
3.24 + self.out(escape_text(">>"))
3.25 + self.out("</span>")
3.26 +
3.27 + def end_macro(self):
3.28 + pass
3.29 +
3.30 def start_monospace(self):
3.31 self.out("<tt>")
3.32
3.33 @@ -213,32 +236,6 @@
3.34 def break_(self):
3.35 pass
3.36
3.37 - def macro(self, name, args):
3.38 -
3.39 - # NOTE: Special case.
3.40 -
3.41 - if name == "BR":
3.42 - self.out("<br />")
3.43 - return
3.44 -
3.45 - # Fallback case.
3.46 -
3.47 - self.out("<span class='macro'>")
3.48 - self.out(escape_text("<<"))
3.49 - self.out("<span class='name'>%s</span>" % escape_text(name))
3.50 - if args:
3.51 - self.out("(")
3.52 - first = True
3.53 - for arg in args:
3.54 - if not first:
3.55 - self.out(",")
3.56 - self.out("<span class='arg'>%s</span>" % escape_text(arg))
3.57 - first = False
3.58 - if args:
3.59 - self.out(")")
3.60 - self.out(escape_text(">>"))
3.61 - self.out("</span>")
3.62 -
3.63 def rule(self, length):
3.64 self.out("<hr style='height: %dpt' />" % min(length, 10))
3.65
4.1 --- a/moinformat/serialisers/moin/moin.py Tue Jul 24 15:43:20 2018 +0200
4.2 +++ b/moinformat/serialisers/moin/moin.py Tue Jul 24 18:45:13 2018 +0200
4.3 @@ -102,6 +102,12 @@
4.4 def end_listitem(self, indent, marker, space, num):
4.5 pass
4.6
4.7 + def start_macro(self, name, args):
4.8 + self.out("<<%s%s>>" % (name, args and "(%s)" % ",".join(args) or ""))
4.9 +
4.10 + def end_macro(self):
4.11 + pass
4.12 +
4.13 def start_monospace(self):
4.14 self.out("`")
4.15
4.16 @@ -177,9 +183,6 @@
4.17 def continuation(self, text):
4.18 self.out(text)
4.19
4.20 - def macro(self, name, args):
4.21 - self.out("<<%s%s>>" % (name, args and "(%s)" % ",".join(args) or ""))
4.22 -
4.23 def rule(self, length):
4.24 self.out("-" * length)
4.25
5.1 --- a/moinformat/tree/moin.py Tue Jul 24 15:43:20 2018 +0200
5.2 +++ b/moinformat/tree/moin.py Tue Jul 24 18:45:13 2018 +0200
5.3 @@ -453,6 +453,28 @@
5.4 out.end_linktext()
5.5 out.end_link()
5.6
5.7 +class Macro(Container):
5.8 +
5.9 + "Macro details."
5.10 +
5.11 + def __init__(self, name, args, nodes=None):
5.12 + Container.__init__(self, nodes or [])
5.13 + self.name = name
5.14 + self.args = args
5.15 +
5.16 + def __repr__(self):
5.17 + return "Macro(%r, %r, %r)" % (self.name, self.args, self.nodes)
5.18 +
5.19 + def prettyprint(self, indent=""):
5.20 + l = ["%sMacro: name=%r args=%r" % (indent, self.name, self.args)]
5.21 + return self._prettyprint(l, indent)
5.22 +
5.23 + def to_string(self, out):
5.24 + out.start_macro(self.name, self.args)
5.25 + if self.nodes:
5.26 + self._to_string(out)
5.27 + out.end_macro()
5.28 +
5.29 class Monospace(Inline):
5.30
5.31 "Monospaced text."
5.32 @@ -531,23 +553,6 @@
5.33 def to_string(self, out):
5.34 out.break_()
5.35
5.36 -class Macro(Node):
5.37 -
5.38 - "Macro details."
5.39 -
5.40 - def __init__(self, name, args):
5.41 - self.name = name
5.42 - self.args = args
5.43 -
5.44 - def __repr__(self):
5.45 - return "Macro(%r, %r)" % (self.name, self.args)
5.46 -
5.47 - def prettyprint(self, indent=""):
5.48 - return "%sMacro: name=%r args=%r" % (indent, self.name, self.args)
5.49 -
5.50 - def to_string(self, out):
5.51 - out.macro(self.name, self.args)
5.52 -
5.53 class Rule(Node):
5.54
5.55 "A horizontal rule."