1.1 --- a/convert.py Sun Nov 25 16:59:19 2018 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,265 +0,0 @@
1.4 -#!/usr/bin/env python
1.5 -
1.6 -from moinformat import make_parser, make_serialiser, Metadata, parse, serialise
1.7 -from os.path import split
1.8 -import sys
1.9 -
1.10 -def getmapping(mappings):
1.11 - mapping = {}
1.12 - key = None
1.13 -
1.14 - for arg in mappings:
1.15 - if key is None:
1.16 - key = arg
1.17 - else:
1.18 - mapping[key] = arg
1.19 - key = None
1.20 -
1.21 - return mapping
1.22 -
1.23 -def getvalue(values, default=None):
1.24 - return values and values[0] or default
1.25 -
1.26 -def main():
1.27 - dirname, progname = split(sys.argv[0])
1.28 - args = sys.argv[1:]
1.29 -
1.30 - # Option values.
1.31 -
1.32 - l = filenames = []
1.33 - formats = []
1.34 - input_dir_types = []
1.35 - input_dirs = []
1.36 - input_encodings = []
1.37 - input_page_seps = []
1.38 - mappings = []
1.39 - output_dirs = []
1.40 - output_encodings = []
1.41 - theme_names = []
1.42 - pagenames = []
1.43 - root_pagenames = []
1.44 -
1.45 - # Flags.
1.46 -
1.47 - all = False
1.48 - fragment = False
1.49 - macros = False
1.50 - tree = False
1.51 -
1.52 - for arg in args:
1.53 -
1.54 - # Detect tree output.
1.55 -
1.56 - if arg == "--tree":
1.57 - tree = True
1.58 -
1.59 - # Detect macro evaluation.
1.60 -
1.61 - elif arg == "--macros":
1.62 - macros = True
1.63 -
1.64 - # Detect all documents.
1.65 -
1.66 - elif arg == "--all":
1.67 - all = True
1.68 -
1.69 - # Detect fragment output (if serialising).
1.70 -
1.71 - elif arg == "--fragment":
1.72 - fragment = True
1.73 -
1.74 - # Switch to collecting formats.
1.75 -
1.76 - elif arg == "--format":
1.77 - l = formats
1.78 - continue
1.79 -
1.80 - # Switch to collecting input locations.
1.81 -
1.82 - elif arg == "--input-dir":
1.83 - l = input_dirs
1.84 - continue
1.85 -
1.86 - # Switch to collecting input context types.
1.87 -
1.88 - elif arg == "--input-dir-type":
1.89 - l = input_dir_types
1.90 - continue
1.91 -
1.92 - # Switch to collecting input encodings.
1.93 -
1.94 - elif arg == "--input-encoding":
1.95 - l = input_encodings
1.96 - continue
1.97 -
1.98 - # Switch to collecting input page hierarchy separators.
1.99 -
1.100 - elif arg == "--input-page-sep":
1.101 - l = input_page_seps
1.102 - continue
1.103 -
1.104 - # Switch to collecting mappings.
1.105 -
1.106 - elif arg == "--mapping":
1.107 - l = mappings
1.108 - continue
1.109 -
1.110 - # Switch to collecting output locations.
1.111 -
1.112 - elif arg == "--output-dir":
1.113 - l = output_dirs
1.114 - continue
1.115 -
1.116 - # Switch to collecting output encodings.
1.117 -
1.118 - elif arg == "--output-encoding":
1.119 - l = output_encodings
1.120 - continue
1.121 -
1.122 - # Switch to collecting page names.
1.123 -
1.124 - elif arg == "--pagename":
1.125 - l = pagenames
1.126 - continue
1.127 -
1.128 - # Switch to collecting root page names.
1.129 -
1.130 - elif arg == "--root":
1.131 - l = root_pagenames
1.132 - continue
1.133 -
1.134 - # Switch to collecting theme names.
1.135 -
1.136 - elif arg == "--theme":
1.137 - l = theme_names
1.138 - continue
1.139 -
1.140 - # Collect options and arguments.
1.141 -
1.142 - else:
1.143 - l.append(arg)
1.144 -
1.145 - # Collect multiple mappings.
1.146 -
1.147 - if l is mappings:
1.148 - continue
1.149 -
1.150 - # Collect filenames normally.
1.151 -
1.152 - l = filenames
1.153 -
1.154 - format = formats and formats[0] or "html"
1.155 - input_dir = getvalue(input_dirs)
1.156 - output_dir = getvalue(output_dirs)
1.157 -
1.158 - # Define metadata.
1.159 -
1.160 - metadata = Metadata({
1.161 - "input_context" : input_dir and \
1.162 - getvalue(input_dir_types, "directory") or \
1.163 - "standalone",
1.164 - "input_encoding" : getvalue(input_encodings),
1.165 - "input_filename" : input_dir,
1.166 - "input_separator" : getvalue(input_page_seps),
1.167 - "link_format" : format,
1.168 - "mapping" : getmapping(mappings),
1.169 - "output_context" : output_dir and "directory" or "standalone",
1.170 - "output_encoding" : getvalue(output_encodings),
1.171 - "output_format" : format,
1.172 - "output_filename" : output_dir,
1.173 - "root_pagename" : getvalue(root_pagenames, "FrontPage"),
1.174 - "theme_name" : not fragment and \
1.175 - "%s.%s" % (getvalue(theme_names, "default"), format) or None,
1.176 - })
1.177 -
1.178 - # Define the input context and theme.
1.179 -
1.180 - input = metadata.get_input()
1.181 - theme = metadata.get_theme()
1.182 -
1.183 - # Treat filenames as pagenames if an input directory is indicated and if no
1.184 - # pagenames are explicitly specified.
1.185 -
1.186 - if input_dir:
1.187 - if pagenames:
1.188 - print >>sys.stderr, """\
1.189 -Explicit pagenames (indicated using --pagename) are only to be specified when
1.190 -providing filenames without an input directory (indicated using --input-dir).
1.191 -
1.192 -To indicate pagenames within an input directory, omit any --pagename flags."""
1.193 - sys.exit(1)
1.194 -
1.195 - if all:
1.196 - if filenames:
1.197 - print >>sys.stderr, """\
1.198 -Using --all overrides any indicated pagenames. Either --all or the filenames
1.199 -should be omitted."""
1.200 - sys.exit(1)
1.201 - else:
1.202 - filenames = input.all()
1.203 -
1.204 - pagenames = filenames
1.205 - filenames = []
1.206 -
1.207 - # Open each file or page, parse the content, serialise the document.
1.208 -
1.209 - for pagename, filename in map(None, pagenames, filenames):
1.210 -
1.211 - # Define a pagename if missing.
1.212 -
1.213 - pagename = pagename or split(filename)[-1]
1.214 - metadata.set("pagename", pagename)
1.215 -
1.216 - # Read either from a filename or using a pagename.
1.217 -
1.218 - if filename:
1.219 - pagetext = input.readfile(filename)
1.220 - else:
1.221 - pagetext = input.readpage(pagename)
1.222 -
1.223 - # Parse the page content.
1.224 -
1.225 - p = make_parser(metadata)
1.226 - d = parse(pagetext, p)
1.227 -
1.228 - if macros:
1.229 - p.evaluate_macros()
1.230 -
1.231 - # Show a document tree for debugging purposes, if requested.
1.232 -
1.233 - if tree:
1.234 - print d.prettyprint()
1.235 - continue
1.236 -
1.237 - # Otherwise, serialise the document.
1.238 -
1.239 - # Obtain a serialiser using the configuration.
1.240 -
1.241 - serialiser = make_serialiser(metadata)
1.242 - outtext = serialise(d, serialiser)
1.243 -
1.244 - # With a theme, apply it to the text.
1.245 -
1.246 - if theme:
1.247 - outtext = theme.apply(outtext)
1.248 -
1.249 - # If reading from a file, show the result. Otherwise, write to the
1.250 - # output context.
1.251 -
1.252 - output = metadata.get_output()
1.253 -
1.254 - if not output.can_write():
1.255 - print outtext
1.256 - else:
1.257 - output.writepage(outtext, pagename)
1.258 - print >>sys.stderr, pagename
1.259 -
1.260 - # Install any theme resources.
1.261 -
1.262 - if theme:
1.263 - theme.install_resources()
1.264 -
1.265 -if __name__ == "__main__":
1.266 - main()
1.267 -
1.268 -# vim: tabstop=4 expandtab shiftwidth=4