mkapiref.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. elif doctype == '@typedef':
  102. types.append(process_typedef(infile))
  103. elif doctype in ['@class', '@struct', '@union']:
  104. types.append(process_struct(infile))
  105. elif doctype == '@enum':
  106. enums.append(process_enum(infile))
  107. elif doctype == '@macro':
  108. macros.append(process_macro(infile))
  109. alldocs = [('Macros', macros),
  110. ('Enums', enums),
  111. ('Types (classes, structs, unions and typedefs)', types),
  112. ('Functions', functions)]
  113. for title, docs in alldocs:
  114. if not docs:
  115. continue
  116. print title
  117. print '-'*len(title)
  118. for doc in docs:
  119. doc.write(sys.stdout)
  120. print ''
  121. print ''
  122. def process_macro(infile):
  123. content = read_content(infile)
  124. line = infile.readline()
  125. macro_name = line.split()[1]
  126. return MacroDoc(macro_name, content)
  127. def process_enum(infile):
  128. members = []
  129. enum_name = None
  130. content = read_content(infile)
  131. while True:
  132. line = infile.readline()
  133. if not line:
  134. break
  135. elif re.match(r'\s*/\*\*\n', line):
  136. member_content = read_content(infile)
  137. line = infile.readline()
  138. items = line.split()
  139. member_name = items[0].rstrip(',')
  140. if len(items) >= 3:
  141. member_content.insert(0, '(``{}``) '\
  142. .format(items[2].rstrip(',')))
  143. members.append((member_name, member_content))
  144. elif line.startswith('}'):
  145. if not enum_name:
  146. enum_name = line.rstrip().split()[1]
  147. enum_name = re.sub(r';$', '', enum_name)
  148. break
  149. elif not enum_name:
  150. m = re.match(r'^\s*enum\s+([\S]+)\s*{\s*', line)
  151. if m:
  152. enum_name = m.group(1)
  153. return StructDoc(enum_name, content, 'type', members, 'c:macro')
  154. def process_struct(infile):
  155. members = []
  156. domain = 'type'
  157. struct_name = None
  158. content = read_content(infile)
  159. while True:
  160. line = infile.readline()
  161. if not line:
  162. break
  163. elif re.match(r'\s*/\*\*\n', line):
  164. member_content = read_content(infile)
  165. line = infile.readline()
  166. member_name = line.rstrip().rstrip(';')
  167. member_name = re.sub(r'\)\s*=\s*0', ')', member_name)
  168. member_name = re.sub(r' virtual ', '', member_name)
  169. members.append((member_name, member_content))
  170. elif line.startswith('}') or\
  171. (line.startswith('typedef ') and line.endswith(';\n')):
  172. if not struct_name:
  173. if line.startswith('}'):
  174. index = 1
  175. else:
  176. index = 3
  177. struct_name = line.rstrip().split()[index]
  178. struct_name = re.sub(r';$', '', struct_name)
  179. break
  180. elif not struct_name:
  181. m = re.match(r'^\s*(struct|class)\s+([\S]+)\s*(?:{|;)', line)
  182. if m:
  183. domain = m.group(1)
  184. if domain == 'struct':
  185. domain = 'type'
  186. struct_name = m.group(2)
  187. if line.endswith(';\n'):
  188. break
  189. return StructDoc(struct_name, content, domain, members, 'member')
  190. def process_function(domain, infile):
  191. content = read_content(infile)
  192. func_proto = []
  193. while True:
  194. line = infile.readline()
  195. if not line:
  196. break
  197. elif line == '\n':
  198. break
  199. else:
  200. func_proto.append(line)
  201. func_proto = ''.join(func_proto)
  202. func_proto = re.sub(r';\n$', '', func_proto)
  203. func_proto = re.sub(r'\s+', ' ', func_proto)
  204. return FunctionDoc(func_proto, content, domain)
  205. def process_typedef(infile):
  206. content = read_content(infile)
  207. lines = []
  208. while True:
  209. line = infile.readline()
  210. if not line:
  211. break
  212. elif line == '\n':
  213. break
  214. else:
  215. lines.append(line)
  216. typedef = ''.join(lines)
  217. typedef = re.sub(r';\n$', '', typedef)
  218. typedef = re.sub(r'\s+', ' ', typedef)
  219. return TypedefDoc(typedef.split()[-1], content)
  220. def read_content(infile):
  221. content = []
  222. while True:
  223. line = infile.readline()
  224. if not line:
  225. break
  226. if re.match(r'\s*\*/\n', line):
  227. break
  228. else:
  229. content.append(transform_content(line.rstrip()))
  230. return content
  231. def arg_repl(matchobj):
  232. return '*{}*'.format(matchobj.group(1).replace('*', '\\*'))
  233. def transform_content(content):
  234. content = re.sub(r'^\s+\* ?', '', content)
  235. content = re.sub(r'\|([^\s|]+)\|', arg_repl, content)
  236. content = re.sub(r':enum:', ':macro:', content)
  237. return content
  238. if __name__ == '__main__':
  239. parser = argparse.ArgumentParser(description="Generate API reference")
  240. parser.add_argument('--header', type=argparse.FileType('rb', 0),
  241. help='header inserted at the top of the page')
  242. parser.add_argument('files', nargs='+', type=argparse.FileType('rb', 0),
  243. help='source file')
  244. args = parser.parse_args()
  245. if args.header:
  246. print args.header.read()
  247. for infile in args.files:
  248. make_api_ref(args.files)