# HG changeset patch # User Paul Boddie # Date 1543244750 -3600 # Node ID 6425c9e2744f21d5779eab1d1465644f0a96104f # Parent 104491ce1ed4d3398018b2b939eb85457791f4cd Renamed the conversion tool. diff -r 104491ce1ed4 -r 6425c9e2744f convert.py --- a/convert.py Sun Nov 25 21:28:12 2018 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,313 +0,0 @@ -#!/usr/bin/env python - -""" -Moin wiki format converter. - -Copyright (C) 2018 Paul Boddie - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 3 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -FOR A PARTICULAR PURPOSE. See the GNU General Public License for more -details. - -You should have received a copy of the GNU General Public License along with -this program. If not, see . -""" - -from moinformat import make_parser, make_serialiser, Metadata, parse, serialise -from os.path import split -import sys - -# Long messages. - -message_all_with_filenames = """\ -Using --all overrides any indicated pagenames. Either --all or the filenames -should be omitted.""" - -message_explicit_pagenames = """\ -Explicit pagenames (indicated using --pagename) are only to be specified when -providing filenames without an input directory (indicated using --input-dir). - -To indicate pagenames within an input directory, omit any --pagename flags.""" - - - -# Options management. - -def getmapping(mappings): - - """ - Return the given 'mappings' - a collection of key-then-value items - as a - dictionary. - """ - - mapping = {} - key = None - - for arg in mappings: - if key is None: - key = arg - else: - mapping[key] = arg - key = None - - return mapping - -def getvalue(values, default=None): - - """ - Return the first value from 'values' or 'default' if 'values' is empty or - the first value tests as false. - """ - - return values and values[0] or default - - - -# Main program. - -def main(): - - "Interpret program options and perform the conversion." - - dirname, progname = split(sys.argv[0]) - args = sys.argv[1:] - - # Option values. - - l = filenames = [] - formats = [] - input_dir_types = [] - input_dirs = [] - input_encodings = [] - input_page_seps = [] - mappings = [] - output_dirs = [] - output_encodings = [] - theme_names = [] - pagenames = [] - root_pagenames = [] - - # Flags. - - all = False - fragment = False - macros = False - tree = False - - for arg in args: - - # Detect tree output. - - if arg == "--tree": - tree = True - - # Detect macro evaluation. - - elif arg == "--macros": - macros = True - - # Detect all documents. - - elif arg == "--all": - all = True - - # Detect fragment output (if serialising). - - elif arg == "--fragment": - fragment = True - - # Switch to collecting formats. - - elif arg == "--format": - l = formats - continue - - # Switch to collecting input locations. - - elif arg == "--input-dir": - l = input_dirs - continue - - # Switch to collecting input context types. - - elif arg == "--input-dir-type": - l = input_dir_types - continue - - # Switch to collecting input encodings. - - elif arg == "--input-encoding": - l = input_encodings - continue - - # Switch to collecting input page hierarchy separators. - - elif arg == "--input-page-sep": - l = input_page_seps - continue - - # Switch to collecting mappings. - - elif arg == "--mapping": - l = mappings - continue - - # Switch to collecting output locations. - - elif arg == "--output-dir": - l = output_dirs - continue - - # Switch to collecting output encodings. - - elif arg == "--output-encoding": - l = output_encodings - continue - - # Switch to collecting page names. - - elif arg == "--pagename": - l = pagenames - continue - - # Switch to collecting root page names. - - elif arg == "--root": - l = root_pagenames - continue - - # Switch to collecting theme names. - - elif arg == "--theme": - l = theme_names - continue - - # Collect options and arguments. - - else: - l.append(arg) - - # Collect multiple mappings. - - if l is mappings: - continue - - # Collect filenames normally. - - l = filenames - - format = formats and formats[0] or "html" - input_dir = getvalue(input_dirs) - output_dir = getvalue(output_dirs) - - # Define metadata. - - metadata = Metadata({ - "input_context" : input_dir and \ - getvalue(input_dir_types, "directory") or \ - "standalone", - "input_encoding" : getvalue(input_encodings), - "input_filename" : input_dir, - "input_separator" : getvalue(input_page_seps), - "link_format" : format, - "mapping" : getmapping(mappings), - "output_context" : output_dir and "directory" or "standalone", - "output_encoding" : getvalue(output_encodings), - "output_format" : format, - "output_filename" : output_dir, - "root_pagename" : getvalue(root_pagenames, "FrontPage"), - "theme_name" : not fragment and \ - "%s.%s" % (getvalue(theme_names, "default"), format) or None, - }) - - # Define the input context and theme. - - input = metadata.get_input() - theme = metadata.get_theme() - - # Treat filenames as pagenames if an input directory is indicated and if no - # pagenames are explicitly specified. - - if input_dir: - if pagenames: - print >>sys.stderr, message_explicit_pagenames - sys.exit(1) - - if all: - if filenames: - print >>sys.stderr, message_all_with_filenames - sys.exit(1) - else: - filenames = input.all() - - pagenames = filenames - filenames = [] - - # Open each file or page, parse the content, serialise the document. - - for pagename, filename in map(None, pagenames, filenames): - - # Define a pagename if missing. - - pagename = pagename or split(filename)[-1] - metadata.set("pagename", pagename) - - # Read either from a filename or using a pagename. - - if filename: - pagetext = input.readfile(filename) - else: - pagetext = input.readpage(pagename) - - # Parse the page content. - - p = make_parser(metadata) - d = parse(pagetext, p) - - if macros: - p.evaluate_macros() - - # Show a document tree for debugging purposes, if requested. - - if tree: - print d.prettyprint() - continue - - # Otherwise, serialise the document. - - # Obtain a serialiser using the configuration. - - serialiser = make_serialiser(metadata) - outtext = serialise(d, serialiser) - - # With a theme, apply it to the text. - - if theme: - outtext = theme.apply(outtext) - - # If reading from a file, show the result. Otherwise, write to the - # output context. - - output = metadata.get_output() - - if not output.can_write(): - print outtext - else: - output.writepage(outtext, pagename) - print >>sys.stderr, pagename - - # Install any theme resources. - - if theme: - theme.install_resources() - -if __name__ == "__main__": - main() - -# vim: tabstop=4 expandtab shiftwidth=4 diff -r 104491ce1ed4 -r 6425c9e2744f moinconvert --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/moinconvert Mon Nov 26 16:05:50 2018 +0100 @@ -0,0 +1,313 @@ +#!/usr/bin/env python + +""" +Moin wiki format converter. + +Copyright (C) 2018 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +from moinformat import make_parser, make_serialiser, Metadata, parse, serialise +from os.path import split +import sys + +# Long messages. + +message_all_with_filenames = """\ +Using --all overrides any indicated pagenames. Either --all or the filenames +should be omitted.""" + +message_explicit_pagenames = """\ +Explicit pagenames (indicated using --pagename) are only to be specified when +providing filenames without an input directory (indicated using --input-dir). + +To indicate pagenames within an input directory, omit any --pagename flags.""" + + + +# Options management. + +def getmapping(mappings): + + """ + Return the given 'mappings' - a collection of key-then-value items - as a + dictionary. + """ + + mapping = {} + key = None + + for arg in mappings: + if key is None: + key = arg + else: + mapping[key] = arg + key = None + + return mapping + +def getvalue(values, default=None): + + """ + Return the first value from 'values' or 'default' if 'values' is empty or + the first value tests as false. + """ + + return values and values[0] or default + + + +# Main program. + +def main(): + + "Interpret program options and perform the conversion." + + dirname, progname = split(sys.argv[0]) + args = sys.argv[1:] + + # Option values. + + l = filenames = [] + formats = [] + input_dir_types = [] + input_dirs = [] + input_encodings = [] + input_page_seps = [] + mappings = [] + output_dirs = [] + output_encodings = [] + theme_names = [] + pagenames = [] + root_pagenames = [] + + # Flags. + + all = False + fragment = False + macros = False + tree = False + + for arg in args: + + # Detect tree output. + + if arg == "--tree": + tree = True + + # Detect macro evaluation. + + elif arg == "--macros": + macros = True + + # Detect all documents. + + elif arg == "--all": + all = True + + # Detect fragment output (if serialising). + + elif arg == "--fragment": + fragment = True + + # Switch to collecting formats. + + elif arg == "--format": + l = formats + continue + + # Switch to collecting input locations. + + elif arg == "--input-dir": + l = input_dirs + continue + + # Switch to collecting input context types. + + elif arg == "--input-dir-type": + l = input_dir_types + continue + + # Switch to collecting input encodings. + + elif arg == "--input-encoding": + l = input_encodings + continue + + # Switch to collecting input page hierarchy separators. + + elif arg == "--input-page-sep": + l = input_page_seps + continue + + # Switch to collecting mappings. + + elif arg == "--mapping": + l = mappings + continue + + # Switch to collecting output locations. + + elif arg == "--output-dir": + l = output_dirs + continue + + # Switch to collecting output encodings. + + elif arg == "--output-encoding": + l = output_encodings + continue + + # Switch to collecting page names. + + elif arg == "--pagename": + l = pagenames + continue + + # Switch to collecting root page names. + + elif arg == "--root": + l = root_pagenames + continue + + # Switch to collecting theme names. + + elif arg == "--theme": + l = theme_names + continue + + # Collect options and arguments. + + else: + l.append(arg) + + # Collect multiple mappings. + + if l is mappings: + continue + + # Collect filenames normally. + + l = filenames + + format = formats and formats[0] or "html" + input_dir = getvalue(input_dirs) + output_dir = getvalue(output_dirs) + + # Define metadata. + + metadata = Metadata({ + "input_context" : input_dir and \ + getvalue(input_dir_types, "directory") or \ + "standalone", + "input_encoding" : getvalue(input_encodings), + "input_filename" : input_dir, + "input_separator" : getvalue(input_page_seps), + "link_format" : format, + "mapping" : getmapping(mappings), + "output_context" : output_dir and "directory" or "standalone", + "output_encoding" : getvalue(output_encodings), + "output_format" : format, + "output_filename" : output_dir, + "root_pagename" : getvalue(root_pagenames, "FrontPage"), + "theme_name" : not fragment and \ + "%s.%s" % (getvalue(theme_names, "default"), format) or None, + }) + + # Define the input context and theme. + + input = metadata.get_input() + theme = metadata.get_theme() + + # Treat filenames as pagenames if an input directory is indicated and if no + # pagenames are explicitly specified. + + if input_dir: + if pagenames: + print >>sys.stderr, message_explicit_pagenames + sys.exit(1) + + if all: + if filenames: + print >>sys.stderr, message_all_with_filenames + sys.exit(1) + else: + filenames = input.all() + + pagenames = filenames + filenames = [] + + # Open each file or page, parse the content, serialise the document. + + for pagename, filename in map(None, pagenames, filenames): + + # Define a pagename if missing. + + pagename = pagename or split(filename)[-1] + metadata.set("pagename", pagename) + + # Read either from a filename or using a pagename. + + if filename: + pagetext = input.readfile(filename) + else: + pagetext = input.readpage(pagename) + + # Parse the page content. + + p = make_parser(metadata) + d = parse(pagetext, p) + + if macros: + p.evaluate_macros() + + # Show a document tree for debugging purposes, if requested. + + if tree: + print d.prettyprint() + continue + + # Otherwise, serialise the document. + + # Obtain a serialiser using the configuration. + + serialiser = make_serialiser(metadata) + outtext = serialise(d, serialiser) + + # With a theme, apply it to the text. + + if theme: + outtext = theme.apply(outtext) + + # If reading from a file, show the result. Otherwise, write to the + # output context. + + output = metadata.get_output() + + if not output.can_write(): + print outtext + else: + output.writepage(outtext, pagename) + print >>sys.stderr, pagename + + # Install any theme resources. + + if theme: + theme.install_resources() + +if __name__ == "__main__": + main() + +# vim: tabstop=4 expandtab shiftwidth=4