This repository has been archived by the owner on Jul 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
sphinx_local_hooks.py
65 lines (49 loc) · 1.95 KB
/
sphinx_local_hooks.py
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
#-*- coding: utf-8 -*-
import itertools as it, operator as op, functools as ft
from collections import Iterable
import types
from sphinx.ext.autodoc import Documenter
_autodoc_add_line = Documenter.add_line
@ft.wraps(_autodoc_add_line)
def autodoc_add_line(self, line, *argz, **kwz):
tee = self.env.app.config.autodoc_dump_rst
if tee:
tee_line = self.indent + line
if isinstance(tee, file): tee.write(tee_line + '\n')
elif tee is True: print tee_line
else:
raise ValueError( 'Unrecognized'
' value for "autodoc_dump_rst" option: {!r}'.format(tee) )
return _autodoc_add_line(self, line, *argz, **kwz)
Documenter.add_line = autodoc_add_line
def process_docstring(app, what, name, obj, options, lines):
if not lines: return
i, ld = 0, dict(enumerate(lines)) # to allow arbitrary peeks
i_max = max(ld)
def process_line(i):
line, i_next = ld[i], i + 1
while i_next not in ld and i_next <= i_max: i_next += 1
line_next = ld.get(i_next)
if line_next and line_next[0] in u' \t': # tabbed continuation of the sentence
ld[i] = u'{} {}'.format(line, line_next.strip())
del ld[i_next]
process_line(i)
elif line.endswith(u'.') or (line_next and line_next[0].isupper()): ld[i+0.5] = u''
for i in xrange(i_max + 1):
if i not in ld: continue # was removed
process_line(i)
# Overwrite the list items inplace, extending the list if necessary
for i, (k, line) in enumerate(sorted(ld.viewitems())):
try: lines[i] = line
except IndexError: lines.append(line)
def skip_override(app, what, name, obj, skip, options):
if what == 'exception':
return False if name == '__init__'\
and isinstance(obj, types.UnboundMethodType) else True
elif what == 'class' and name in ['__init__', '__call__']\
and isinstance(obj, types.UnboundMethodType): return False
return skip
def setup(app):
app.connect('autodoc-process-docstring', process_docstring)
app.connect('autodoc-skip-member', skip_override)
app.add_config_value('autodoc_dump_rst', None, True)