mkapiref.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #!/usr/bin/env python
  2. #
  3. # aria2 - The high speed download utility
  4. #
  5. # Copyright (C) 2013 Tatsuhiro Tsujikawa
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. #
  21. # In addition, as a special exception, the copyright holders give
  22. # permission to link the code of portions of this program with the
  23. # OpenSSL library under certain conditions as described in each
  24. # individual source file, and distribute linked combinations
  25. # including the two.
  26. # You must obey the GNU General Public License in all respects
  27. # for all of the code used other than OpenSSL. If you modify
  28. # file(s) with this exception, you may extend this exception to your
  29. # version of the file(s), but you are not obligated to do so. If you
  30. # do not wish to do so, delete this exception statement from your
  31. # version. If you delete this exception statement from all source
  32. # files in the program, then also delete it here.
  33. #
  34. # Generates API reference from C++ source code.
  35. from __future__ import print_function
  36. import re, sys, argparse
  37. class FunctionDoc:
  38. def __init__(self, name, content, domain):
  39. self.name = name
  40. self.content = content
  41. self.domain = domain
  42. def write(self, out):
  43. print('''.. {}:: {}'''.format(self.domain, self.name))
  44. print()
  45. for line in self.content:
  46. print(' {}'.format(line))
  47. class TypedefDoc:
  48. def __init__(self, name, content):
  49. self.name = name
  50. self.content = content
  51. def write(self, out):
  52. print('''.. type:: {}'''.format(self.name))
  53. print()
  54. for line in self.content:
  55. print(' {}'.format(line))
  56. class StructDoc:
  57. def __init__(self, name, content, domain, members, member_domain):
  58. self.name = name
  59. self.content = content
  60. self.domain = domain
  61. self.members = members
  62. self.member_domain = member_domain
  63. def write(self, out):
  64. if self.name:
  65. print('''.. {}:: {}'''.format(self.domain, self.name))
  66. print()
  67. for line in self.content:
  68. print(' {}'.format(line))
  69. print()
  70. for name, content in self.members:
  71. name = name.strip()
  72. # For function (e.g., int foo())
  73. m = re.match(r'(.+)\s+([^ ]+\(.*)', name)
  74. if not m:
  75. # For variable (e.g., bool a)
  76. m = re.match(r'(.+)\s+([^ ]+)', name)
  77. if m:
  78. print(''' .. {}:: {} {}::{}'''.format(
  79. 'function' if name.endswith(')') else self.member_domain,
  80. m.group(1),
  81. self.name,
  82. m.group(2)))
  83. else:
  84. if name.endswith(')'):
  85. # For function, without return type, like
  86. # constructor
  87. print(''' .. {}:: {}::{}'''.format(
  88. 'function' if name.endswith(')') else self.member_domain,
  89. self.name, name))
  90. else:
  91. # enum
  92. print(''' .. {}:: {}'''.format(
  93. 'function' if name.endswith(')') else self.member_domain,
  94. name))
  95. print()
  96. for line in content:
  97. print(''' {}'''.format(line))
  98. print()
  99. class MacroDoc:
  100. def __init__(self, name, content):
  101. self.name = name
  102. self.content = content
  103. def write(self, out):
  104. print('''.. macro:: {}'''.format(self.name))
  105. print()
  106. for line in self.content:
  107. print(' {}'.format(line))
  108. def make_api_ref(infiles):
  109. macros = []
  110. enums = []
  111. types = []
  112. functions = []
  113. for infile in infiles:
  114. while True:
  115. line = infile.readline()
  116. if not line:
  117. break
  118. elif line == '/**\n':
  119. line = infile.readline()
  120. doctype = line.split()[1]
  121. if doctype == '@function':
  122. functions.append(process_function('function', infile))
  123. if doctype == '@functypedef':
  124. types.append(process_function('type', infile))
  125. elif doctype == '@typedef':
  126. types.append(process_typedef(infile))
  127. elif doctype in ['@class', '@struct', '@union']:
  128. types.append(process_struct(infile))
  129. elif doctype == '@enum':
  130. enums.append(process_enum(infile))
  131. elif doctype == '@macro':
  132. macros.append(process_macro(infile))
  133. alldocs = [('Macros', macros),
  134. ('Enums', enums),
  135. ('Types (classes, structs, unions and typedefs)', types),
  136. ('Functions', functions)]
  137. for title, docs in alldocs:
  138. if not docs:
  139. continue
  140. print(title)
  141. print('-'*len(title))
  142. for doc in docs:
  143. doc.write(sys.stdout)
  144. print()
  145. print()
  146. def process_macro(infile):
  147. content = read_content(infile)
  148. line = infile.readline()
  149. macro_name = line.split()[1]
  150. return MacroDoc(macro_name, content)
  151. def process_enum(infile):
  152. members = []
  153. enum_name = None
  154. content = read_content(infile)
  155. while True:
  156. line = infile.readline()
  157. if not line:
  158. break
  159. elif re.match(r'\s*/\*\*\n', line):
  160. member_content = read_content(infile)
  161. line = infile.readline()
  162. items = line.split()
  163. member_name = items[0].rstrip(',')
  164. if len(items) >= 3:
  165. member_content.insert(0, '(``{}``) '\
  166. .format(items[2].rstrip(',')))
  167. members.append((member_name, member_content))
  168. elif line.startswith('}'):
  169. if not enum_name:
  170. enum_name = line.rstrip().split()[1]
  171. enum_name = re.sub(r';$', '', enum_name)
  172. break
  173. elif not enum_name:
  174. m = re.match(r'^\s*enum\s+([\S]+)\s*{\s*', line)
  175. if m:
  176. enum_name = m.group(1)
  177. return StructDoc(enum_name, content, 'type', members, 'c:macro')
  178. def process_struct(infile):
  179. members = []
  180. domain = 'type'
  181. struct_name = None
  182. content = read_content(infile)
  183. while True:
  184. line = infile.readline()
  185. if not line:
  186. break
  187. elif re.match(r'\s*/\*\*\n', line):
  188. member_content = read_content(infile)
  189. line = infile.readline()
  190. member_name = line.rstrip().rstrip(';')
  191. member_name = re.sub(r'\)\s*=\s*0', ')', member_name)
  192. member_name = re.sub(r' virtual ', '', member_name)
  193. members.append((member_name, member_content))
  194. elif line.startswith('}') or\
  195. (line.startswith('typedef ') and line.endswith(';\n')):
  196. if not struct_name:
  197. if line.startswith('}'):
  198. index = 1
  199. else:
  200. index = 3
  201. struct_name = line.rstrip().split()[index]
  202. struct_name = re.sub(r';$', '', struct_name)
  203. break
  204. elif not struct_name:
  205. m = re.match(r'^\s*(struct|class)\s+([\S]+)\s*(?:{|;)', line)
  206. if m:
  207. domain = m.group(1)
  208. if domain == 'struct':
  209. domain = 'type'
  210. struct_name = m.group(2)
  211. if line.endswith(';\n'):
  212. break
  213. return StructDoc(struct_name, content, domain, members, 'member')
  214. def process_function(domain, infile):
  215. content = read_content(infile)
  216. func_proto = []
  217. while True:
  218. line = infile.readline()
  219. if not line:
  220. break
  221. elif line == '\n':
  222. break
  223. else:
  224. func_proto.append(line)
  225. func_proto = ''.join(func_proto)
  226. func_proto = re.sub(r';\n$', '', func_proto)
  227. func_proto = re.sub(r'\s+', ' ', func_proto)
  228. func_proto = re.sub(r'typedef ', '', func_proto)
  229. return FunctionDoc(func_proto, content, domain)
  230. def process_typedef(infile):
  231. content = read_content(infile)
  232. lines = []
  233. while True:
  234. line = infile.readline()
  235. if not line:
  236. break
  237. elif line == '\n':
  238. break
  239. else:
  240. lines.append(line)
  241. typedef = ''.join(lines)
  242. typedef = re.sub(r';\n$', '', typedef)
  243. typedef = re.sub(r'\s+', ' ', typedef)
  244. typedef = re.sub(r'typedef ', '', typedef)
  245. return TypedefDoc(typedef.split()[-1], content)
  246. def read_content(infile):
  247. content = []
  248. while True:
  249. line = infile.readline()
  250. if not line:
  251. break
  252. if re.match(r'\s*\*/\n', line):
  253. break
  254. else:
  255. content.append(transform_content(line.rstrip()))
  256. return content
  257. def arg_repl(matchobj):
  258. return '*{}*'.format(matchobj.group(1).replace('*', '\\*'))
  259. def transform_content(content):
  260. content = re.sub(r'^\s+\* ?', '', content)
  261. content = re.sub(r'\|([^\s|]+)\|', arg_repl, content)
  262. content = re.sub(r':enum:', ':macro:', content)
  263. return content
  264. if __name__ == '__main__':
  265. parser = argparse.ArgumentParser(description="Generate API reference")
  266. parser.add_argument('--header', type=argparse.FileType('rb', 0),
  267. help='header inserted at the top of the page')
  268. parser.add_argument('files', nargs='+', type=argparse.FileType('rb', 0),
  269. help='source file')
  270. args = parser.parse_args()
  271. if args.header:
  272. print(args.header.read())
  273. for infile in args.files:
  274. make_api_ref(args.files)