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