Skip to content

Commit d86a81c

Browse files
authored
Merge pull request #20 from piyushk/piyushk/autodoc_disable_nested
Allow disabling nesting of autosummary tables
2 parents 04ae4a2 + 46998d4 commit d86a81c

File tree

7 files changed

+81
-17
lines changed

7 files changed

+81
-17
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ env:
1616
- SPHINX_VERSION=1.8.*
1717
- SPHINX_VERSION=2.0.*
1818
- SPHINX_VERSION=2.1.*
19+
- SPHINX_VERSION=2.2.*
1920
- SPHINX_VERSION=""
2021
matrix:
2122
exclude:
2223
- python: "2.7"
2324
env: SPHINX_VERSION=2.0.*
2425
- python: "2.7"
2526
env: SPHINX_VERSION=2.1.*
27+
- python: "2.7"
28+
env: SPHINX_VERSION=2.2.*
2629
- python: "3.7"
2730
env: SPHINX_VERSION=1.3.*
2831
- python: "3.7"

autodocsumm/__init__.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
#: Options of the :class:`sphinx.ext.autodoc.ModuleDocumenter` that have an
6363
#: effect on the selection of members for the documentation
6464
member_options = {
65-
'members', 'undoc-members', 'inherited-members', 'exlude-members',
65+
'members', 'undoc-members', 'inherited-members', 'exclude-members',
6666
'private-members', 'special-members', 'imported-members',
6767
'ignore-module-all'}
6868

@@ -208,6 +208,7 @@ class AutoSummModuleDocumenter(ModuleDocumenter, AutosummaryDocumenter):
208208
#: but with additional autosummary boolean option
209209
option_spec = ModuleDocumenter.option_spec.copy()
210210
option_spec['autosummary'] = bool_option
211+
option_spec['autosummary-no-nesting'] = bool_option
211212

212213
#: Add options for members for the autosummary
213214
for _option in member_options.intersection(option_spec):
@@ -244,6 +245,7 @@ class AutoSummClassDocumenter(ClassDocumenter, AutosummaryDocumenter):
244245
#: but with additional autosummary boolean option
245246
option_spec = ClassDocumenter.option_spec.copy()
246247
option_spec['autosummary'] = bool_option
248+
option_spec['autosummary-no-nesting'] = bool_option
247249

248250
#: Add options for members for the autosummary
249251
for _option in member_options.intersection(option_spec):
@@ -399,10 +401,10 @@ class AutoSummDirective(AutodocDirective, Autosummary):
399401

400402
if sphinx_version < [1, 7]:
401403
_default_flags = AutodocDirective._default_flags.union(
402-
{'autosummary'} | set(map('autosummary-{}'.format, member_options))
404+
{'autosummary', 'autosummary-no-nesting'} | set(map('autosummary-{}'.format, member_options))
403405
)
404406
else:
405-
AUTODOC_DEFAULT_OPTIONS.append('autosummary')
407+
AUTODOC_DEFAULT_OPTIONS.extend(['autosummary', 'autosummary-no-nesting'])
406408
AUTODOC_DEFAULT_OPTIONS.extend(
407409
map('autosummary-{}'.format, member_options))
408410

@@ -463,7 +465,8 @@ def run(self):
463465
self.result = ViewList()
464466
documenter = self.autosummary_documenter
465467
grouped_documenters = documenter.get_grouped_documenters()
466-
summ_nodes = self.autosumm_nodes(documenter, grouped_documenters)
468+
nested = 'autosummary-no-nesting' not in self.options
469+
summ_nodes = self.autosumm_nodes(documenter, grouped_documenters, nested)
467470

468471
dn = summ_nodes.pop(documenter.fullname)
469472
if self.name == 'automodule':
@@ -559,7 +562,7 @@ def inject_summary(node):
559562
inject_summary(node)
560563
return doc_nodes
561564

