summaryrefslogtreecommitdiff
path: root/sphinx_ext.py
blob: ff06e70404d859e6085196600782ada248ad5d60 (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
60
61
62
63
64
65
66
67
68
69
70
from logilab.common.decorators import monkeypatch

from sphinx.ext import autodoc

class DocstringOnlyModuleDocumenter(autodoc.ModuleDocumenter):
    objtype = 'docstring'
    def format_signature(self):
        pass
    def add_directive_header(self, sig):
        pass
    def document_members(self, all_members=False):
        pass

    def resolve_name(self, modname, parents, path, base):
        if modname is not None:
            return modname, parents + [base]
        return (path or '') + base, []


#autodoc.add_documenter(DocstringOnlyModuleDocumenter)

def setup(app):
    app.add_autodocumenter(DocstringOnlyModuleDocumenter)



from sphinx.ext.autodoc import (ViewList, Options, AutodocReporter, nodes,
                                assemble_option_dict, nested_parse_with_titles)

@monkeypatch(autodoc.AutoDirective)
def run(self):
    self.filename_set = set()  # a set of dependent filenames
    self.reporter = self.state.document.reporter
    self.env = self.state.document.settings.env
    self.warnings = []
    self.result = ViewList()

    # find out what documenter to call
    objtype = self.name[4:]
    doc_class = self._registry[objtype]
    # process the options with the selected documenter's option_spec
    self.genopt = Options(assemble_option_dict(
        self.options.items(), doc_class.option_spec))
    # generate the output
    documenter = doc_class(self, self.arguments[0])
    documenter.generate(more_content=self.content)
    if not self.result:
        return self.warnings

    # record all filenames as dependencies -- this will at least
    # partially make automatic invalidation possible
    for fn in self.filename_set:
        self.env.note_dependency(fn)

    # use a custom reporter that correctly assigns lines to source
    # filename/description and lineno
    old_reporter = self.state.memo.reporter
    self.state.memo.reporter = AutodocReporter(self.result,
                                               self.state.memo.reporter)
    if self.name in ('automodule', 'autodocstring'):
        node = nodes.section()
        # necessary so that the child nodes get the right source/line set
        node.document = self.state.document
        nested_parse_with_titles(self.state, self.result, node)
    else:
        node = nodes.paragraph()
        node.document = self.state.document
        self.state.nested_parse(self.result, 0, node)
    self.state.memo.reporter = old_reporter
    return self.warnings + node.children