refcounting.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 <petri@digip.org>
  16. :license: MIT, see LICENSE for details.
  17. """
  18. from docutils import nodes
  19. class refcounting(nodes.emphasis): pass
  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. def refcounting_directive(name, arguments, options, content, lineno,
  29. content_offset, block_text, state, state_machine):
  30. if arguments[0] == 'borrow':
  31. text = 'Return value: Borrowed reference.'
  32. elif arguments[0] == 'new':
  33. text = 'Return value: New reference.'
  34. else:
  35. raise Error('Valid arguments: new, borrow')
  36. return [refcounting(text, text)]
  37. def setup(app):
  38. app.add_node(refcounting,
  39. html=(html_visit, html_depart),
  40. latex=(visit, depart),
  41. text=(visit, depart),
  42. man=(visit, depart))
  43. app.add_directive('refcounting', refcounting_directive, 0, (1, 0, 0))