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