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 tree = False 15 macros = False 16 17 for arg in args: 18 19 # Detect tree output. 20 21 if arg == "--tree": 22 tree = True 23 24 # Detect macro evaluation. 25 26 elif arg == "--macros": 27 macros = True 28 29 # Switch to collecting formats. 30 31 elif arg == "--format": 32 l = formats 33 continue 34 35 # Switch to collecting page names. 36 37 elif arg == "--pagename": 38 l = pagenames 39 continue 40 41 # Collect options and arguments. 42 43 else: 44 l.append(arg) 45 46 # Collect filenames normally. 47 48 l = filenames 49 50 format = formats and formats[0] or "html" 51 52 # Derive the page name from the filename if not specified. 53 54 filename = filenames[0] 55 pagename = pagenames and pagenames[0] or split(filename)[-1] 56 57 f = open(filename) 58 try: 59 p = make_parser() 60 d = parse(f.read(), p) 61 62 if macros: 63 p.evaluate_macros() 64 65 if tree: 66 print d.prettyprint() 67 else: 68 l = make_linker(format, pagename) 69 s = make_serialiser(format, l) 70 print serialise(d, s) 71 finally: 72 f.close() 73 74 if __name__ == "__main__": 75 main() 76 77 # vim: tabstop=4 expandtab shiftwidth=4