refcounting.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """
  2. refcounting
  3. ~~~~~~~~~~~
  4. Reference count annotations for C API functions. Has the same
  5. result as the sphinx.ext.refcounting extension but works for all
  6. functions regardless of the signature, and the reference counting
  7. information is written inline with the documentation instead of a
  8. separate file.
  9. Adds a new directive "refcounting". The directive has no content
  10. and one required positional parameter:: "new" or "borrow".
  11. Example:
  12. .. cfunction:: json_t *json_object(void)
  13. .. refcounting:: new
  14. <description of the json_object function>
  15. :copyright: Copyright (c) 2009-2016 Petri Lehtinen <[email protected]>
  16. :license: MIT, see LICENSE for details.
  17. """
  18. from docutils import nodes
  19. from docutils.parsers.rst import Directive
  20. def visit(self, node):
  21. self.visit_emphasis(node)
  22. def depart(self, node):
  23. self.depart_emphasis(node)
  24. def html_visit(self, node):
  25. self.body.append(self.starttag(node, 'em', '', CLASS='refcount'))
  26. def html_depart(self, node):
  27. self.body.append('</em>')
  28. class refcounting(nodes.emphasis):
  29. pass
  30. class refcounting_directive(Directive):
  31. has_content = False
  32. required_arguments = 1
  33. optional_arguments = 0
  34. final_argument_whitespace = False
  35. def run(self):
  36. if self.arguments[0] == 'borrow':
  37. text = 'Return value: Borrowed reference.'
  38. elif self.arguments[0] == 'new':
  39. text = 'Return value: New reference.'
  40. else:
  41. raise Error('Valid arguments: new, borrow')
  42. return [refcounting(text, text)]
  43. def setup(app):
  44. app.add_node(refcounting,
  45. html=(html_visit, html_depart),
  46. latex=(visit, depart),
  47. text=(visit, depart),
  48. man=(visit, depart))
  49. app.add_directive('refcounting', refcounting_directive)