1.1 --- a/convert.py Mon Aug 13 22:54:01 2018 +0200
1.2 +++ b/convert.py Tue Aug 14 22:33:30 2018 +0200
1.3 @@ -1,7 +1,6 @@
1.4 #!/usr/bin/env python
1.5
1.6 -from moinformat import make_input, make_linker, make_output, make_parser, \
1.7 - make_serialiser, make_theme, parse, serialise
1.8 +from moinformat import make_parser, make_serialiser, Metadata, parse, serialise
1.9 from os.path import split
1.10 import sys
1.11
1.12 @@ -18,8 +17,8 @@
1.13
1.14 return mapping
1.15
1.16 -def getvalue(values):
1.17 - return values and values[0] or None
1.18 +def getvalue(values, default=None):
1.19 + return values and values[0] or default
1.20
1.21 def main():
1.22 dirname, progname = split(sys.argv[0])
1.23 @@ -38,6 +37,7 @@
1.24 output_encodings = []
1.25 theme_names = []
1.26 pagenames = []
1.27 + root_pagenames = []
1.28
1.29 # Flags.
1.30
1.31 @@ -122,6 +122,12 @@
1.32 l = pagenames
1.33 continue
1.34
1.35 + # Switch to collecting root page names.
1.36 +
1.37 + elif arg == "--root":
1.38 + l = root_pagenames
1.39 + continue
1.40 +
1.41 # Switch to collecting theme names.
1.42
1.43 elif arg == "--theme":
1.44 @@ -143,40 +149,33 @@
1.45 l = filenames
1.46
1.47 format = formats and formats[0] or "html"
1.48 -
1.49 - # Derive a proper mapping from the given list of values.
1.50 -
1.51 - mapping = getmapping(mappings)
1.52 -
1.53 - # Obtain encodings.
1.54 -
1.55 - input_encoding = getvalue(input_encodings)
1.56 - output_encoding = getvalue(output_encodings)
1.57 -
1.58 - # Obtain the input and output locations and contexts.
1.59 -
1.60 input_dir = getvalue(input_dirs)
1.61 output_dir = getvalue(output_dirs)
1.62
1.63 - input_page_sep = getvalue(input_page_seps)
1.64 -
1.65 - input_context = input_dir and (getvalue(input_dir_types) or
1.66 - "directory") or "standalone"
1.67 -
1.68 - input = make_input(input_context, {"encoding" : input_encoding,
1.69 - "filename" : input_dir,
1.70 - "separator" : input_page_sep})
1.71 + # Define metadata.
1.72
1.73 - output_context = output_dir and "directory" or "standalone"
1.74 -
1.75 - output = make_output(output_context, {"encoding" : output_encoding,
1.76 - "filename" : output_dir})
1.77 + metadata = Metadata({
1.78 + "input_context" : input_dir and \
1.79 + getvalue(input_dir_types, "directory") or \
1.80 + "standalone",
1.81 + "input_encoding" : getvalue(input_encodings),
1.82 + "input_filename" : input_dir,
1.83 + "input_separator" : getvalue(input_page_seps),
1.84 + "link_format" : format,
1.85 + "mapping" : getmapping(mappings),
1.86 + "output_context" : output_dir and "directory" or "standalone",
1.87 + "output_encoding" : getvalue(output_encodings),
1.88 + "output_format" : format,
1.89 + "output_filename" : output_dir,
1.90 + "root_pagename" : getvalue(root_pagenames, "FrontPage"),
1.91 + "theme_name" : not fragment and \
1.92 + "%s.%s" % (getvalue(theme_names, "default"), format) or None,
1.93 + })
1.94
1.95 - # Obtain a theme name.
1.96 + # Define the input context and theme.
1.97
1.98 - theme_name = not fragment and (getvalue(theme_names) or "default") or None
1.99 -
1.100 - theme = None
1.101 + input = metadata.get_input()
1.102 + theme = metadata.get_theme()
1.103
1.104 # Treat filenames as pagenames if an input directory is indicated and if no
1.105 # pagenames are explicitly specified.
1.106 @@ -209,6 +208,7 @@
1.107 # Define a pagename if missing.
1.108
1.109 pagename = pagename or split(filename)[-1]
1.110 + metadata.set("pagename", pagename)
1.111
1.112 # Read either from a filename or using a pagename.
1.113
1.114 @@ -219,7 +219,7 @@
1.115
1.116 # Parse the page content.
1.117
1.118 - p = make_parser()
1.119 + p = make_parser(metadata)
1.120 d = parse(pagetext, p)
1.121
1.122 if macros:
1.123 @@ -233,20 +233,11 @@
1.124
1.125 # Otherwise, serialise the document.
1.126
1.127 - # Obtain a linker using format and pagename details.
1.128 -
1.129 - linker = make_linker(format, pagename, mapping)
1.130 -
1.131 # Obtain a serialiser using the configuration.
1.132
1.133 - serialiser = make_serialiser(format, output, linker, pagename)
1.134 + serialiser = make_serialiser(metadata)
1.135 outtext = serialise(d, serialiser)
1.136
1.137 - # Obtain a theme object for theming.
1.138 -
1.139 - theme = theme_name and make_theme("%s.%s" % (theme_name, format),
1.140 - output, linker, pagename)
1.141 -
1.142 # With a theme, apply it to the text.
1.143
1.144 if theme:
1.145 @@ -255,6 +246,8 @@
1.146 # If reading from a file, show the result. Otherwise, write to the
1.147 # output context.
1.148
1.149 + output = metadata.get_output()
1.150 +
1.151 if not output.can_write():
1.152 print outtext
1.153 else: