mkapiref.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. import re, sys, argparse
  36. class FunctionDoc:
  37. def __init__(self, name, content, domain):
  38. self.name = name
  39. self.content = content
  40. self.domain = domain
  41. def write(self, out):
  42. print '''.. {}:: {}'''.format(self.domain, self.name)
  43. print ''
  44. for line in self.content:
  45. print ' {}'.format(line)
  46. class TypedefDoc:
  47. def __init__(self, name, content):
  48. self.name = name
  49. self.content = content
  50. def write(self, out):
  51. print '''.. type:: {}'''.format(self.name)
  52. print ''
  53. for line in self.content:
  54. print ' {}'.format(line)
  55. class StructDoc:
  56. def __init__(self, name, content, domain, members, member_domain):
  57. self.name = name
  58. self.content = content
  59. self.domain = domain
  60. self.members = members
  61. self.member_domain = member_domain
  62. def write(self, out):
  63. if self.name:
  64. print '''.. {}:: {}'''.format(self.domain, self.name)
  65. print ''
  66. for line in self.content:
  67. print ' {}'.format(line)
  68. print ''
  69. for name, content in self.members:
  70. print ''' .. {}:: {}'''.format(\
  71. 'function' if name.endswith(')') else self.member_domain,
  72. name)
  73. print ''
  74. for line in content:
  75. print ''' {}'''.format(line)
  76. print ''
  77. class MacroDoc:
  78. def __init__(self, name, content):
  79. self.name = name
  80. self.content = content
  81. def write(self, out):
  82. print '''.. macro:: {}'''.format(self.name)
  83. print ''
  84. for line in self.content:
  85. print ' {}'.format(line)
  86. def make_api_ref(infiles):
  87. macros = []
  88. enums = []
  89. types = []
  90. functions = []
  91. for infile in infiles:
  92. while True:
  93. line = infile.readline()
  94. if not line:
  95. break
  96. elif line == '/**\n':
  97. line = infile.readline()
  98. doctype = line.split()[1]
  99. if doctype == '@function':
  100. functions.append(process_function('function', infile))
  101. if doctype == '@functypedef':
  102. types.append(process_function('c:type', infile))
  103. elif doctype == '@typedef':
  104. types.append(process_typedef(infile))
  105. elif doctype in ['@class', '@struct', '@union']:
  106. types.append(process_struct(infile))
  107. elif doctype == '@enum':
  108. enums.append(process_enum(infile))
  109. elif doctype == '@macro':
  110. macros.append(process_macro(infile))
  111. alldocs = [('Macros', macros),
  112. ('Enums', enums),
  113. ('Types (classes, structs, unions and typedefs)', types),
  114. ('Functions', functions)]
  115. for title, docs in alldocs:
  116. if not docs:
  117. continue
  118. print title
  119. print '-'*len(title)
  120. for doc in docs:
  121. doc.write(sys.stdout)
  122. print ''
  123. print ''
  124. def process_macro(infile):
  125. content = read_content(infile)
  126. line = infile.readline()
  127. macro_name = line.split()[1]
  128. return MacroDoc(macro_name, content)
  129. def process_enum(infile):
  130. members = []
  131. enum_name = None
  132. content = read_content(infile)
  133. while True:
  134. line = infile.readline()
  135. if not line:
  136. break
  137. elif re.match(r'\s*/\*\*\n', line):
  138. member_content = read_content(infile)
  139. line = infile.readline()
  140. items = line.split()
  141. member_name = items[0].rstrip(',')
  142. if len(items) >= 3:
  143. member_content.insert(0, '(``{}``) '\
  144. .format(items[2].rstrip(',')))
  145. members.append((member_name, member_content))
  146. elif line.startswith('}'):
  147. if not enum_name:
  148. enum_name = line.rstrip().split()[1]
  149. enum_name = re.sub(r';$', '', enum_name)
  150. break
  151. elif not enum_name:
  152. m = re.match(r'^\s*enum\s+([\S]+)\s*{\s*', line)
  153. if m:
  154. enum_name = m.group(1)
  155. return StructDoc(enum_name, content, 'type', members, 'c:macro')
  156. def process_struct(infile):
  157. members = []
  158. domain = 'type'
  159. struct_name = None
  160. content = read_content(infile)
  161. while True:
  162. line = infile.readline()
  163. if not line:
  164. break
  165. elif re.match(r'\s*/\*\*\n', line):
  166. member_content = read_content(infile)
  167. line = infile.readline()
  168. member_name = line.rstrip().rstrip(';')
  169. member_name = re.sub(r'\)\s*=\s*0', ')', member_name)
  170. member_name = re.sub(r' virtual ', '', member_name)
  171. members.append((member_name, member_content))
  172. elif line.startswith('}') or\
  173. (line.startswith('typedef ') and line.endswith(';\n')):
  174. if not struct_name:
  175. if line.startswith('}'):
  176. index = 1
  177. else:
  178. index = 3
  179. struct_name = line.rstrip().split()[index]
  180. struct_name = re.sub(r';$', '', struct_name)
  181. break
  182. elif not struct_name:
  183. m = re.match(r'^\s*(struct|class)\s+([\S]+)\s*(?:{|;)', line)
  184. if m:
  185. domain = m.group(1)
  186. if domain == 'struct':
  187. domain = 'type'
  188. struct_name = m.group(2)
  189. if line.endswith(';\n'):
  190. break
  191. return StructDoc(struct_name, content, domain, members, 'member')
  192. def process_function(domain, infile):
  193. content = read_content(infile)
  194. func_proto = []
  195. while True:
  196. line = infile.readline()
  197. if not line:
  198. break
  199. elif line == '\n':
  200. break
  201. else:
  202. func_proto.append(line)
  203. func_proto = ''.join(func_proto)
  204. func_proto = re.sub(r';\n$', '', func_proto)
  205. func_proto = re.sub(r'\s+', ' ', func_proto)
  206. return FunctionDoc(func_proto, content, domain)
  207. def process_typedef(infile):
  208. content = read_content(infile)
  209. lines = []
  210. while True:
  211. line = infile.readline()
  212. if not line:
  213. break
  214. elif line == '\n':
  215. break
  216. else:
  217. lines.append(line)
  218. typedef = ''.join(lines)
  219. typedef = re.sub(r';\n$', '', typedef)
  220. typedef = re.sub(r'\s+', ' ', typedef)
  221. return TypedefDoc(typedef.split()[-1], content)
  222. def read_content(infile):
  223. content = []
  224. while True:
  225. line = infile.readline()
  226. if not line:
  227. break
  228. if re.match(r'\s*\*/\n', line):
  229. break
  230. else:
  231. content.append(transform_content(line.rstrip()))
  232. return content
  233. def arg_repl(matchobj):
  234. return '*{}*'.format(matchobj.group(1).replace('*', '\\*'))
  235. def transform_content(content):
  236. content = re.sub(r'^\s+\* ?', '', content)
  237. content = re.sub(r'\|([^\s|]+)\|', arg_repl, content)
  238. content = re.sub(r':enum:', ':macro:', content)
  239. return content
  240. if __name__ == '__main__':
  241. parser = argparse.ArgumentParser(description="Generate API reference")
  242. parser.add_argument('--header', type=argparse.FileType('rb', 0),
  243. help='header inserted at the top of the page')
  244. parser.add_argument('files', nargs='+', type=argparse.FileType('rb', 0),
  245. help='source file')
  246. args = parser.parse_args()
  247. if args.header:
  248. print args.header.read()
  249. for infile in args.files:
  250. make_api_ref(args.files)