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