gen_vimdoc.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. #!/usr/bin/env python3
  2. """Generates Nvim help docs from C docstrings, by parsing Doxygen XML.
  3. This would be easier using lxml and XSLT, but:
  4. 1. This should avoid needing Python dependencies, especially ones that are
  5. C modules that have library dependencies (lxml requires libxml and
  6. libxslt).
  7. 2. I wouldn't know how to deal with nested indentation in <para> tags using
  8. XSLT.
  9. Each function documentation is formatted with the following rules:
  10. - Maximum width of 78 characters (`text_width`).
  11. - Spaces for indentation.
  12. - Function signature and helptag are on the same line.
  13. - Helptag is right aligned.
  14. - Signature and helptag must have a minimum of 8 spaces between them.
  15. - If the signature is too long, it is placed on the line after the
  16. helptag. The signature wraps at `text_width - 8` characters with
  17. subsequent lines indented to the open parenthesis.
  18. - Documentation body will be indented by 16 spaces.
  19. - Subsection bodies are indented an additional 4 spaces.
  20. - Documentation body consists of the function description, parameter details,
  21. return description, and C declaration.
  22. - Parameters are omitted for the `void` and `Error *` types, or if the
  23. parameter is marked as [out].
  24. - Each function documentation is separated by a single line.
  25. The C declaration is added to the end to show actual argument types.
  26. """
  27. import os
  28. import re
  29. import sys
  30. import shutil
  31. import textwrap
  32. import subprocess
  33. import collections
  34. from xml.dom import minidom
  35. if sys.version_info[0] < 3:
  36. print("use Python 3")
  37. sys.exit(1)
  38. DEBUG = ('DEBUG' in os.environ)
  39. INCLUDE_C_DECL = ('INCLUDE_C_DECL' in os.environ)
  40. INCLUDE_DEPRECATED = ('INCLUDE_DEPRECATED' in os.environ)
  41. text_width = 78
  42. script_path = os.path.abspath(__file__)
  43. base_dir = os.path.dirname(os.path.dirname(script_path))
  44. out_dir = os.path.join(base_dir, 'tmp-{mode}-doc')
  45. filter_cmd = '%s %s' % (sys.executable, script_path)
  46. seen_funcs = set()
  47. lua2dox_filter = os.path.join(base_dir, 'scripts', 'lua2dox_filter')
  48. CONFIG = {
  49. 'api': {
  50. 'filename': 'api.txt',
  51. # String used to find the start of the generated part of the doc.
  52. 'section_start_token': '*api-global*',
  53. # Section ordering.
  54. 'section_order': [
  55. 'vim.c',
  56. 'buffer.c',
  57. 'window.c',
  58. 'tabpage.c',
  59. 'ui.c',
  60. ],
  61. # List of files/directories for doxygen to read, separated by blanks
  62. 'files': os.path.join(base_dir, 'src/nvim/api'),
  63. # file patterns used by doxygen
  64. 'file_patterns': '*.h *.c',
  65. # Only function with this prefix are considered
  66. 'func_name_prefix': 'nvim_',
  67. # Section name overrides.
  68. 'section_name': {
  69. 'vim.c': 'Global',
  70. },
  71. # Module name overrides (for Lua).
  72. 'module_override': {},
  73. # Append the docs for these modules, do not start a new section.
  74. 'append_only': [],
  75. },
  76. 'lua': {
  77. 'filename': 'if_lua.txt',
  78. 'section_start_token': '*lua-vim*',
  79. 'section_order': [
  80. 'vim.lua',
  81. 'shared.lua',
  82. ],
  83. 'files': ' '.join([
  84. os.path.join(base_dir, 'src/nvim/lua/vim.lua'),
  85. os.path.join(base_dir, 'runtime/lua/vim/shared.lua'),
  86. ]),
  87. 'file_patterns': '*.lua',
  88. 'func_name_prefix': '',
  89. 'section_name': {},
  90. 'module_override': {
  91. # `shared` functions are exposed on the `vim` module.
  92. 'shared': 'vim',
  93. },
  94. 'append_only': [
  95. 'shared.lua',
  96. ],
  97. },
  98. }
  99. param_exclude = (
  100. 'channel_id',
  101. )
  102. # Annotations are displayed as line items after API function descriptions.
  103. annotation_map = {
  104. 'FUNC_API_FAST': '{fast}',
  105. }
  106. # Tracks `xrefsect` titles. As of this writing, used only for separating
  107. # deprecated functions.
  108. xrefs = set()
  109. def debug_this(s, n):
  110. o = n if isinstance(n, str) else n.toprettyxml(indent=' ', newl='\n')
  111. name = '' if isinstance(n, str) else n.nodeName
  112. if s in o:
  113. raise RuntimeError('xxx: {}\n{}'.format(name, o))
  114. # XML Parsing Utilities {{{
  115. def find_first(parent, name):
  116. """Finds the first matching node within parent."""
  117. sub = parent.getElementsByTagName(name)
  118. if not sub:
  119. return None
  120. return sub[0]
  121. def get_children(parent, name):
  122. """Yield matching child nodes within parent."""
  123. for child in parent.childNodes:
  124. if child.nodeType == child.ELEMENT_NODE and child.nodeName == name:
  125. yield child
  126. def get_child(parent, name):
  127. """Get the first matching child node."""
  128. for child in get_children(parent, name):
  129. return child
  130. return None
  131. def clean_text(text):
  132. """Cleans text.
  133. Only cleans superfluous whitespace at the moment.
  134. """
  135. return ' '.join(text.split()).strip()
  136. def clean_lines(text):
  137. """Removes superfluous lines.
  138. The beginning and end of the string is trimmed. Empty lines are collapsed.
  139. """
  140. return re.sub(r'\A\n\s*\n*|\n\s*\n*\Z', '', re.sub(r'(\n\s*\n+)+', '\n\n', text))
  141. def is_blank(text):
  142. return '' == clean_lines(text)
  143. def get_text(parent, preformatted=False):
  144. """Combine all text in a node."""
  145. if parent.nodeType == parent.TEXT_NODE:
  146. return parent.data
  147. out = ''
  148. for node in parent.childNodes:
  149. if node.nodeType == node.TEXT_NODE:
  150. out += node.data if preformatted else clean_text(node.data)
  151. elif node.nodeType == node.ELEMENT_NODE:
  152. out += ' ' + get_text(node, preformatted)
  153. return out
  154. # Gets the length of the last line in `text`, excluding newline ("\n") char.
  155. def len_lastline(text):
  156. lastnl = text.rfind('\n')
  157. if -1 == lastnl:
  158. return len(text)
  159. if '\n' == text[-1]:
  160. return lastnl - (1 + text.rfind('\n', 0, lastnl))
  161. return len(text) - (1 + lastnl)
  162. def len_lastline_withoutindent(text, indent):
  163. n = len_lastline(text)
  164. return (n - len(indent)) if n > len(indent) else 0
  165. # Returns True if node `n` contains only inline (not block-level) elements.
  166. def is_inline(n):
  167. for c in n.childNodes:
  168. if c.nodeType != c.TEXT_NODE and c.nodeName != 'computeroutput':
  169. return False
  170. if not is_inline(c):
  171. return False
  172. return True
  173. def doc_wrap(text, prefix='', width=70, func=False, indent=None):
  174. """Wraps text to `width`.
  175. First line is prefixed with `prefix`, subsequent lines are aligned.
  176. If `func` is True, only wrap at commas.
  177. """
  178. if not width:
  179. # return prefix + text
  180. return text
  181. # Whitespace used to indent all lines except the first line.
  182. indent = ' ' * len(prefix) if indent is None else indent
  183. indent_only = (prefix == '' and indent is not None)
  184. if func:
  185. lines = [prefix]
  186. for part in text.split(', '):
  187. if part[-1] not in ');':
  188. part += ', '
  189. if len(lines[-1]) + len(part) > width:
  190. lines.append(indent)
  191. lines[-1] += part
  192. return '\n'.join(x.rstrip() for x in lines).rstrip()
  193. # XXX: Dummy prefix to force TextWrapper() to wrap the first line.
  194. if indent_only:
  195. prefix = indent
  196. tw = textwrap.TextWrapper(break_long_words=False,
  197. break_on_hyphens=False,
  198. width=width,
  199. initial_indent=prefix,
  200. subsequent_indent=indent)
  201. result = '\n'.join(tw.wrap(text.strip()))
  202. # XXX: Remove the dummy prefix.
  203. if indent_only:
  204. result = result[len(indent):]
  205. return result
  206. def has_nonexcluded_params(nodes):
  207. """Returns true if any of the given <parameterlist> elements has at least
  208. one non-excluded item."""
  209. for n in nodes:
  210. if render_params(n) != '':
  211. return True
  212. def render_params(parent, width=62):
  213. """Renders Doxygen <parameterlist> tag as Vim help text."""
  214. name_length = 0
  215. items = []
  216. for node in parent.childNodes:
  217. if node.nodeType == node.TEXT_NODE:
  218. continue
  219. name_node = find_first(node, 'parametername')
  220. if name_node.getAttribute('direction') == 'out':
  221. continue
  222. name = get_text(name_node)
  223. if name in param_exclude:
  224. continue
  225. name = '{%s}' % name
  226. name_length = max(name_length, len(name) + 2)
  227. items.append((name.strip(), node))
  228. out = ''
  229. for name, node in items:
  230. name = ' {}'.format(name.ljust(name_length))
  231. desc = ''
  232. desc_node = get_child(node, 'parameterdescription')
  233. if desc_node:
  234. desc = parse_parblock(desc_node, width=width,
  235. indent=(' ' * len(name)))
  236. out += '{}{}\n'.format(name, desc)
  237. return out.rstrip()
  238. def render_node(n, text, prefix='', indent='', width=62):
  239. """Renders a node as Vim help text, recursively traversing all descendants."""
  240. text = ''
  241. # space_preceding = (len(text) > 0 and ' ' == text[-1][-1])
  242. # text += (int(not space_preceding) * ' ')
  243. if n.nodeType == n.TEXT_NODE:
  244. # `prefix` is NOT sent to doc_wrap, it was already handled by now.
  245. text += doc_wrap(n.data, indent=indent, width=width)
  246. elif n.nodeName == 'computeroutput':
  247. text += ' `{}` '.format(get_text(n))
  248. elif n.nodeName == 'preformatted':
  249. o = get_text(n, preformatted=True)
  250. ensure_nl = '' if o[-1] == '\n' else '\n'
  251. text += ' >{}{}\n<'.format(ensure_nl, o)
  252. elif is_inline(n):
  253. for c in n.childNodes:
  254. text += render_node(c, text)
  255. text = doc_wrap(text, indent=indent, width=width)
  256. elif n.nodeName == 'verbatim':
  257. # TODO: currently we don't use this. The "[verbatim]" hint is there as
  258. # a reminder that we must decide how to format this if we do use it.
  259. text += ' [verbatim] {}'.format(get_text(n))
  260. elif n.nodeName == 'listitem':
  261. for c in n.childNodes:
  262. text += (
  263. indent
  264. + prefix
  265. + render_node(c, text, indent=indent + (' ' * len(prefix)), width=width)
  266. )
  267. elif n.nodeName in ('para', 'heading'):
  268. for c in n.childNodes:
  269. text += render_node(c, text, indent=indent, width=width)
  270. if is_inline(n):
  271. text = doc_wrap(text, indent=indent, width=width)
  272. elif n.nodeName == 'itemizedlist':
  273. for c in n.childNodes:
  274. text += '{}\n'.format(render_node(c, text, prefix='• ',
  275. indent=indent, width=width))
  276. elif n.nodeName == 'orderedlist':
  277. i = 1
  278. for c in n.childNodes:
  279. if is_blank(get_text(c)):
  280. text += '\n'
  281. continue
  282. text += '{}\n'.format(render_node(c, text, prefix='{}. '.format(i),
  283. indent=indent, width=width))
  284. i = i + 1
  285. elif n.nodeName == 'simplesect' and 'note' == n.getAttribute('kind'):
  286. text += 'Note:\n '
  287. for c in n.childNodes:
  288. text += render_node(c, text, indent=' ', width=width)
  289. text += '\n'
  290. elif n.nodeName == 'simplesect' and 'warning' == n.getAttribute('kind'):
  291. text += 'Warning:\n '
  292. for c in n.childNodes:
  293. text += render_node(c, text, indent=' ', width=width)
  294. text += '\n'
  295. elif (n.nodeName == 'simplesect'
  296. and n.getAttribute('kind') in ('return', 'see')):
  297. text += ' '
  298. for c in n.childNodes:
  299. text += render_node(c, text, indent=' ', width=width)
  300. else:
  301. raise RuntimeError('unhandled node type: {}\n{}'.format(
  302. n.nodeName, n.toprettyxml(indent=' ', newl='\n')))
  303. return text
  304. def render_para(parent, indent='', width=62):
  305. """Renders Doxygen <para> containing arbitrary nodes.
  306. NB: Blank lines in a docstring manifest as <para> tags.
  307. """
  308. if is_inline(parent):
  309. return clean_lines(doc_wrap(render_node(parent, ''),
  310. indent=indent, width=width).strip())
  311. # Ordered dict of ordered lists.
  312. groups = collections.OrderedDict([
  313. ('params', []),
  314. ('return', []),
  315. ('seealso', []),
  316. ('xrefs', []),
  317. ])
  318. # Gather nodes into groups. Mostly this is because we want "parameterlist"
  319. # nodes to appear together.
  320. text = ''
  321. kind = ''
  322. last = ''
  323. for child in parent.childNodes:
  324. if child.nodeName == 'parameterlist':
  325. groups['params'].append(child)
  326. elif child.nodeName == 'xrefsect':
  327. groups['xrefs'].append(child)
  328. elif child.nodeName == 'simplesect':
  329. last = kind
  330. kind = child.getAttribute('kind')
  331. if kind == 'return' or (kind == 'note' and last == 'return'):
  332. groups['return'].append(child)
  333. elif kind == 'see':
  334. groups['seealso'].append(child)
  335. elif kind in ('note', 'warning'):
  336. text += render_node(child, text, indent=indent, width=width)
  337. else:
  338. raise RuntimeError('unhandled simplesect: {}\n{}'.format(
  339. child.nodeName, child.toprettyxml(indent=' ', newl='\n')))
  340. else:
  341. text += render_node(child, text, indent=indent, width=width)
  342. chunks = [text]
  343. # Generate text from the gathered items.
  344. if len(groups['params']) > 0 and has_nonexcluded_params(groups['params']):
  345. chunks.append('\nParameters: ~')
  346. for child in groups['params']:
  347. chunks.append(render_params(child, width=width))
  348. if len(groups['return']) > 0:
  349. chunks.append('\nReturn: ~')
  350. for child in groups['return']:
  351. chunks.append(render_node(
  352. child, chunks[-1][-1], indent=indent, width=width))
  353. if len(groups['seealso']) > 0:
  354. chunks.append('\nSee also: ~')
  355. for child in groups['seealso']:
  356. chunks.append(render_node(
  357. child, chunks[-1][-1], indent=indent, width=width))
  358. for child in groups['xrefs']:
  359. title = get_text(get_child(child, 'xreftitle'))
  360. xrefs.add(title)
  361. xrefdesc = render_para(get_child(child, 'xrefdescription'), width=width)
  362. chunks.append(doc_wrap(xrefdesc, prefix='{}: '.format(title),
  363. width=width) + '\n')
  364. return clean_lines('\n'.join(chunks).strip())
  365. def parse_parblock(parent, prefix='', width=62, indent=''):
  366. """Renders a nested block of <para> tags as Vim help text."""
  367. paragraphs = []
  368. for child in parent.childNodes:
  369. paragraphs.append(render_para(child, width=width, indent=indent))
  370. paragraphs.append('')
  371. return clean_lines('\n'.join(paragraphs).strip())
  372. # }}}
  373. def parse_source_xml(filename, mode):
  374. """Collects API functions.
  375. Returns two strings:
  376. 1. API functions
  377. 2. Deprecated API functions
  378. Caller decides what to do with the deprecated documentation.
  379. """
  380. global xrefs
  381. xrefs = set()
  382. functions = []
  383. deprecated_functions = []
  384. dom = minidom.parse(filename)
  385. compoundname = get_text(dom.getElementsByTagName('compoundname')[0])
  386. for member in dom.getElementsByTagName('memberdef'):
  387. if member.getAttribute('static') == 'yes' or \
  388. member.getAttribute('kind') != 'function' or \
  389. member.getAttribute('prot') == 'private' or \
  390. get_text(get_child(member, 'name')).startswith('_'):
  391. continue
  392. loc = find_first(member, 'location')
  393. if 'private' in loc.getAttribute('file'):
  394. continue
  395. return_type = get_text(get_child(member, 'type'))
  396. if return_type == '':
  397. continue
  398. if return_type.startswith(('ArrayOf', 'DictionaryOf')):
  399. parts = return_type.strip('_').split('_')
  400. return_type = '{}({})'.format(parts[0], ', '.join(parts[1:]))
  401. name = get_text(get_child(member, 'name'))
  402. annotations = get_text(get_child(member, 'argsstring'))
  403. if annotations and ')' in annotations:
  404. annotations = annotations.rsplit(')', 1)[-1].strip()
  405. # XXX: (doxygen 1.8.11) 'argsstring' only includes attributes of
  406. # non-void functions. Special-case void functions here.
  407. if name == 'nvim_get_mode' and len(annotations) == 0:
  408. annotations += 'FUNC_API_FAST'
  409. annotations = filter(None, map(lambda x: annotation_map.get(x),
  410. annotations.split()))
  411. if mode == 'lua':
  412. fstem = compoundname.split('.')[0]
  413. fstem = CONFIG[mode]['module_override'].get(fstem, fstem)
  414. vimtag = '*{}.{}()*'.format(fstem, name)
  415. else:
  416. vimtag = '*{}()*'.format(name)
  417. params = []
  418. type_length = 0
  419. for param in get_children(member, 'param'):
  420. param_type = get_text(get_child(param, 'type')).strip()
  421. param_name = ''
  422. declname = get_child(param, 'declname')
  423. if declname:
  424. param_name = get_text(declname).strip()
  425. elif mode == 'lua':
  426. # that's how it comes out of lua2dox
  427. param_name = param_type
  428. param_type = ''
  429. if param_name in param_exclude:
  430. continue
  431. if param_type.endswith('*'):
  432. param_type = param_type.strip('* ')
  433. param_name = '*' + param_name
  434. type_length = max(type_length, len(param_type))
  435. params.append((param_type, param_name))
  436. c_args = []
  437. for param_type, param_name in params:
  438. c_args.append(' ' + (
  439. '%s %s' % (param_type.ljust(type_length), param_name)).strip())
  440. c_decl = textwrap.indent('%s %s(\n%s\n);' % (return_type, name,
  441. ',\n'.join(c_args)),
  442. ' ')
  443. prefix = '%s(' % name
  444. suffix = '%s)' % ', '.join('{%s}' % a[1] for a in params
  445. if a[0] not in ('void', 'Error'))
  446. # Minimum 8 chars between signature and vimtag
  447. lhs = (text_width - 8) - len(prefix)
  448. if len(prefix) + len(suffix) > lhs:
  449. signature = vimtag.rjust(text_width) + '\n'
  450. signature += doc_wrap(suffix, width=text_width-8, prefix=prefix,
  451. func=True)
  452. else:
  453. signature = prefix + suffix
  454. signature += vimtag.rjust(text_width - len(signature))
  455. doc = ''
  456. desc = find_first(member, 'detaileddescription')
  457. if desc:
  458. doc = parse_parblock(desc)
  459. if DEBUG:
  460. print(textwrap.indent(
  461. re.sub(r'\n\s*\n+', '\n',
  462. desc.toprettyxml(indent=' ', newl='\n')), ' ' * 16))
  463. if not doc:
  464. doc = 'TODO: Documentation'
  465. annotations = '\n'.join(annotations)
  466. if annotations:
  467. annotations = ('\n\nAttributes: ~\n' +
  468. textwrap.indent(annotations, ' '))
  469. i = doc.rfind('Parameters: ~')
  470. if i == -1:
  471. doc += annotations
  472. else:
  473. doc = doc[:i] + annotations + '\n\n' + doc[i:]
  474. if INCLUDE_C_DECL:
  475. doc += '\n\nC Declaration: ~\n>\n'
  476. doc += c_decl
  477. doc += '\n<'
  478. func_doc = signature + '\n'
  479. func_doc += textwrap.indent(clean_lines(doc), ' ' * 16)
  480. func_doc = re.sub(r'^\s+([<>])$', r'\1', func_doc, flags=re.M)
  481. if 'Deprecated' in xrefs:
  482. deprecated_functions.append(func_doc)
  483. elif name.startswith(CONFIG[mode]['func_name_prefix']):
  484. functions.append(func_doc)
  485. xrefs.clear()
  486. return '\n\n'.join(functions), '\n\n'.join(deprecated_functions)
  487. def delete_lines_below(filename, tokenstr):
  488. """Deletes all lines below the line containing `tokenstr`, the line itself,
  489. and one line above it.
  490. """
  491. lines = open(filename).readlines()
  492. i = 0
  493. for i, line in enumerate(lines, 1):
  494. if tokenstr in line:
  495. break
  496. i = max(0, i - 2)
  497. with open(filename, 'wt') as fp:
  498. fp.writelines(lines[0:i])
  499. def gen_docs(config):
  500. """Generate documentation.
  501. Doxygen is called and configured through stdin.
  502. """
  503. for mode in CONFIG:
  504. output_dir = out_dir.format(mode=mode)
  505. p = subprocess.Popen(['doxygen', '-'], stdin=subprocess.PIPE)
  506. p.communicate(
  507. config.format(
  508. input=CONFIG[mode]['files'],
  509. output=output_dir,
  510. filter=filter_cmd,
  511. file_patterns=CONFIG[mode]['file_patterns'])
  512. .encode('utf8')
  513. )
  514. if p.returncode:
  515. sys.exit(p.returncode)
  516. sections = {}
  517. intros = {}
  518. sep = '=' * text_width
  519. base = os.path.join(output_dir, 'xml')
  520. dom = minidom.parse(os.path.join(base, 'index.xml'))
  521. # generate docs for section intros
  522. for compound in dom.getElementsByTagName('compound'):
  523. if compound.getAttribute('kind') != 'group':
  524. continue
  525. groupname = get_text(find_first(compound, 'name'))
  526. groupxml = os.path.join(base, '%s.xml' %
  527. compound.getAttribute('refid'))
  528. desc = find_first(minidom.parse(groupxml), 'detaileddescription')
  529. if desc:
  530. doc = parse_parblock(desc)
  531. if doc:
  532. intros[groupname] = doc
  533. for compound in dom.getElementsByTagName('compound'):
  534. if compound.getAttribute('kind') != 'file':
  535. continue
  536. filename = get_text(find_first(compound, 'name'))
  537. if filename.endswith('.c') or filename.endswith('.lua'):
  538. functions, deprecated = parse_source_xml(
  539. os.path.join(base, '%s.xml' %
  540. compound.getAttribute('refid')), mode)
  541. if not functions and not deprecated:
  542. continue
  543. if functions or deprecated:
  544. name = os.path.splitext(os.path.basename(filename))[0]
  545. if name == 'ui':
  546. name = name.upper()
  547. else:
  548. name = name.title()
  549. doc = ''
  550. intro = intros.get('api-%s' % name.lower())
  551. if intro:
  552. doc += '\n\n' + intro
  553. if functions:
  554. doc += '\n\n' + functions
  555. if INCLUDE_DEPRECATED and deprecated:
  556. doc += '\n\n\nDeprecated %s Functions: ~\n\n' % name
  557. doc += deprecated
  558. if doc:
  559. filename = os.path.basename(filename)
  560. name = CONFIG[mode]['section_name'].get(filename, name)
  561. if mode == 'lua':
  562. title = 'Lua module: {}'.format(name.lower())
  563. helptag = '*lua-{}*'.format(name.lower())
  564. else:
  565. title = '{} Functions'.format(name)
  566. helptag = '*api-{}*'.format(name.lower())
  567. sections[filename] = (title, helptag, doc)
  568. if not sections:
  569. return
  570. docs = ''
  571. i = 0
  572. for filename in CONFIG[mode]['section_order']:
  573. if filename not in sections:
  574. raise RuntimeError(
  575. 'found new module "{}"; update the "section_order" map'.format(
  576. filename))
  577. title, helptag, section_doc = sections.pop(filename)
  578. i += 1
  579. if filename not in CONFIG[mode]['append_only']:
  580. docs += sep
  581. docs += '\n%s%s' % (title,
  582. helptag.rjust(text_width - len(title)))
  583. docs += section_doc
  584. docs += '\n\n\n'
  585. docs = docs.rstrip() + '\n\n'
  586. docs += ' vim:tw=78:ts=8:ft=help:norl:\n'
  587. doc_file = os.path.join(base_dir, 'runtime', 'doc',
  588. CONFIG[mode]['filename'])
  589. delete_lines_below(doc_file, CONFIG[mode]['section_start_token'])
  590. with open(doc_file, 'ab') as fp:
  591. fp.write(docs.encode('utf8'))
  592. shutil.rmtree(output_dir)
  593. def filter_source(filename):
  594. name, extension = os.path.splitext(filename)
  595. if extension == '.lua':
  596. p = subprocess.run([lua2dox_filter, filename], stdout=subprocess.PIPE)
  597. op = ('?' if 0 != p.returncode else p.stdout.decode('utf-8'))
  598. print(op)
  599. else:
  600. """Filters the source to fix macros that confuse Doxygen."""
  601. with open(filename, 'rt') as fp:
  602. print(re.sub(r'^(ArrayOf|DictionaryOf)(\(.*?\))',
  603. lambda m: m.group(1)+'_'.join(
  604. re.split(r'[^\w]+', m.group(2))),
  605. fp.read(), flags=re.M))
  606. # Doxygen Config {{{
  607. Doxyfile = '''
  608. OUTPUT_DIRECTORY = {output}
  609. INPUT = {input}
  610. INPUT_ENCODING = UTF-8
  611. FILE_PATTERNS = {file_patterns}
  612. RECURSIVE = YES
  613. INPUT_FILTER = "{filter}"
  614. EXCLUDE =
  615. EXCLUDE_SYMLINKS = NO
  616. EXCLUDE_PATTERNS = */private/*
  617. EXCLUDE_SYMBOLS =
  618. EXTENSION_MAPPING = lua=C
  619. EXTRACT_PRIVATE = NO
  620. GENERATE_HTML = NO
  621. GENERATE_DOCSET = NO
  622. GENERATE_HTMLHELP = NO
  623. GENERATE_QHP = NO
  624. GENERATE_TREEVIEW = NO
  625. GENERATE_LATEX = NO
  626. GENERATE_RTF = NO
  627. GENERATE_MAN = NO
  628. GENERATE_DOCBOOK = NO
  629. GENERATE_AUTOGEN_DEF = NO
  630. GENERATE_XML = YES
  631. XML_OUTPUT = xml
  632. XML_PROGRAMLISTING = NO
  633. ENABLE_PREPROCESSING = YES
  634. MACRO_EXPANSION = YES
  635. EXPAND_ONLY_PREDEF = NO
  636. MARKDOWN_SUPPORT = YES
  637. '''
  638. # }}}
  639. if __name__ == "__main__":
  640. if len(sys.argv) > 1:
  641. filter_source(sys.argv[1])
  642. else:
  643. gen_docs(Doxyfile)
  644. # vim: set ft=python ts=4 sw=4 tw=79 et fdm=marker :