diff --git a/autodocsumm/__init__.py b/autodocsumm/__init__.py index f1cedbe..4788445 100755 --- a/autodocsumm/__init__.py +++ b/autodocsumm/__init__.py @@ -279,8 +279,15 @@ def get_grouped_documenters(self, all_members=False): self.options.update(options_save) return documenters - def add_autosummary(self): - """Add the autosammary table of this documenter.""" + def add_autosummary(self, relative_ref_paths=False): + """Add the autosammary table of this documenter. + + Parameters + ========== + relative_ref_paths: bool + Use paths relative to the current module instead of + absolute import paths for each object + """ if ( self.options.get("autosummary") and not self.options.get("no-autosummary") @@ -303,8 +310,14 @@ def add_autosummary(self): indent = ' ' for (documenter, _) in documenters: - self.add_line( - indent + '~' + documenter.fullname, sourcename) + obj_ref_path = documenter.fullname + if relative_ref_paths: + modname = self.modname + "." + if documenter.fullname.startswith(modname): + obj_ref_path = documenter.fullname[len(modname):] + + self.add_line(indent + '~' + obj_ref_path, sourcename) + self.add_line('', sourcename) @@ -351,7 +364,7 @@ class AutoSummModuleDocumenter(ModuleDocumenter, AutosummaryDocumenter): def add_content(self, *args, **kwargs): super().add_content(*args, **kwargs) - self.add_autosummary() + self.add_autosummary(relative_ref_paths=True) if self.options.get("autosummary-no-nesting"): self.options["no-autosummary"] = "True" @@ -400,7 +413,7 @@ class AutoSummClassDocumenter(ClassDocumenter, AutosummaryDocumenter): def add_content(self, *args, **kwargs): super().add_content(*args, **kwargs) - self.add_autosummary() + self.add_autosummary(relative_ref_paths=True) class CallableDataDocumenter(DataDocumenter): diff --git a/tests/test-root/dummy_submodule/__init__.py b/tests/test-root/dummy_submodule/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test-root/dummy_submodule/submodule1.py b/tests/test-root/dummy_submodule/submodule1.py new file mode 100644 index 0000000..29c2102 --- /dev/null +++ b/tests/test-root/dummy_submodule/submodule1.py @@ -0,0 +1,9 @@ +import dummy_submodule.submodule2 + + +class SubmoduleClass1: + """Docu for myclass 1""" + + def func1(self): + """Docu for func 1""" + pass diff --git a/tests/test-root/dummy_submodule/submodule2.py b/tests/test-root/dummy_submodule/submodule2.py new file mode 100644 index 0000000..5030b43 --- /dev/null +++ b/tests/test-root/dummy_submodule/submodule2.py @@ -0,0 +1,9 @@ +import dummy_submodule.submodule1 + + +class SubmoduleClass2: + """Docu for myclass 1""" + + def func2(self): + """Docu for func 1""" + pass diff --git a/tests/test-root/index.rst b/tests/test-root/index.rst index 8c63c8b..4f6b787 100644 --- a/tests/test-root/index.rst +++ b/tests/test-root/index.rst @@ -17,3 +17,5 @@ Example documentation test_automodulesumm test_automodulesumm_some_sections test_empty + test_class_submodule + test_module_submodule \ No newline at end of file diff --git a/tests/test-root/test_class_submodule.rst b/tests/test-root/test_class_submodule.rst new file mode 100644 index 0000000..e27c626 --- /dev/null +++ b/tests/test-root/test_class_submodule.rst @@ -0,0 +1,4 @@ +Test if links in summary are correctly generated +================================================ + +.. autoclass:: dummy_submodule.submodule1.SubmoduleClass1 diff --git a/tests/test-root/test_module_submodule.rst b/tests/test-root/test_module_submodule.rst new file mode 100644 index 0000000..25f598a --- /dev/null +++ b/tests/test-root/test_module_submodule.rst @@ -0,0 +1,4 @@ +Test if links in summary are correctly generated +================================================ + +.. automodule:: dummy_submodule.submodule2 diff --git a/tests/test_autodocsumm.py b/tests/test_autodocsumm.py index e7b2af5..f7f2636 100644 --- a/tests/test_autodocsumm.py +++ b/tests/test_autodocsumm.py @@ -212,10 +212,7 @@ def test_class(self, app): if sphinx_version[:2] > [3, 1]: assert in_autosummary("instance_attribute", html) elif sphinx_version[:2] < [3, 1]: - assert ( - 'dummy.TestClass.instance_attribute' - in html - ) + assert in_autosummary("TestClass.instance_attribute", html) assert in_autosummary("test_method", html) assert in_autosummary("test_attr", html) @@ -267,10 +264,7 @@ def test_class_order(self, app): if sphinx_version[:2] > [3, 1]: assert in_autosummary("instance_attribute", html) elif sphinx_version[:2] < [3, 1]: - assert ( - 'dummy.TestClass.instance_attribute' - in html - ) + assert in_autosummary("TestClass.instance_attribute", html) assert in_autosummary("test_attr", html) assert in_autosummary("large_data", html) @@ -287,10 +281,7 @@ def test_class_summary_only(self, app): if sphinx_version[:2] > [3, 1]: assert in_autosummary("instance_attribute", html) elif sphinx_version[:2] < [3, 1]: - assert ( - 'dummy.TestClass.instance_attribute' - in html - ) + assert in_autosummary("TestClass.instance_attribute", html) assert in_autosummary("test_method", html) assert in_autosummary("test_attr", html) @@ -314,10 +305,7 @@ def test_class_nosignatures(self, app): if sphinx_version[:2] > [3, 1]: assert in_autosummary("instance_attribute", html) elif sphinx_version[:2] < [3, 1]: - assert ( - 'dummy.TestClass.instance_attribute' - in html - ) + assert in_autosummary("TestClass.instance_attribute", html) assert in_autosummary("test_method", html) assert in_autosummary("test_attr", html) @@ -366,6 +354,28 @@ def test_autoclasssumm_inline(self, app): assert docstring_end > methods_start + def test_class_submodule(self, app): + app.build() + + html = get_html(app, '/test_class_submodule.html') + + # check that hyperlink for instance method exists in summary table + assert re.findall(r'