2 # nghttp2 - HTTP/2 C Library
4 # Copyright (c) 2012 Tatsuhiro Tsujikawa
6 # Permission is hereby granted, free of charge, to any person obtaining
7 # a copy of this software and associated documentation files (the
8 # "Software"), to deal in the Software without restriction, including
9 # without limitation the rights to use, copy, modify, merge, publish,
10 # distribute, sublicense, and/or sell copies of the Software, and to
11 # permit persons to whom the Software is furnished to do so, subject to
12 # the following conditions:
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 # Generates API reference from C source code.
26 from __future__ import print_function # At least python 2.6 is required
27 import re, sys, argparse, os.path
30 def __init__(self, name, content, domain):
32 self.content = content
34 if self.domain == 'function':
35 self.funcname = re.search(r'(nghttp2_[^ )]+)\(', self.name).group(1)
38 out.write('.. {}:: {}\n'.format(self.domain, self.name))
40 for line in self.content:
41 out.write(' {}\n'.format(line))
44 def __init__(self, name, content, members, member_domain):
46 self.content = content
47 self.members = members
48 self.member_domain = member_domain
52 out.write('.. type:: {}\n'.format(self.name))
54 for line in self.content:
55 out.write(' {}\n'.format(line))
57 for name, content in self.members:
58 out.write(' .. {}:: {}\n'.format(self.member_domain, name))
61 out.write(' {}\n'.format(line))
65 def __init__(self, name, content):
67 self.content = content
70 out.write('''.. macro:: {}\n'''.format(self.name))
72 for line in self.content:
73 out.write(' {}\n'.format(line))
75 def make_api_ref(infiles):
80 for infile in infiles:
82 line = infile.readline()
86 line = infile.readline()
87 doctype = line.split()[1]
88 if doctype == '@function':
89 functions.append(process_function('function', infile))
90 elif doctype == '@functypedef':
91 types.append(process_function('type', infile))
92 elif doctype == '@struct' or doctype == '@union':
93 types.append(process_struct(infile))
94 elif doctype == '@enum':
95 enums.append(process_enum(infile))
96 elif doctype == '@macro':
97 macros.append(process_macro(infile))
98 return macros, enums, types, functions
100 alldocs = [('Macros', macros),
102 ('Types (structs, unions and typedefs)', types),
103 ('Functions', functions)]
106 indexfile, macrosfile, enumsfile, typesfile, funcsdir,
107 macros, enums, types, functions):
120 for doc in functions:
121 indexfile.write(' {}\n'.format(doc.funcname))
128 doc.write(macrosfile)
138 Types (structs, unions and typedefs)
139 ====================================
144 for doc in functions:
145 with open(os.path.join(funcsdir, doc.funcname + '.rst'), 'w') as f:
153 *#include <nghttp2/nghttp2.h>*
155 '''.format(funcname=doc.funcname, secul='='*len(doc.funcname)))
158 def process_macro(infile):
159 content = read_content(infile)
160 line = infile.readline()
161 macro_name = line.split()[1]
162 return MacroDoc(macro_name, content)
164 def process_enum(infile):
167 content = read_content(infile)
169 line = infile.readline()
172 elif re.match(r'\s*/\*\*\n', line):
173 member_content = read_content(infile)
174 line = infile.readline()
176 member_name = items[0]
178 member_content.insert(0, '(``{}``) '\
179 .format(' '.join(items[2:]).rstrip(',')))
180 members.append((member_name, member_content))
181 elif line.startswith('}'):
182 enum_name = line.rstrip().split()[1]
183 enum_name = re.sub(r';$', '', enum_name)
185 return StructDoc(enum_name, content, members, 'macro')
187 def process_struct(infile):
190 content = read_content(infile)
192 line = infile.readline()
195 elif re.match(r'\s*/\*\*\n', line):
196 member_content = read_content(infile)
197 line = infile.readline()
198 member_name = line.rstrip().rstrip(';')
199 members.append((member_name, member_content))
200 elif line.startswith('}') or\
201 (line.startswith('typedef ') and line.endswith(';\n')):
202 if line.startswith('}'):
206 struct_name = line.rstrip().split()[index]
207 struct_name = re.sub(r';$', '', struct_name)
209 return StructDoc(struct_name, content, members, 'member')
211 def process_function(domain, infile):
212 content = read_content(infile)
215 line = infile.readline()
221 func_proto.append(line)
222 func_proto = ''.join(func_proto)
223 func_proto = re.sub(r';\n$', '', func_proto)
224 func_proto = re.sub(r'\s+', ' ', func_proto)
225 func_proto = re.sub(r'NGHTTP2_EXTERN ', '', func_proto)
226 return FunctionDoc(func_proto, content, domain)
228 def read_content(infile):
231 line = infile.readline()
234 if re.match(r'\s*\*/\n', line):
237 content.append(transform_content(line.rstrip()))
240 def arg_repl(matchobj):
241 return '*{}*'.format(matchobj.group(1).replace('*', '\\*'))
243 def transform_content(content):
244 content = re.sub(r'^\s+\* ?', '', content)
245 content = re.sub(r'\|([^\s|]+)\|', arg_repl, content)
246 content = re.sub(r':enum:', ':macro:', content)
249 if __name__ == '__main__':
250 parser = argparse.ArgumentParser(description="Generate API reference")
251 parser.add_argument('index', type=argparse.FileType('w'),
252 help='index output file')
253 parser.add_argument('macros', type=argparse.FileType('w'),
254 help='macros section output file. The filename should be macros.rst')
255 parser.add_argument('enums', type=argparse.FileType('w'),
256 help='enums section output file. The filename should be enums.rst')
257 parser.add_argument('types', type=argparse.FileType('w'),
258 help='types section output file. The filename should be types.rst')
259 parser.add_argument('funcsdir',
260 help='functions doc output dir')
261 parser.add_argument('files', nargs='+', type=argparse.FileType('r'),
263 args = parser.parse_args()
268 for infile in args.files:
269 m, e, t, f = make_api_ref(args.files)
274 funcs.sort(key=lambda x: x.funcname)
276 args.index, args.macros, args.enums, args.types, args.funcsdir,
277 macros, enums, types, funcs)