1 #!/usr/bin/env python 2 3 from moinformat import make_linker, make_output, make_parser, make_serialiser, \ 4 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 l = filenames = [] 13 formats = [] 14 pagenames = [] 15 mappings = [] 16 outputs = [] 17 tree = False 18 macros = False 19 20 for arg in args: 21 22 # Detect tree output. 23 24 if arg == "--tree": 25 tree = True 26 27 # Detect macro evaluation. 28 29 elif arg == "--macros": 30 macros = True 31 32 # Switch to collecting formats. 33 34 elif arg == "--format": 35 l = formats 36 continue 37 38 # Switch to collecting mappings. 39 40 elif arg == "--mapping": 41 l = mappings 42 continue 43 44 # Switch to collecting output locations. 45 46 elif arg == "--output": 47 l = outputs 48 continue 49 50 # Switch to collecting page names. 51 52 elif arg == "--pagename": 53 l = pagenames 54 continue 55 56 # Collect options and arguments. 57 58 else: 59 l.append(arg) 60 61 # Collect multiple mappings. 62 63 if l is mappings: 64 continue 65 66 # Collect filenames normally. 67 68 l = filenames 69 70 format = formats and formats[0] or "html" 71 72 # Derive the page name from the filename if not specified. 73 74 filename = filenames[0] 75 pagename = pagenames and pagenames[0] or split(filename)[-1] 76 77 # Obtain an output context from any specified output details. 78 79 output = outputs and make_output(outputs[0]) or None 80 81 # Derive a proper mapping from the given list of values. 82 83 mapping = {} 84 key = None 85 86 for arg in mappings: 87 if key is None: 88 key = arg 89 else: 90 mapping[key] = arg 91 key = None 92 93 # Open the file, parse the content, serialise the document. 94 95 f = open(filename) 96 try: 97 p = make_parser() 98 d = parse(f.read(), p) 99 100 if macros: 101 p.evaluate_macros() 102 103 if tree: 104 print d.prettyprint() 105 else: 106 l = make_linker(format, pagename, mapping) 107 s = make_serialiser(format, output, l) 108 print serialise(d, s) 109 finally: 110 f.close() 111 112 if __name__ == "__main__": 113 main() 114 115 # vim: tabstop=4 expandtab shiftwidth=4