562-
def autosumm_nodes(self, documenter, grouped_documenters):
565+
def autosumm_nodes(self, documenter, grouped_documenters, nested):
563566
"""Create the autosummary nodes based on the documenter content
564567
565568
Parameters
@@ -570,6 +573,8 @@ def autosumm_nodes(self, documenter, grouped_documenters):
570573
grouped_documenters: dict
571574
The dictionary as it is returned from the
572575
:meth:`AutosummaryDocumenter.get_grouped_documenters` method
576+
nested: bool
577+
If true, autosummary tables will also be created for members.
573578
574579
Returns
575580
-------
@@ -596,13 +601,14 @@ def autosumm_nodes(self, documenter, grouped_documenters):
596601
ViewList(['**%s**' % section]), 0, node)
597602
this_nodes += node
598603
this_nodes += self.get_table(items)
599-
for mdocumenter, check_module in documenters:
600-
if (mdocumenter.objtype == 'class' and
601-
not (check_module and not mdocumenter.check_module())):
602-
if hasattr(mdocumenter, 'get_grouped_documenters'):
603-
summ_nodes.update(self.autosumm_nodes(
604-
mdocumenter, mdocumenter.get_grouped_documenters())
605-
)
604+
if nested:
605+
for mdocumenter, check_module in documenters:
606+
if (mdocumenter.objtype == 'class' and
607+
not (check_module and not mdocumenter.check_module())):
608+
if hasattr(mdocumenter, 'get_grouped_documenters'):
609+
summ_nodes.update(self.autosumm_nodes(
610+
mdocumenter, mdocumenter.get_grouped_documenters(), nested)
611+
)
606612
summ_nodes[documenter.fullname] = this_nodes
607613
return summ_nodes
608614

@@ -781,12 +787,12 @@ def setup(app):
781787
CallableAttributeDocumenter, NoDataDataDocumenter,
782788
NoDataAttributeDocumenter]:
783789
if not issubclass(registry.get(cls.objtype), cls):
784-
try:
785-
# we use add_documenter because this does not add a new
786-
# directive
787-
app.add_documenter(cls)
788-
except AttributeError:
790+
if sphinx_version >= [2, 2]:
791+
app.add_autodocumenter(cls, override=True)
792+
elif sphinx_version >= [0, 6]:
789793
app.add_autodocumenter(cls)
794+
else:
795+
app.add_documenter(cls)
790796

791797
# directives
792798
if sphinx_version >= [1, 8]:

docs/conf_settings.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,16 @@ table: these are
3838
- ``autosummary-imported-members``
3939
- ``autosummary-ignore-module-all``
4040
- ``autosummary-members``
41+
- ``autosummary-no-nesting``
4142

4243
They are essentially the same as the options for :mod:`~sphinx.ext.autodoc`
4344
(i.e. ``private-members`` or ``members``), but they only affect the
4445
autosummary table (see the example in :ref:`summary-table-example`).
4546

47+
The new additional flag ``autosummary-no-nesting`` only generates
48+
autosummary tables for a module, but not for members within. See
49+
:ref:`no-nesting-example`.
50+
4651

4752
.. _confvals:
4853

docs/examples.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,27 @@ which gives us
114114
:no-members:
115115
:autosummary:
116116
:autosummary-members:
117+
118+
.. _no-nesting-example:
119+
120+
Generating a summary table for the module without nesting
121+
---------------------------------------------------------
122+
123+
Using the ``autosummary-no-nesting`` option, you can generate the autosummary
124+
table for a module without generating autosummary tables for members within
125+
that module. This is useful when you only want to use the autosummary table as
126+
a table of contents for a given page. For the :doc:`demo module <demo_module>`,
127+
here's an example::
128+
129+
.. automodule:: dummy
130+
:autosummary:
131+
:members:
132+
:autosummary-no-nesting:
133+
134+
which gives us
135+
136+
.. automodule:: dummy
137+
:noindex:
138+
:members:
139+
:autosummary:
140+
:autosummary-no-nesting:

tests/sphinx_supp/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ Example documentation
99
test_class_summary_only
1010
test_inherited
1111
test_module_title
12+
test_module_no_nesting
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Docs of dummy test module without nesting
2+
=========================================
3+
4+
.. automodule:: dummy
5+
:autosummary-no-nesting:

tests/test_autodocsumm.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ def test_module(self, app, status, warning):
5050
self.assertNotIn('Should also be skipped', html)
5151
self.assertIn('Should also be included', html)
5252

53+
@with_app(buildername='html', srcdir=sphinx_supp,
54+
copy_srcdir_to_tmpdir=True)
55+
def test_module_no_nesting(self, app, status, warning):
56+
app.build()
57+
html = get_html(app, 'test_module_no_nesting.html')
58+
59+
self.assertIn('<span class="pre">TestClass</span>', html)
60+
self.assertIn('<span class="pre">test_func</span>', html)
61+
62+
# test whether the data is shown correctly
63+
self.assertIn('<span class="pre">large_data</span>', html)
64+
self.assertIn('<span class="pre">small_data</span>', html)
65+
66+
# test that elements of TestClass are not autosummarized, since nesting is disabled.
67+
self.assertNotIn('<span class="pre">test_method</span>', html)
68+
self.assertNotIn('<span class="pre">test_attr</span>', html)
69+
70+
# test the members are still displayed
71+
self.assertIn('<dt id="dummy.Class_CallTest">', html)
72+
5373
@with_app(buildername='html', srcdir=sphinx_supp,
5474
copy_srcdir_to_tmpdir=True)
5575
def test_module_summary_only(self, app, status, warning):

0 commit comments

Comments
 (0)