Skip to content

Commit 979a51a

Browse files
authored
Merge pull request #70 from theOehrly/fix-no-links-in-summary
Fix: no links generated in summary table
2 parents 073d513 + 93a7473 commit 979a51a

File tree

8 files changed

+73
-22
lines changed

8 files changed

+73
-22
lines changed

autodocsumm/__init__.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,15 @@ def get_grouped_documenters(self, all_members=False):
279279
self.options.update(options_save)
280280
return documenters
281281

282-
def add_autosummary(self):
283-
"""Add the autosammary table of this documenter."""
282+
def add_autosummary(self, relative_ref_paths=False):
283+
"""Add the autosammary table of this documenter.
284+
285+
Parameters
286+
==========
287+
relative_ref_paths: bool
288+
Use paths relative to the current module instead of
289+
absolute import paths for each object
290+
"""
284291
if (
285292
self.options.get("autosummary")
286293
and not self.options.get("no-autosummary")
@@ -303,8 +310,14 @@ def add_autosummary(self):
303310
indent = ' '
304311

305312
for (documenter, _) in documenters:
306-
self.add_line(
307-
indent + '~' + documenter.fullname, sourcename)
313+
obj_ref_path = documenter.fullname
314+
if relative_ref_paths:
315+
modname = self.modname + "."
316+
if documenter.fullname.startswith(modname):
317+
obj_ref_path = documenter.fullname[len(modname):]
318+
319+
self.add_line(indent + '~' + obj_ref_path, sourcename)
320+
308321
self.add_line('', sourcename)
309322

310323

@@ -351,7 +364,7 @@ class AutoSummModuleDocumenter(ModuleDocumenter, AutosummaryDocumenter):
351364
def add_content(self, *args, **kwargs):
352365
super().add_content(*args, **kwargs)
353366

354-
self.add_autosummary()
367+
self.add_autosummary(relative_ref_paths=True)
355368

356369
if self.options.get("autosummary-no-nesting"):
357370
self.options["no-autosummary"] = "True"
@@ -400,7 +413,7 @@ class AutoSummClassDocumenter(ClassDocumenter, AutosummaryDocumenter):
400413
def add_content(self, *args, **kwargs):
401414
super().add_content(*args, **kwargs)
402415

403-
self.add_autosummary()
416+
self.add_autosummary(relative_ref_paths=True)
404417

405418

406419
class CallableDataDocumenter(DataDocumenter):

tests/test-root/dummy_submodule/__init__.py

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import dummy_submodule.submodule2
2+
3+
4+
class SubmoduleClass1:
5+
"""Docu for myclass 1"""
6+
7+
def func1(self):
8+
"""Docu for func 1"""
9+
pass
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import dummy_submodule.submodule1
2+
3+
4+
class SubmoduleClass2:
5+
"""Docu for myclass 1"""
6+
7+
def func2(self):
8+
"""Docu for func 1"""
9+
pass

tests/test-root/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ Example documentation
1717
test_automodulesumm
1818
test_automodulesumm_some_sections
1919
test_empty
20+
test_class_submodule
21+
test_module_submodule
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Test if links in summary are correctly generated
2+
================================================
3+
4+
.. autoclass:: dummy_submodule.submodule1.SubmoduleClass1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Test if links in summary are correctly generated
2+
================================================
3+
4+
.. automodule:: dummy_submodule.submodule2

tests/test_autodocsumm.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,7 @@ def test_class(self, app):
212212
if sphinx_version[:2] > [3, 1]:
213213
assert in_autosummary("instance_attribute", html)
214214
elif sphinx_version[:2] < [3, 1]:
215-
assert (
216-
'<span class="pre">dummy.TestClass.instance_attribute</span>'
217-
in html
218-
)
215+
assert in_autosummary("TestClass.instance_attribute", html)
219216

220217
assert in_autosummary("test_method", html)
221218
assert in_autosummary("test_attr", html)
@@ -267,10 +264,7 @@ def test_class_order(self, app):
267264
if sphinx_version[:2] > [3, 1]:
268265
assert in_autosummary("instance_attribute", html)
269266
elif sphinx_version[:2] < [3, 1]:
270-
assert (
271-
'<span class="pre">dummy.TestClass.instance_attribute</span>'
272-
in html
273-
)
267+
assert in_autosummary("TestClass.instance_attribute", html)
274268

275269
assert in_autosummary("test_attr", html)
276270
assert in_autosummary("large_data", html)
@@ -287,10 +281,7 @@ def test_class_summary_only(self, app):
287281
if sphinx_version[:2] > [3, 1]:
288282
assert in_autosummary("instance_attribute", html)
289283
elif sphinx_version[:2] < [3, 1]:
290-
assert (
291-
'<span class="pre">dummy.TestClass.instance_attribute</span>'
292-
in html
293-
)
284+
assert in_autosummary("TestClass.instance_attribute", html)
294285

295286
assert in_autosummary("test_method", html)
296287
assert in_autosummary("test_attr", html)
@@ -314,10 +305,7 @@ def test_class_nosignatures(self, app):
314305
if sphinx_version[:2] > [3, 1]:
315306
assert in_autosummary("instance_attribute", html)
316307
elif sphinx_version[:2] < [3, 1]:
317-
assert (
318-
'<span class="pre">dummy.TestClass.instance_attribute</span>'
319-
in html
320-
)
308+
assert in_autosummary("TestClass.instance_attribute", html)
321309

322310
assert in_autosummary("test_method", html)
323311
assert in_autosummary("test_attr", html)
@@ -366,6 +354,28 @@ def test_autoclasssumm_inline(self, app):
366354

367355
assert docstring_end > methods_start
368356

357+
def test_class_submodule(self, app):
358+
app.build()
359+
360+
html = get_html(app, '/test_class_submodule.html')
361+
362+
# check that hyperlink for instance method exists in summary table
363+
assert re.findall(r'<td>.*href="#dummy_submodule\.submodule1'
364+
r'\.SubmoduleClass1\.func1".*</td>', html)
365+
366+
def test_module_submodule(self, app):
367+
app.build()
368+
369+
html = get_html(app, '/test_module_submodule.html')
370+
371+
# check that hyperlink for class exists in summary table
372+
assert re.findall(r'<td>.*href="#dummy_submodule\.submodule2'
373+
r'\.SubmoduleClass2".*</td>', html)
374+
375+
# check that hyperlink for instance method exists in summary table
376+
assert re.findall(r'<td>.*href="#dummy_submodule\.submodule2'
377+
r'\.SubmoduleClass2\.func2".*</td>', html)
378+
369379

370380
class TestAutoDocSummDirective:
371381
"""Test case for the :class:`autodocsumm.AutoDocSummDirective`."""

0 commit comments

Comments
 (0)