1.1 --- a/convert.py Thu Jul 26 17:05:08 2018 +0200
1.2 +++ b/convert.py Thu Jul 26 20:09:48 2018 +0200
1.3 @@ -1,6 +1,6 @@
1.4 #!/usr/bin/env python
1.5
1.6 -from moinformat import get_serialiser, make_parser, parse, serialise
1.7 +from moinformat import make_linker, make_parser, make_serialiser, parse, serialise
1.8 from os.path import split
1.9 import sys
1.10
1.11 @@ -65,8 +65,9 @@
1.12 if tree:
1.13 print d.prettyprint()
1.14 else:
1.15 - p.translate_links(format, pagename)
1.16 - print serialise(d, get_serialiser(format))
1.17 + l = make_linker(format, pagename)
1.18 + s = make_serialiser(format, l)
1.19 + print serialise(d, s)
1.20 finally:
1.21 f.close()
1.22
2.1 --- a/moinformat/__init__.py Thu Jul 26 17:05:08 2018 +0200
2.2 +++ b/moinformat/__init__.py Thu Jul 26 20:09:48 2018 +0200
2.3 @@ -19,6 +19,7 @@
2.4 this program. If not, see <http://www.gnu.org/licenses/>.
2.5 """
2.6
2.7 +from moinformat.links import make_linker
2.8 from moinformat.parsers import parse, parsers as all_parsers
2.9 from moinformat.serialisers import serialise, serialisers as all_serialisers
2.10
2.11 @@ -40,4 +41,16 @@
2.12
2.13 return all_serialisers["%s.moin" % name]
2.14
2.15 +def make_serialiser(name, linker=None):
2.16 +
2.17 + """
2.18 + Return a serialiser instance for the format having the given 'name'.
2.19 +
2.20 + The optional 'linker' is used to control which linking scheme is used with
2.21 + the serialiser, with the default having the same name as the serialiser.
2.22 + """
2.23 +
2.24 + linker = linker or make_linker(name, "")
2.25 + return get_serialiser(name)(formats=all_serialisers, linker=linker)
2.26 +
2.27 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/moinformat/links/__init__.py Thu Jul 26 17:05:08 2018 +0200
3.2 +++ b/moinformat/links/__init__.py Thu Jul 26 20:09:48 2018 +0200
3.3 @@ -26,10 +26,23 @@
3.4 def get_linker(name):
3.5
3.6 """
3.7 - Return the linking scheme handler with the given 'name' or None if no such
3.8 - handler is found.
3.9 + Return the linking scheme handler class with the given 'name' or None if no
3.10 + such handler is found.
3.11 """
3.12
3.13 return linkers.get(name)
3.14
3.15 +def make_linker(name, pagename):
3.16 +
3.17 + """
3.18 + Return a linking scheme handler with the given 'name' and using the given
3.19 + 'pagename'.
3.20 + """
3.21 +
3.22 + linker_cls = get_linker(name)
3.23 + if not linker_cls:
3.24 + return None
3.25 +
3.26 + return linker_cls(pagename)
3.27 +
3.28 # vim: tabstop=4 expandtab shiftwidth=4
4.1 --- a/moinformat/parsers/moin.py Thu Jul 26 17:05:08 2018 +0200
4.2 +++ b/moinformat/parsers/moin.py Thu Jul 26 20:09:48 2018 +0200
4.3 @@ -21,7 +21,6 @@
4.4
4.5 # Document transformations.
4.6
4.7 -from moinformat.links import get_linker
4.8 from moinformat.macros import get_macro
4.9
4.10 # Parser functionality and pattern definition.
4.11 @@ -68,7 +67,6 @@
4.12 # Record certain node occurrences for later evaluation.
4.13
4.14 self.macros = []
4.15 - self.links = []
4.16
4.17 # Principal parser methods.
4.18
4.19 @@ -119,31 +117,6 @@
4.20 macro = macro_cls(node, self.region)
4.21 macro.evaluate()
4.22
4.23 - # Link translation.
4.24 -
4.25 - def translate_links(self, scheme, pagename):
4.26 -
4.27 - """
4.28 - Translate the link nodes in the document for the given 'scheme' and
4.29 - employing the given document 'pagename'.
4.30 - """
4.31 -
4.32 - # Obtain a class for the named linker.
4.33 -
4.34 - linker_cls = get_linker(scheme)
4.35 - if not linker_cls:
4.36 - return
4.37 -
4.38 - # Instantiate the class with document metadata.
4.39 -
4.40 - linker = linker_cls(pagename)
4.41 -
4.42 - for node in self.links:
4.43 -
4.44 - # Translate the link.
4.45 -
4.46 - node.target = linker.translate(node.target)
4.47 -
4.48
4.49
4.50 # Parser methods supporting different page features.
4.51 @@ -518,10 +491,6 @@
4.52 link = Link(text and [Text(text)], target)
4.53 region.append_inline(link)
4.54
4.55 - # Record the link for later processing.
4.56 -
4.57 - self.root.links.append(link)
4.58 -
4.59 def parse_macro(self, region):
4.60 name = self.match_group("name")
4.61 args = self.match_group("args")
5.1 --- a/moinformat/serialisers/__init__.py Thu Jul 26 17:05:08 2018 +0200
5.2 +++ b/moinformat/serialisers/__init__.py Thu Jul 26 20:09:48 2018 +0200
5.3 @@ -24,12 +24,17 @@
5.4
5.5 # Top-level functions.
5.6
5.7 -def serialise(doc, serialiser=MoinSerialiser):
5.8 +def serialise(doc, serialiser=None):
5.9
5.10 - "Serialise 'doc' using 'serialiser' or the Moin serialiser if omitted."
5.11 + """
5.12 + Serialise 'doc' using the given 'serialiser' instance or the Moin serialiser
5.13 + if omitted.
5.14 + """
5.15
5.16 l = []
5.17 - doc.to_string(serialiser(l.append, serialisers))
5.18 + serialiser = serialiser or MoinSerialiser(formats=serialisers)
5.19 + serialiser.out = l.append
5.20 + doc.to_string(serialiser)
5.21 return "".join(l)
5.22
5.23 # vim: tabstop=4 expandtab shiftwidth=4
6.1 --- a/moinformat/serialisers/common.py Thu Jul 26 17:05:08 2018 +0200
6.2 +++ b/moinformat/serialisers/common.py Thu Jul 26 20:09:48 2018 +0200
6.3 @@ -25,15 +25,17 @@
6.4
6.5 format = None # defined by subclasses
6.6
6.7 - def __init__(self, out, formats=None):
6.8 + def __init__(self, out=None, formats=None, linker=None):
6.9
6.10 """
6.11 - Initialise the serialiser with an 'out' callable and an optional
6.12 - 'formats' mapping from names to serialiser classes.
6.13 + Initialise the serialiser with an 'out' callable, an optional 'formats'
6.14 + mapping from names to serialiser classes, and an optional 'linker'
6.15 + object for translating links.
6.16 """
6.17
6.18 self.out = out
6.19 self.formats = formats
6.20 + self.linker = linker
6.21 self.init()
6.22
6.23 def init(self):
6.24 @@ -43,7 +45,8 @@
6.25 pass
6.26
6.27 def __repr__(self):
6.28 - return "%s(%r, %r)" % (self.__class__.__name__, self.out, self.formats)
6.29 + return "%s(%r, %r, %r)" % (self.__class__.__name__, self.out,
6.30 + self.formats, self.linker)
6.31
6.32 def escape_attr(s):
6.33
7.1 --- a/moinformat/serialisers/html/moin.py Thu Jul 26 17:05:08 2018 +0200
7.2 +++ b/moinformat/serialisers/html/moin.py Thu Jul 26 20:09:48 2018 +0200
7.3 @@ -97,6 +97,8 @@
7.4 self.out("</big>")
7.5
7.6 def start_link(self, target):
7.7 + if self.linker:
7.8 + target = self.linker.translate(target)
7.9 self.out('<a href="%s">' % escape_attr(target))
7.10
7.11 def end_link(self):
8.1 --- a/moinformat/tree/moin.py Thu Jul 26 17:05:08 2018 +0200
8.2 +++ b/moinformat/tree/moin.py Thu Jul 26 20:09:48 2018 +0200
8.3 @@ -146,8 +146,9 @@
8.4 # Retain the same serialiser if no appropriate serialiser could be
8.5 # obtained.
8.6
8.7 - region_out = serialiser_cls and serialiser_cls(out.out, out.formats) \
8.8 - or out
8.9 + region_out = serialiser_cls and serialiser_cls is not out and \
8.10 + serialiser_cls(out.out, out.formats, out.linker) or \
8.11 + out
8.12
8.13 # Serialise the region.
8.14
9.1 --- a/tests/test_parser.py Thu Jul 26 17:05:08 2018 +0200
9.2 +++ b/tests/test_parser.py Thu Jul 26 20:09:48 2018 +0200
9.3 @@ -12,7 +12,7 @@
9.4 if split(parent)[1] == "MoinLight":
9.5 sys.path.append(parent)
9.6
9.7 -from moinformat import get_serialiser, parse, serialise
9.8 +from moinformat import make_serialiser, parse, serialise
9.9 from moinformat.tree.moin import Container
9.10 from glob import glob
9.11
9.12 @@ -36,7 +36,7 @@
9.13 print "-" * 60
9.14 print s
9.15 print "-" * 60
9.16 - print serialise(d, get_serialiser("html"))
9.17 + print serialise(d, make_serialiser("html"))
9.18 print "-" * 60
9.19 print
9.20