1.1 --- a/convert.py Sun Aug 05 18:15:54 2018 +0200
1.2 +++ b/convert.py Sun Aug 05 18:16:58 2018 +0200
1.3 @@ -5,6 +5,22 @@
1.4 from os.path import split
1.5 import sys
1.6
1.7 +def getmapping(mappings):
1.8 + mapping = {}
1.9 + key = None
1.10 +
1.11 + for arg in mappings:
1.12 + if key is None:
1.13 + key = arg
1.14 + else:
1.15 + mapping[key] = arg
1.16 + key = None
1.17 +
1.18 + return mapping
1.19 +
1.20 +def getvalue(values):
1.21 + return values and values[0] or None
1.22 +
1.23 def main():
1.24 dirname, progname = split(sys.argv[0])
1.25 args = sys.argv[1:]
1.26 @@ -13,7 +29,10 @@
1.27
1.28 l = filenames = []
1.29 formats = []
1.30 + input_dir_types = []
1.31 + input_dirs = []
1.32 input_encodings = []
1.33 + input_page_seps = []
1.34 mappings = []
1.35 output_dirs = []
1.36 output_encodings = []
1.37 @@ -21,6 +40,7 @@
1.38
1.39 # Flags.
1.40
1.41 + all = False
1.42 macros = False
1.43 tree = False
1.44
1.45 @@ -36,18 +56,41 @@
1.46 elif arg == "--macros":
1.47 macros = True
1.48
1.49 + # Detect all documents.
1.50 +
1.51 + elif arg == "--all":
1.52 + all = True
1.53 +
1.54 # Switch to collecting formats.
1.55
1.56 elif arg == "--format":
1.57 l = formats
1.58 continue
1.59
1.60 + # Switch to collecting input locations.
1.61 +
1.62 + elif arg == "--input-dir":
1.63 + l = input_dirs
1.64 + continue
1.65 +
1.66 + # Switch to collecting input context types.
1.67 +
1.68 + elif arg == "--input-dir-type":
1.69 + l = input_dir_types
1.70 + continue
1.71 +
1.72 # Switch to collecting input encodings.
1.73
1.74 elif arg == "--input-encoding":
1.75 l = input_encodings
1.76 continue
1.77
1.78 + # Switch to collecting input page hierarchy separators.
1.79 +
1.80 + elif arg == "--input-page-sep":
1.81 + l = input_page_seps
1.82 + continue
1.83 +
1.84 # Switch to collecting mappings.
1.85
1.86 elif arg == "--mapping":
1.87 @@ -88,56 +131,88 @@
1.88
1.89 format = formats and formats[0] or "html"
1.90
1.91 - # Derive the page name from the filename if not specified.
1.92 -
1.93 - filename = filenames[0]
1.94 - pagename = pagenames and pagenames[0] or split(filename)[-1]
1.95 -
1.96 # Derive a proper mapping from the given list of values.
1.97
1.98 - mapping = {}
1.99 - key = None
1.100 -
1.101 - for arg in mappings:
1.102 - if key is None:
1.103 - key = arg
1.104 - else:
1.105 - mapping[key] = arg
1.106 - key = None
1.107 -
1.108 - # Obtain output location.
1.109 -
1.110 - output_dir = output_dirs and output_dirs[0] or None
1.111 + mapping = getmapping(mappings)
1.112
1.113 # Obtain encodings.
1.114
1.115 - input_encoding = input_encodings and input_encodings[0] or None
1.116 - output_encoding = output_encodings and output_encodings[0] or None
1.117 + input_encoding = getvalue(input_encodings)
1.118 + output_encoding = getvalue(output_encodings)
1.119 +
1.120 + # Obtain the input and output locations.
1.121 +
1.122 + input_dir = getvalue(input_dirs)
1.123 + output_dir = getvalue(output_dirs)
1.124
1.125 - # Open the file, parse the content, serialise the document.
1.126 + input_page_sep = getvalue(input_page_seps)
1.127 +
1.128 + input_context = input_dir and (getvalue(input_dir_types) or
1.129 + "directory") or "standalone"
1.130 +
1.131 + input = make_input(input_context, {"encoding" : input_encoding,
1.132 + "filename" : input_dir,
1.133 + "separator" : input_page_sep})
1.134
1.135 - input = make_input("standalone", {"encoding" : input_encoding})
1.136 + output_context = output_dir and "directory" or "standalone"
1.137 +
1.138 + output = make_output(output_context, {"encoding" : output_encoding,
1.139 + "filename" : output_dir})
1.140 +
1.141 + # Treat filenames as pagenames if an input directory is indicated and if no
1.142 + # pagenames are explicitly specified.
1.143
1.144 - p = make_parser()
1.145 - d = parse(input.readfile(filename), p)
1.146 + if input_dir:
1.147 + if pagenames:
1.148 + print >>sys.stderr, """\
1.149 +Explicit pagenames (indicated using --pagename) are only to be specified when
1.150 +providing filenames without an input directory (indicated using --input-dir).
1.151
1.152 - if macros:
1.153 - p.evaluate_macros()
1.154 +To indicate pagenames within an input directory, omit any --pagename flags."""
1.155 + sys.exit(1)
1.156
1.157 - # Show a document tree for debugging purposes, if requested.
1.158 + if all:
1.159 + if filenames:
1.160 + print >>sys.stderr, """\
1.161 +Using --all overrides any indicated pagenames. Either --all or the filenames
1.162 +should be omitted."""
1.163 + sys.exit(1)
1.164 + else:
1.165 + filenames = input.all()
1.166
1.167 - if tree:
1.168 - print d.prettyprint()
1.169 + pagenames = filenames
1.170 + filenames = []
1.171 +
1.172 + # Open each file or page, parse the content, serialise the document.
1.173
1.174 - # Otherwise, serialise the document.
1.175 + for pagename, filename in map(None, pagenames, filenames):
1.176 +
1.177 + # Define a pagename if missing.
1.178 +
1.179 + pagename = pagename or split(filename)[-1]
1.180
1.181 - else:
1.182 - # Obtain an output context from any specified output details.
1.183 + # Read either from a filename or using a pagename.
1.184 +
1.185 + if filename:
1.186 + pagetext = input.readfile(filename)
1.187 + else:
1.188 + pagetext = input.readpage(pagename)
1.189 +
1.190 + # Parse the page content.
1.191
1.192 - output_context = output_dir and "directory" or "standalone"
1.193 + p = make_parser()
1.194 + d = parse(pagetext, p)
1.195 +
1.196 + if macros:
1.197 + p.evaluate_macros()
1.198
1.199 - output = make_output(output_context, {"encoding" : output_encoding,
1.200 - "filename" : output_dir})
1.201 + # Show a document tree for debugging purposes, if requested.
1.202 +
1.203 + if tree:
1.204 + print d.prettyprint()
1.205 + continue
1.206 +
1.207 + # Otherwise, serialise the document.
1.208
1.209 # Obtain a linker using format and pagename details.
1.210
1.211 @@ -146,7 +221,16 @@
1.212 # Obtain a serialiser using the configuration.
1.213
1.214 serialiser = make_serialiser(format, output, linker)
1.215 - print serialise(d, serialiser)
1.216 + outtext = serialise(d, serialiser)
1.217 +
1.218 + # If reading from a file, show the result. Otherwise, write to the
1.219 + # output context.
1.220 +
1.221 + if not output.can_write():
1.222 + print outtext
1.223 + else:
1.224 + output.writepage(outtext, pagename)
1.225 + print >>sys.stderr, pagename
1.226
1.227 if __name__ == "__main__":
1.228 main()