1.1 --- a/moinformat/imports.py Mon Jul 30 23:24:20 2018 +0200
1.2 +++ b/moinformat/imports.py Tue Jul 31 00:07:46 2018 +0200
1.3 @@ -19,10 +19,12 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 -from os.path import isdir, join, splitext
1.8 +from os.path import isdir, join, split, splitext
1.9 from os import listdir
1.10 from importlib import import_module
1.11
1.12 +reserved = ["__init__", "common", "manifest"]
1.13 +
1.14 def get_extensions(dirname, modname, stores, reserved, prefix=None):
1.15
1.16 "Import extensions inside 'dirname'."
1.17 @@ -46,4 +48,37 @@
1.18 store_name = prefix and "%s.%s" % (prefix, leafname) or leafname
1.19 stores[store_name] = import_module("%s.%s" % (modname, leafname))
1.20
1.21 +def get_mapping(modules, get_key, get_value):
1.22 +
1.23 + """
1.24 + Using the 'modules' mapping, employ 'get_key' and 'get_value' to register
1.25 + objects provided by the modules in a mapping.
1.26 + """
1.27 +
1.28 + mapping = {}
1.29 +
1.30 + # Use the callables to obtain the keys and values from modules.
1.31 +
1.32 + for name, module in modules.items():
1.33 + mapping[get_key(name, module)] = get_value(module)
1.34 +
1.35 + return mapping
1.36 +
1.37 +def get_modules(module_file, module_name):
1.38 +
1.39 + """
1.40 + Obtain details of the indicated module's package using 'module_file',
1.41 + corresponding to a __file__ attribute, and 'module_name', corresponding to
1.42 + a __name__ attribute.
1.43 + """
1.44 +
1.45 + dirname = split(module_file)[0]
1.46 + package = module_name.rsplit(".", 1)[0]
1.47 +
1.48 + # Define an attribute mapping names to modules.
1.49 +
1.50 + modules = {}
1.51 + get_extensions(dirname, package, modules, reserved)
1.52 + return modules
1.53 +
1.54 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/moinformat/input/manifest.py Mon Jul 30 23:24:20 2018 +0200
2.2 +++ b/moinformat/input/manifest.py Tue Jul 31 00:07:46 2018 +0200
2.3 @@ -19,29 +19,17 @@
2.4 this program. If not, see <http://www.gnu.org/licenses/>.
2.5 """
2.6
2.7 -from moinformat.imports import get_extensions
2.8 -from os.path import split
2.9 -
2.10 -reserved = ["__init__", "common", "manifest"]
2.11 -
2.12 -# Obtain details of this module's package.
2.13 -
2.14 -dirname = split(__file__)[0]
2.15 -package = __name__.rsplit(".", 1)[0]
2.16 +from moinformat.imports import get_extensions, get_mapping, get_modules
2.17
2.18 # Define an attribute mapping names to modules.
2.19
2.20 -modules = {}
2.21 -get_extensions(dirname, package, modules, reserved)
2.22 +modules = get_modules(__file__, __name__)
2.23
2.24 # Obtain all input contexts.
2.25
2.26 -inputs = {}
2.27 -
2.28 -# Use names declared in each class to register the handlers:
2.29 +# Use names declared in each class to register the contexts:
2.30 # input.name -> input
2.31
2.32 -for module in modules.values():
2.33 - inputs[module.input.name] = module.input
2.34 +inputs = get_mapping(modules, lambda n, m: m.input.name, lambda m: m.input)
2.35
2.36 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/moinformat/links/manifest.py Mon Jul 30 23:24:20 2018 +0200
3.2 +++ b/moinformat/links/manifest.py Tue Jul 31 00:07:46 2018 +0200
3.3 @@ -19,29 +19,17 @@
3.4 this program. If not, see <http://www.gnu.org/licenses/>.
3.5 """
3.6
3.7 -from moinformat.imports import get_extensions
3.8 -from os.path import split
3.9 -
3.10 -reserved = ["__init__", "common", "manifest"]
3.11 -
3.12 -# Obtain details of this module's package.
3.13 -
3.14 -dirname = split(__file__)[0]
3.15 -package = __name__.rsplit(".", 1)[0]
3.16 +from moinformat.imports import get_extensions, get_mapping, get_modules
3.17
3.18 # Define an attribute mapping names to modules.
3.19
3.20 -modules = {}
3.21 -get_extensions(dirname, package, modules, reserved)
3.22 +modules = get_modules(__file__, __name__)
3.23
3.24 # Obtain all linkers.
3.25
3.26 -linkers = {}
3.27 -
3.28 -# Use names declared in each handler to register the handlers:
3.29 +# Use names declared in each class to register the linkers:
3.30 # linker.name -> linker
3.31
3.32 -for module in modules.values():
3.33 - linkers[module.linker.name] = module.linker
3.34 +linkers = get_mapping(modules, lambda n, m: m.linker.name, lambda m: m.linker)
3.35
3.36 # vim: tabstop=4 expandtab shiftwidth=4
4.1 --- a/moinformat/macros/br.py Mon Jul 30 23:24:20 2018 +0200
4.2 +++ b/moinformat/macros/br.py Tue Jul 31 00:07:46 2018 +0200
4.3 @@ -32,16 +32,10 @@
4.4
4.5 "Evaluate the macro, producing a line break node."
4.6
4.7 - macro = self.node
4.8 - nodes = macro.parent.nodes
4.9 -
4.10 # Replace the macro node with the line break.
4.11
4.12 - try:
4.13 - i = nodes.index(macro)
4.14 - nodes[i] = LineBreak()
4.15 - except ValueError:
4.16 - pass
4.17 + macro = self.node
4.18 + macro.parent.replace(macro, LineBreak())
4.19
4.20 macro = LineBreakMacro
4.21
5.1 --- a/moinformat/macros/manifest.py Mon Jul 30 23:24:20 2018 +0200
5.2 +++ b/moinformat/macros/manifest.py Tue Jul 31 00:07:46 2018 +0200
5.3 @@ -19,29 +19,17 @@
5.4 this program. If not, see <http://www.gnu.org/licenses/>.
5.5 """
5.6
5.7 -from moinformat.imports import get_extensions
5.8 -from os.path import split
5.9 -
5.10 -reserved = ["__init__", "common", "manifest"]
5.11 -
5.12 -# Obtain details of this module's package.
5.13 -
5.14 -dirname = split(__file__)[0]
5.15 -package = __name__.rsplit(".", 1)[0]
5.16 +from moinformat.imports import get_extensions, get_mapping, get_modules
5.17
5.18 # Define an attribute mapping names to modules.
5.19
5.20 -modules = {}
5.21 -get_extensions(dirname, package, modules, reserved)
5.22 +modules = get_modules(__file__, __name__)
5.23
5.24 # Obtain all macros.
5.25
5.26 -macros = {}
5.27 -
5.28 -# Use names declared in each handler to register the handlers:
5.29 +# Use names declared in each class to register the handlers:
5.30 # macro.name -> macro
5.31
5.32 -for module in modules.values():
5.33 - macros[module.macro.name] = module.macro
5.34 +macros = get_mapping(modules, lambda n, m: m.macro.name, lambda m: m.macro)
5.35
5.36 # vim: tabstop=4 expandtab shiftwidth=4
6.1 --- a/moinformat/macros/toc.py Mon Jul 30 23:24:20 2018 +0200
6.2 +++ b/moinformat/macros/toc.py Tue Jul 31 00:07:46 2018 +0200
6.3 @@ -134,6 +134,7 @@
6.4 items.append(item)
6.5
6.6 # Replace the macro node's children with the top-level list.
6.7 + # The macro cannot be replaced because it will be appearing inline.
6.8
6.9 self.node.nodes = [lists[0]]
6.10
7.1 --- a/moinformat/output/manifest.py Mon Jul 30 23:24:20 2018 +0200
7.2 +++ b/moinformat/output/manifest.py Tue Jul 31 00:07:46 2018 +0200
7.3 @@ -19,29 +19,17 @@
7.4 this program. If not, see <http://www.gnu.org/licenses/>.
7.5 """
7.6
7.7 -from moinformat.imports import get_extensions
7.8 -from os.path import split
7.9 -
7.10 -reserved = ["__init__", "common", "manifest"]
7.11 -
7.12 -# Obtain details of this module's package.
7.13 -
7.14 -dirname = split(__file__)[0]
7.15 -package = __name__.rsplit(".", 1)[0]
7.16 +from moinformat.imports import get_extensions, get_mapping, get_modules
7.17
7.18 # Define an attribute mapping names to modules.
7.19
7.20 -modules = {}
7.21 -get_extensions(dirname, package, modules, reserved)
7.22 +modules = get_modules(__file__, __name__)
7.23
7.24 # Obtain all output contexts.
7.25
7.26 -outputs = {}
7.27 -
7.28 -# Use names declared in each class to register the handlers:
7.29 +# Use names declared in each class to register the contexts:
7.30 # output.name -> output
7.31
7.32 -for module in modules.values():
7.33 - outputs[module.output.name] = module.output
7.34 +outputs = get_mapping(modules, lambda n, m: m.output.name, lambda m: m.output)
7.35
7.36 # vim: tabstop=4 expandtab shiftwidth=4
8.1 --- a/moinformat/parsers/graphviz.py Mon Jul 30 23:24:20 2018 +0200
8.2 +++ b/moinformat/parsers/graphviz.py Tue Jul 31 00:07:46 2018 +0200
8.3 @@ -33,6 +33,8 @@
8.4
8.5 "A parser for Graphviz content, identifying format directives."
8.6
8.7 + format = "graphviz"
8.8 +
8.9 # Parser handler methods.
8.10
8.11 def parse_directive(self, region):
9.1 --- a/moinformat/parsers/manifest.py Mon Jul 30 23:24:20 2018 +0200
9.2 +++ b/moinformat/parsers/manifest.py Tue Jul 31 00:07:46 2018 +0200
9.3 @@ -3,7 +3,7 @@
9.4 """
9.5 Moin wiki parser manifest.
9.6
9.7 -Copyright (C) 2017 Paul Boddie <paul@boddie.org.uk>
9.8 +Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk>
9.9
9.10 This program is free software; you can redistribute it and/or modify it under
9.11 the terms of the GNU General Public License as published by the Free Software
9.12 @@ -19,26 +19,17 @@
9.13 this program. If not, see <http://www.gnu.org/licenses/>.
9.14 """
9.15
9.16 -from moinformat.imports import get_extensions
9.17 -from os.path import split
9.18 -
9.19 -reserved = ["__init__", "common", "manifest"]
9.20 -
9.21 -# Obtain details of this module's package.
9.22 -
9.23 -dirname = split(__file__)[0]
9.24 -package = __name__.rsplit(".", 1)[0]
9.25 +from moinformat.imports import get_extensions, get_mapping, get_modules
9.26
9.27 # Define an attribute mapping names to modules.
9.28
9.29 -modules = {}
9.30 -get_extensions(dirname, package, modules, reserved)
9.31 +modules = get_modules(__file__, __name__)
9.32
9.33 # Obtain all parsers.
9.34
9.35 -parsers = {}
9.36 +# Use names declared in each class to register the parsers:
9.37 +# parser.format -> parser
9.38
9.39 -for module_name, module in modules.items():
9.40 - parsers[module_name] = module.parser
9.41 +parsers = get_mapping(modules, lambda n, m: m.parser.format, lambda m: m.parser)
9.42
9.43 # vim: tabstop=4 expandtab shiftwidth=4
10.1 --- a/moinformat/parsers/moin.py Mon Jul 30 23:24:20 2018 +0200
10.2 +++ b/moinformat/parsers/moin.py Tue Jul 31 00:07:46 2018 +0200
10.3 @@ -48,6 +48,8 @@
10.4
10.5 "A wiki region parser."
10.6
10.7 + format = "moin"
10.8 +
10.9 def __init__(self, formats=None, root=None):
10.10
10.11 """
11.1 --- a/moinformat/parsers/table.py Mon Jul 30 23:24:20 2018 +0200
11.2 +++ b/moinformat/parsers/table.py Tue Jul 31 00:07:46 2018 +0200
11.3 @@ -33,6 +33,8 @@
11.4
11.5 "A parser for improved table syntax."
11.6
11.7 + format = "table"
11.8 +
11.9 # Principal parser methods.
11.10
11.11 def parse_region_content(self, items, region):
12.1 --- a/moinformat/serialisers/manifest.py Mon Jul 30 23:24:20 2018 +0200
12.2 +++ b/moinformat/serialisers/manifest.py Tue Jul 31 00:07:46 2018 +0200
12.3 @@ -3,7 +3,7 @@
12.4 """
12.5 Moin wiki serialiser manifest.
12.6
12.7 -Copyright (C) 2017 Paul Boddie <paul@boddie.org.uk>
12.8 +Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk>
12.9
12.10 This program is free software; you can redistribute it and/or modify it under
12.11 the terms of the GNU General Public License as published by the Free Software
12.12 @@ -19,26 +19,17 @@
12.13 this program. If not, see <http://www.gnu.org/licenses/>.
12.14 """
12.15
12.16 -from moinformat.imports import get_extensions
12.17 -from os.path import split
12.18 -
12.19 -reserved = ["__init__", "common", "manifest"]
12.20 -
12.21 -# Obtain details of this module's package.
12.22 -
12.23 -dirname = split(__file__)[0]
12.24 -package = __name__.rsplit(".", 1)[0]
12.25 +from moinformat.imports import get_extensions, get_mapping, get_modules
12.26
12.27 # Define an attribute mapping names to modules.
12.28
12.29 -modules = {}
12.30 -get_extensions(dirname, package, modules, reserved)
12.31 +modules = get_modules(__file__, __name__)
12.32
12.33 # Obtain all serialisers.
12.34
12.35 -serialisers = {}
12.36 +# Use names declared in each class to register the handlers:
12.37 +# serialiser.format -> serialiser
12.38
12.39 -for module_name, module in modules.items():
12.40 - serialisers[module_name] = module.serialiser
12.41 +serialisers = get_mapping(modules, lambda n, m: n, lambda m: m.serialiser)
12.42
12.43 # vim: tabstop=4 expandtab shiftwidth=4
13.1 --- a/moinformat/tree/moin.py Mon Jul 30 23:24:20 2018 +0200
13.2 +++ b/moinformat/tree/moin.py Tue Jul 31 00:07:46 2018 +0200
13.3 @@ -106,6 +106,13 @@
13.4 if text:
13.5 self.append(text)
13.6
13.7 + def replace(self, old, new):
13.8 +
13.9 + "Replace 'old' with 'new' in the children."
13.10 +
13.11 + i = self.nodes.index(old)
13.12 + self.nodes[i] = new
13.13 +
13.14 def __str__(self):
13.15 return self.prettyprint()
13.16