[GTK] Generate API documentation for GObject DOM bindings
[WebKit-https.git] / Tools / gtk / generate-webkitdom-doc-files
1 #!/usr/bin/env python
2 # Copyright (C) 2013 Igalia S.L.
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 # 02110-1301  USA
18
19 import os
20 import re
21 import sys
22
23 class WebKitDOMDocGenerator:
24
25     def write_header(self):
26         pass
27
28     def write_section(self, symbol_file):
29         raise NotImplementedError
30
31     def write_footer(self):
32         pass
33
34 class WebKitDOMDocGeneratorDocs(WebKitDOMDocGenerator):
35
36     def write_header(self):
37         sys.stdout.write('''<?xml version="1.0"?>
38 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
39                "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [
40 <!ENTITY version SYSTEM "version.xml">
41 ]>
42 <book id="index" xmlns:xi="http://www.w3.org/2003/XInclude">
43   <bookinfo>
44     <title>WebKitDOMGTK+ Reference Manual</title>
45     <releaseinfo>for WebKitDOMGTK+ &version;</releaseinfo>
46   </bookinfo>
47
48   <chapter>
49     <title>Class Overview</title>
50 ''')
51
52     def write_section(self, symbol_file):
53         basename = os.path.basename(symbol_file)
54         sys.stdout.write('    <xi:include href="xml/%s"/>\n' % basename.replace(".symbols", ".xml"))
55
56     def write_footer(self):
57         sys.stdout.write('''  </chapter>
58
59   <index id="index-all">
60     <title>Index</title>
61   </index>
62
63   <xi:include href="xml/annotation-glossary.xml"><xi:fallback /></xi:include>
64 </book>
65 ''')
66
67 class WebKitDOMDocGeneratorSections(WebKitDOMDocGenerator):
68
69     def __init__(self):
70         self._first_decamelize_re = re.compile('(.)([A-Z][a-z]+)')
71         self._second_decamelize_re = re.compile('([a-z0-9])([A-Z])')
72         self._dom_class_re = re.compile('(^WebKitDOM)(.+)$')
73         self._function_re = re.compile('^.+ (.+)\(.+$')
74
75     def _dom_class(self, class_name):
76         return self._dom_class_re.sub(r'\2', class_name)
77
78     def _dom_class_decamelize(self, class_name):
79         s1 = self._first_decamelize_re.sub(r'\1_\2', self._dom_class(class_name))
80         retval = self._second_decamelize_re.sub(r'\1_\2', s1)
81
82         # Fix some exceptions.
83         retval = retval.replace('Web_Kit', 'WebKit')
84         retval = retval.replace('X_Path', 'XPath')
85         retval = retval.replace('HTMLI_Frame', 'HTML_IFrame')
86
87         return retval
88
89     def _symbol_list(self, symbol_file):
90         retval = []
91         f = open(symbol_file, 'r')
92         for line in f.readlines():
93             match = self._function_re.match(line)
94             if not match or match.group(1).endswith('get_type'):
95                 continue
96             retval.append(match.group(1))
97
98         return retval
99
100     def write_section(self, symbol_file):
101         class_name = os.path.basename(symbol_file).replace(".symbols", "")
102         is_custom = class_name == 'WebKitDOMCustom'
103         is_interface = class_name == 'WebKitDOMEventTarget'
104         is_object = class_name == 'WebKitDOMObject'
105         sys.stdout.write('<SECTION>\n')
106         sys.stdout.write('<FILE>%s</FILE>\n<TITLE>%s</TITLE>\n' % (class_name, class_name))
107         if not is_custom:
108             sys.stdout.write('%s\n' % class_name)
109         sys.stdout.write('\n')
110         sys.stdout.write('\n'.join(self._symbol_list(symbol_file)) + '\n')
111         if not is_custom:
112             sys.stdout.write('\n<SUBSECTION Standard>\n')
113             sys.stdout.write('%sClass\n' % class_name)
114             dom_class = self._dom_class_decamelize(class_name).upper()
115             sys.stdout.write('WEBKIT_TYPE_DOM_%s\n' % dom_class)
116             sys.stdout.write('WEBKIT_DOM_%s\n' % dom_class)
117             if is_object:
118                 sys.stdout.write('WEBKIT_IS_DOM_%s\n' % dom_class)
119             else:
120                 sys.stdout.write('WEBKIT_DOM_IS_%s\n' % dom_class)
121             sys.stdout.write('WEBKIT_DOM_%s_CLASS\n' % dom_class)
122             if is_interface:
123                 sys.stdout.write('WEBKIT_DOM_%s_GET_IFACE\n' % dom_class)
124             else:
125                 if is_object:
126                     sys.stdout.write('WEBKIT_IS_DOM_%s_CLASS\n' % dom_class)
127                 else:
128                     sys.stdout.write('WEBKIT_DOM_IS_%s_CLASS\n' % dom_class)
129                 sys.stdout.write('WEBKIT_DOM_%s_GET_CLASS\n' % dom_class)
130             sys.stdout.write('\n<SUBSECTION Private>\n')
131             if is_object:
132                 sys.stdout.write('%sPrivate\n' % class_name)
133             sys.stdout.write('webkit_dom_%s_get_type\n' % dom_class.lower())
134         sys.stdout.write('</SECTION>\n\n')
135
136
137 symbol_files = sys.stdin.read().strip('\n').split(' ')
138 if 'docs' in sys.argv[1:]:
139     generator = WebKitDOMDocGeneratorDocs()
140 elif 'sections' in sys.argv[1:]:
141     generator = WebKitDOMDocGeneratorSections()
142
143 generator.write_header()
144 for symbol_file in symbol_files:
145     generator.write_section(symbol_file)
146 generator.write_footer()