File: directives.py

package info (click to toggle)
sphinx-autoapi 3.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 900 kB
  • sloc: python: 5,146; makefile: 7
file content (67 lines) | stat: -rw-r--r-- 2,069 bytes parent folder | download
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
60
61
62
63
64
65
66
67
"""AutoAPI directives"""

from docutils.parsers.rst import Directive
from docutils import nodes

from sphinx.ext.autosummary import Autosummary, mangle_signature
from sphinx.util.nodes import nested_parse_with_titles

from ._objects import PythonFunction


class AutoapiSummary(Autosummary):
    """A version of autosummary that uses static analysis."""

    def get_items(self, names):
        items = []
        env = self.state.document.settings.env
        all_objects = env.autoapi_all_objects
        max_item_chars = 60

        for name in names:
            obj = all_objects[name]
            if isinstance(obj, PythonFunction):
                if obj.overloads:
                    sig = "(\u2026)"
                else:
                    sig = f"({obj.args})"
                    if obj.return_annotation is not None:
                        sig += f" \u2192 {obj.return_annotation}"
            else:
                sig = ""

            if sig:
                max_sig_chars = max(10, max_item_chars - len(obj.short_name))
                sig = mangle_signature(sig, max_chars=max_sig_chars)

            item = (obj.short_name, sig, obj.summary, obj.id)
            items.append(item)

        return items


class NestedParse(Directive):
    """Nested parsing to remove the first heading of included rST

    This is used to handle the case where we like to remove user supplied
    headings from module docstrings. This is required to reduce the number of
    duplicate headings on sections.
    """

    has_content = True
    required_arguments = 0
    optional_arguments = 0
    final_argument_whitespace = False

    def run(self):
        node = nodes.container()
        node.document = self.state.document
        nested_parse_with_titles(self.state, self.content, node)
        try:
            if isinstance(node[0], nodes.section) and isinstance(
                node[0][0], nodes.title
            ):
                node.children = node[0][1:] + node.children[1:]
        except IndexError:
            pass
        return node.children