aboutsummaryrefslogtreecommitdiff
path: root/doc/ext/refcounting.py
blob: 506b2ee6c10f49e5656424bf27affa5a2d516e59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
    refcounting
    ~~~~~~~~~~~

    Reference count annotations for C API functions. Has the same
    result as the sphinx.ext.refcounting extension but works for all
    functions regardless of the signature, and the reference counting
    information is written inline with the documentation instead of a
    separate file.

    Adds a new directive "refcounting". The directive has no content
    and one required positional parameter:: "new" or "borrow".

    Example:

    .. cfunction:: json_t *json_object(void)

       .. refcounting:: new

       <description of the json_object function>

    :copyright: Copyright 2009 Petri Lehtinen <petri@digip.org>
    :license: MIT, see LICENSE for details.
"""

from docutils import nodes

class refcounting(nodes.emphasis): pass

def visit(self, node):
    self.visit_emphasis(node)

def depart(self, node):
    self.depart_emphasis(node)

def html_visit(self, node):
    self.body.append(self.starttag(node, 'em', '', CLASS='refcount'))

def html_depart(self, node):
    self.body.append('</em>')


def refcounting_directive(name, arguments, options, content, lineno,
                   content_offset, block_text, state, state_machine):
    if arguments[0] == 'borrow':
        text = 'Return value: Borrowed reference.'
    elif arguments[0] == 'new':
        text = 'Return value: New reference.'
    else:
        raise Error('Valid arguments: new, borrow')

    return [refcounting(text, text)]

def setup(app):
    app.add_node(refcounting,
                 html=(html_visit, html_depart),
                 latex=(visit, depart),
                 text=(visit, depart))
    app.add_directive('refcounting', refcounting_directive, 0, (1, 0, 0))