giscanner/mallardwriter: Adding experimental Mallard output to g-ir-doc-tool
[platform/upstream/gobject-introspection.git] / giscanner / docmain.py
1 # -*- Mode: Python -*-
2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008-2011 Johan Dahlin
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # 02110-1301, USA.
19 #
20
21 import optparse
22
23 from .docbookwriter import DocBookWriter
24 from .docbookwriter import DocBookFormatterC
25 from .docbookwriter import DocBookFormatterPython
26 from .mallardwriter import MallardWriter
27 from .mallardwriter import MallardFormatterC
28 from .mallardwriter import MallardFormatterPython
29 from .transformer import Transformer
30
31 class GIDocGenerator(object):
32
33     def parse(self, filename):
34         self.transformer = Transformer.parse_from_gir(filename)
35
36     def generate(self, writer, output):
37         writer.add_transformer(self.transformer)
38         writer.write(output)
39
40 def doc_main(args):
41     parser = optparse.OptionParser('%prog [options] GIR-file')
42
43     parser.add_option("-o", "--output",
44                       action="store", dest="output",
45                       help="Filename to write output")
46     parser.add_option("-f", "--format",
47                       action="store", dest="format",
48                       default="docbook",
49                       help="Output format")
50     parser.add_option("-l", "--language",
51                       action="store", dest="language",
52                       default="Python",
53                       help="Output language")
54
55     options, args = parser.parse_args(args)
56     if not options.output:
57         raise SystemExit("missing output parameter")
58
59     if len(args) < 2:
60         raise SystemExit("Need an input GIR filename")
61
62     if options.format == "docbook":
63         if options.language == "Python":
64             formatter = DocBookFormatterPython()
65         elif options.language == "C":
66             formatter = DocBookFormatterC()
67         else:
68             raise SystemExit("Unsupported language: %s" % (options.language, ))
69         writer = DocBookWriter(formatter)
70     elif options.format == "mallard":
71         if options.language == "Python":
72             formatter = MallardFormatterPython()
73         elif options.language == "C":
74             formatter = MallardFormatterC()
75         else:
76             raise SystemExit("Unsupported language: %s" % (options.language, ))
77         writer = MallardWriter(formatter)
78     else:
79         raise SystemExit("Unsupported output format: %s" % (options.format, ))
80
81     generator = GIDocGenerator()
82     generator.parse(args[1])
83
84     generator.generate(writer, options.output)
85
86     return 0