1 #!/usr/bin/env python 2 3 from moinformat import make_input, make_linker, make_output, make_parser, \ 4 make_serialiser, parse, serialise 5 from os.path import split 6 import sys 7 8 def main(): 9 dirname, progname = split(sys.argv[0]) 10 args = sys.argv[1:] 11 12 # Option values. 13 14 l = filenames = [] 15 formats = [] 16 input_encodings = [] 17 mappings = [] 18 output_dirs = [] 19 output_encodings = [] 20 pagenames = [] 21 22 # Flags. 23 24 macros = False 25 tree = False 26 27 for arg in args: 28 29 # Detect tree output. 30 31 if arg == "--tree": 32 tree = True 33 34 # Detect macro evaluation. 35 36 elif arg == "--macros": 37 macros = True 38 39 # Switch to collecting formats. 40 41 elif arg == "--format": 42 l = formats 43 continue 44 45 # Switch to collecting input encodings. 46 47 elif arg == "--input-encoding": 48 l = input_encodings 49 continue 50 51 # Switch to collecting mappings. 52 53 elif arg == "--mapping": 54 l = mappings 55 continue 56 57 # Switch to collecting output locations. 58 59 elif arg == "--output-dir": 60 l = output_dirs 61 continue 62 63 # Switch to collecting output encodings. 64 65 elif arg == "--output-encoding": 66 l = output_encodings 67 continue 68 69 # Switch to collecting page names. 70 71 elif arg == "--pagename": 72 l = pagenames 73 continue 74 75 # Collect options and arguments. 76 77 else: 78 l.append(arg) 79 80 # Collect multiple mappings. 81 82 if l is mappings: 83 continue 84 85 # Collect filenames normally. 86 87 l = filenames 88 89 format = formats and formats[0] or "html" 90 91 # Derive the page name from the filename if not specified. 92 93 filename = filenames[0] 94 pagename = pagenames and pagenames[0] or split(filename)[-1] 95 96 # Derive a proper mapping from the given list of values. 97 98 mapping = {} 99 key = None 100 101 for arg in mappings: 102 if key is None: 103 key = arg 104 else: 105 mapping[key] = arg 106 key = None 107 108 # Obtain output location. 109 110 output_dir = output_dirs and output_dirs[0] or None 111 112 # Obtain encodings. 113 114 input_encoding = input_encodings and input_encodings[0] or None 115 output_encoding = output_encodings and output_encodings[0] or None 116 117 # Open the file, parse the content, serialise the document. 118 119 input = make_input("standalone", {"encoding" : input_encoding}) 120 121 p = make_parser() 122 d = parse(input.readfile(filename), p) 123 124 if macros: 125 p.evaluate_macros() 126 127 # Show a document tree for debugging purposes, if requested. 128 129 if tree: 130 print d.prettyprint() 131 132 # Otherwise, serialise the document. 133 134 else: 135 # Obtain an output context from any specified output details. 136 137 output_context = output_dir and "directory" or "standalone" 138 139 output = make_output(output_context, {"encoding" : output_encoding, 140 "filename" : output_dir}) 141 142 # Obtain a linker using format and pagename details. 143 144 linker = make_linker(format, pagename, mapping) 145 146 # Obtain a serialiser using the configuration. 147 148 serialiser = make_serialiser(format, output, linker) 149 print serialise(d, serialiser) 150 151 if __name__ == "__main__": 152 main() 153 154 # vim: tabstop=4 expandtab shiftwidth=4