mkapiref.py 8.9 KB

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