# HG changeset patch # User Paul Boddie # Date 1533485818 -7200 # Node ID 22f6b761f4a16ce2da5166f1d5bdb286d09c4ed0 # Parent e8f4edb1d59935113cc623bd33a9778c7b095dee Changed the conversion script to operate on input and output directories if these are specified. Simplified various option processing operations. diff -r e8f4edb1d599 -r 22f6b761f4a1 convert.py --- a/convert.py Sun Aug 05 18:15:54 2018 +0200 +++ b/convert.py Sun Aug 05 18:16:58 2018 +0200 @@ -5,6 +5,22 @@ from os.path import split import sys +def getmapping(mappings): + mapping = {} + key = None + + for arg in mappings: + if key is None: + key = arg + else: + mapping[key] = arg + key = None + + return mapping + +def getvalue(values): + return values and values[0] or None + def main(): dirname, progname = split(sys.argv[0]) args = sys.argv[1:] @@ -13,7 +29,10 @@ l = filenames = [] formats = [] + input_dir_types = [] + input_dirs = [] input_encodings = [] + input_page_seps = [] mappings = [] output_dirs = [] output_encodings = [] @@ -21,6 +40,7 @@ # Flags. + all = False macros = False tree = False @@ -36,18 +56,41 @@ elif arg == "--macros": macros = True + # Detect all documents. + + elif arg == "--all": + all = 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": @@ -88,56 +131,88 @@ format = formats and formats[0] or "html" - # Derive the page name from the filename if not specified. - - filename = filenames[0] - pagename = pagenames and pagenames[0] or split(filename)[-1] - # Derive a proper mapping from the given list of values. - mapping = {} - key = None - - for arg in mappings: - if key is None: - key = arg - else: - mapping[key] = arg - key = None - - # Obtain output location. - - output_dir = output_dirs and output_dirs[0] or None + mapping = getmapping(mappings) # Obtain encodings. - input_encoding = input_encodings and input_encodings[0] or None - output_encoding = output_encodings and output_encodings[0] or None + input_encoding = getvalue(input_encodings) + output_encoding = getvalue(output_encodings) + + # Obtain the input and output locations. + + input_dir = getvalue(input_dirs) + output_dir = getvalue(output_dirs) - # Open the file, parse the content, serialise the document. + input_page_sep = getvalue(input_page_seps) + + input_context = input_dir and (getvalue(input_dir_types) or + "directory") or "standalone" + + input = make_input(input_context, {"encoding" : input_encoding, + "filename" : input_dir, + "separator" : input_page_sep}) - input = make_input("standalone", {"encoding" : input_encoding}) + output_context = output_dir and "directory" or "standalone" + + output = make_output(output_context, {"encoding" : output_encoding, + "filename" : output_dir}) + + # Treat filenames as pagenames if an input directory is indicated and if no + # pagenames are explicitly specified. - p = make_parser() - d = parse(input.readfile(filename), p) + if input_dir: + if pagenames: + print >>sys.stderr, """\ +Explicit pagenames (indicated using --pagename) are only to be specified when +providing filenames without an input directory (indicated using --input-dir). - if macros: - p.evaluate_macros() +To indicate pagenames within an input directory, omit any --pagename flags.""" + sys.exit(1) - # Show a document tree for debugging purposes, if requested. + if all: + if filenames: + print >>sys.stderr, """\ +Using --all overrides any indicated pagenames. Either --all or the filenames +should be omitted.""" + sys.exit(1) + else: + filenames = input.all() - if tree: - print d.prettyprint() + pagenames = filenames + filenames = [] + + # Open each file or page, parse the content, serialise the document. - # Otherwise, serialise the document. + for pagename, filename in map(None, pagenames, filenames): + + # Define a pagename if missing. + + pagename = pagename or split(filename)[-1] - else: - # Obtain an output context from any specified output details. + # Read either from a filename or using a pagename. + + if filename: + pagetext = input.readfile(filename) + else: + pagetext = input.readpage(pagename) + + # Parse the page content. - output_context = output_dir and "directory" or "standalone" + p = make_parser() + d = parse(pagetext, p) + + if macros: + p.evaluate_macros() - output = make_output(output_context, {"encoding" : output_encoding, - "filename" : output_dir}) + # Show a document tree for debugging purposes, if requested. + + if tree: + print d.prettyprint() + continue + + # Otherwise, serialise the document. # Obtain a linker using format and pagename details. @@ -146,7 +221,16 @@ # Obtain a serialiser using the configuration. serialiser = make_serialiser(format, output, linker) - print serialise(d, serialiser) + outtext = serialise(d, serialiser) + + # If reading from a file, show the result. Otherwise, write to the + # output context. + + if not output.can_write(): + print outtext + else: + output.writepage(outtext, pagename) + print >>sys.stderr, pagename if __name__ == "__main__": main()