1.1 --- a/moinformat/parsers/moin.py Tue Jul 24 23:36:13 2018 +0200
1.2 +++ b/moinformat/parsers/moin.py Thu Jul 26 16:33:37 2018 +0200
1.3 @@ -19,11 +19,23 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 +# Document transformations.
1.8 +
1.9 +from moinformat.links import get_linker
1.10 from moinformat.macros import get_macro
1.11 +
1.12 +# Parser functionality and pattern definition.
1.13 +
1.14 from moinformat.parsers.common import ParserBase, get_patterns, \
1.15 excl, expect, group, optional, recur, \
1.16 repeat
1.17 +
1.18 +# Serialisation.
1.19 +
1.20 from moinformat.serialisers import serialise
1.21 +
1.22 +# Document tree nodes.
1.23 +
1.24 from moinformat.tree.moin import Break, DefItem, DefTerm, FontStyle, Heading, \
1.25 Larger, Link, List, ListItem, Macro, \
1.26 Monospace, Region, Rule, Smaller, \
1.27 @@ -53,9 +65,10 @@
1.28
1.29 ParserBase.__init__(self, default_formats, root)
1.30
1.31 - # Record macro occurrences for later evaluation.
1.32 + # Record certain node occurrences for later evaluation.
1.33
1.34 self.macros = []
1.35 + self.links = []
1.36
1.37 # Principal parser methods.
1.38
1.39 @@ -106,6 +119,30 @@
1.40 macro = macro_cls(node, self.region)
1.41 macro.evaluate()
1.42
1.43 + # Link translation.
1.44 +
1.45 + def translate_links(self, scheme, name, base=""):
1.46 +
1.47 + """
1.48 + Translate the link nodes in the document for the given 'scheme' and
1.49 + employing the given document 'name' and 'base'.
1.50 + """
1.51 +
1.52 + # Obtain a class for the named linker.
1.53 +
1.54 + linker_cls = get_linker(scheme)
1.55 + if not linker_cls:
1.56 + return
1.57 +
1.58 + # Instantiate the class with document metadata.
1.59 +
1.60 + linker = linker_cls(name, base)
1.61 +
1.62 + for node in self.links:
1.63 +
1.64 + # Translate the link.
1.65 +
1.66 + linker.translate(node)
1.67
1.68
1.69 # Parser methods supporting different page features.
1.70 @@ -480,6 +517,10 @@
1.71 link = Link(text and [Text(text)], target)
1.72 region.append_inline(link)
1.73
1.74 + # Record the link for later processing.
1.75 +
1.76 + self.root.links.append(link)
1.77 +
1.78 def parse_macro(self, region):
1.79 name = self.match_group("name")
1.80 args = self.match_group("args")