1.1 --- a/convert.py Mon Aug 13 17:11:54 2018 +0200
1.2 +++ b/convert.py Mon Aug 13 17:55:16 2018 +0200
1.3 @@ -1,7 +1,7 @@
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, parse, serialise
1.8 + make_serialiser, make_theme, parse, serialise
1.9 from os.path import split
1.10 import sys
1.11
1.12 @@ -36,11 +36,13 @@
1.13 mappings = []
1.14 output_dirs = []
1.15 output_encodings = []
1.16 + theme_names = []
1.17 pagenames = []
1.18
1.19 # Flags.
1.20
1.21 all = False
1.22 + fragment = False
1.23 macros = False
1.24 tree = False
1.25
1.26 @@ -61,6 +63,11 @@
1.27 elif arg == "--all":
1.28 all = True
1.29
1.30 + # Detect fragment output (if serialising).
1.31 +
1.32 + elif arg == "--fragment":
1.33 + fragment = True
1.34 +
1.35 # Switch to collecting formats.
1.36
1.37 elif arg == "--format":
1.38 @@ -115,6 +122,12 @@
1.39 l = pagenames
1.40 continue
1.41
1.42 + # Switch to collecting theme names.
1.43 +
1.44 + elif arg == "--theme":
1.45 + l = theme_names
1.46 + continue
1.47 +
1.48 # Collect options and arguments.
1.49
1.50 else:
1.51 @@ -140,7 +153,7 @@
1.52 input_encoding = getvalue(input_encodings)
1.53 output_encoding = getvalue(output_encodings)
1.54
1.55 - # Obtain the input and output locations.
1.56 + # Obtain the input and output locations and contexts.
1.57
1.58 input_dir = getvalue(input_dirs)
1.59 output_dir = getvalue(output_dirs)
1.60 @@ -159,6 +172,12 @@
1.61 output = make_output(output_context, {"encoding" : output_encoding,
1.62 "filename" : output_dir})
1.63
1.64 + # Obtain a theme name.
1.65 +
1.66 + theme_name = not fragment and (getvalue(theme_names) or "default") or None
1.67 +
1.68 + theme = None
1.69 +
1.70 # Treat filenames as pagenames if an input directory is indicated and if no
1.71 # pagenames are explicitly specified.
1.72
1.73 @@ -223,6 +242,16 @@
1.74 serialiser = make_serialiser(format, output, linker, pagename)
1.75 outtext = serialise(d, serialiser)
1.76
1.77 + # Obtain a theme object for theming.
1.78 +
1.79 + theme = theme_name and make_theme("%s.%s" % (theme_name, format),
1.80 + output, linker, pagename)
1.81 +
1.82 + # With a theme, apply it to the text.
1.83 +
1.84 + if theme:
1.85 + outtext = theme.apply(outtext)
1.86 +
1.87 # If reading from a file, show the result. Otherwise, write to the
1.88 # output context.
1.89
1.90 @@ -232,6 +261,11 @@
1.91 output.writepage(outtext, pagename)
1.92 print >>sys.stderr, pagename
1.93
1.94 + # Install any theme resources.
1.95 +
1.96 + if theme:
1.97 + theme.install_resources()
1.98 +
1.99 if __name__ == "__main__":
1.100 main()
1.101