21
if sphinx.__version__ < '1.0.1':
22
raise RuntimeError("Sphinx 1.0.1 or newer is required")
19
24
import os, re, pydoc
20
25
from docscrape_sphinx import get_doc_object, SphinxDocString
26
from sphinx.util.compat import Directive
23
29
def mangle_docstrings(app, what, name, obj, options, lines,
24
30
reference_offset=[0]):
32
cfg = dict(use_plots=app.config.numpydoc_use_plots,
33
show_class_members=app.config.numpydoc_show_class_members)
26
35
if what == 'module':
28
37
title_re = re.compile(ur'^\s*[#*=]{4,}\n[a-z0-9 -]+\n[#*=]{4,}\s*',
30
39
lines[:] = title_re.sub(u'', u"\n".join(lines)).split(u"\n")
32
doc = get_doc_object(obj, what, u"\n".join(lines))
33
doc.use_plots = app.config.numpydoc_use_plots
41
doc = get_doc_object(obj, what, u"\n".join(lines), config=cfg)
34
42
lines[:] = unicode(doc).split(u"\n")
36
44
if app.config.numpydoc_edit_link and hasattr(obj, '__name__') and \
81
90
sig = re.sub(u"^[^(]*", u"", doc['Signature'])
86
app.connect('autodoc-process-signature', mangle_signature)
88
monkeypatch_sphinx_ext_autodoc()
90
93
def setup(app, get_doc_object_=get_doc_object):
91
94
global get_doc_object
92
95
get_doc_object = get_doc_object_
94
97
app.connect('autodoc-process-docstring', mangle_docstrings)
95
app.connect('builder-inited', initialize)
96
app.add_config_value('numpydoc_edit_link', None, True)
98
app.connect('autodoc-process-signature', mangle_signature)
99
app.add_config_value('numpydoc_edit_link', None, False)
97
100
app.add_config_value('numpydoc_use_plots', None, False)
99
#------------------------------------------------------------------------------
100
# Monkeypatch sphinx.ext.autodoc to accept argspecless autodocs (Sphinx < 0.5)
101
#------------------------------------------------------------------------------
103
def monkeypatch_sphinx_ext_autodoc():
104
global _original_format_signature
105
import sphinx.ext.autodoc
107
if sphinx.ext.autodoc.format_signature is our_format_signature:
110
print "[numpydoc] Monkeypatching sphinx.ext.autodoc ..."
111
_original_format_signature = sphinx.ext.autodoc.format_signature
112
sphinx.ext.autodoc.format_signature = our_format_signature
114
def our_format_signature(what, obj):
115
r = mangle_signature(None, what, None, obj, None, None, None)
119
return _original_format_signature(what, obj)
101
app.add_config_value('numpydoc_show_class_members', True, True)
103
# Extra mangling domains
104
app.add_domain(NumpyPythonDomain)
105
app.add_domain(NumpyCDomain)
107
#------------------------------------------------------------------------------
108
# Docstring-mangling domains
109
#------------------------------------------------------------------------------
111
from docutils.statemachine import ViewList
112
from sphinx.domains.c import CDomain
113
from sphinx.domains.python import PythonDomain
115
class ManglingDomainBase(object):
116
directive_mangling_map = {}
118
def __init__(self, *a, **kw):
119
super(ManglingDomainBase, self).__init__(*a, **kw)
120
self.wrap_mangling_directives()
122
def wrap_mangling_directives(self):
123
for name, objtype in self.directive_mangling_map.items():
124
self.directives[name] = wrap_mangling_directive(
125
self.directives[name], objtype)
127
class NumpyPythonDomain(ManglingDomainBase, PythonDomain):
129
directive_mangling_map = {
130
'function': 'function',
132
'exception': 'class',
133
'method': 'function',
134
'classmethod': 'function',
135
'staticmethod': 'function',
136
'attribute': 'attribute',
139
class NumpyCDomain(ManglingDomainBase, CDomain):
141
directive_mangling_map = {
142
'function': 'function',
143
'member': 'attribute',
149
def wrap_mangling_directive(base_directive, objtype):
150
class directive(base_directive):
152
env = self.state.document.settings.env
156
m = re.match(r'^(.*\s+)?(.*?)(\(.*)?', self.arguments[0])
157
name = m.group(2).strip()
160
name = self.arguments[0]
162
lines = list(self.content)
163
mangle_docstrings(env.app, objtype, name, None, None, lines)
164
self.content = ViewList(lines, self.content.parent)
166
return base_directive.run(self)