diff --git a/src/devdocs2zim/client.py b/src/devdocs2zim/client.py index 44e0085..d7924c5 100644 --- a/src/devdocs2zim/client.py +++ b/src/devdocs2zim/client.py @@ -148,8 +148,15 @@ def count(self) -> int: def _contained_pages(self) -> set[str]: return {link.path_without_fragment for link in self.links} - def contains_page(self, page_path: str) -> bool: - """Returns whether this section contains the given page.""" + def opens_for_page(self, page_path: str) -> bool: + """Returns whether this section should be rendered open for the given page.""" + + # Some docs like Lua or CoffeeScript have all of their content in the index. + # Others like RequireJS are split between index and additional pages. + # We don't want sections opening when the user navigates to the index. + if page_path == "index": + return False + return page_path in self._contained_pages diff --git a/src/devdocs2zim/templates/page.html b/src/devdocs2zim/templates/page.html index 887f7c7..72c0bba 100644 --- a/src/devdocs2zim/templates/page.html +++ b/src/devdocs2zim/templates/page.html @@ -29,7 +29,7 @@ {{devdocs_metadata.name}}
{% for section in nav_sections %} -
+
{{ section.count | safe}} diff --git a/tests/test_client.py b/tests/test_client.py index 88f6625..857b5ae 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -280,7 +280,7 @@ def test_count_non_empty(self): self.assertEqual(1, got) - def test_contains_page(self): + def test_opens_for_page(self): section = NavigationSection( name="", links=[ @@ -290,9 +290,20 @@ def test_contains_page(self): ], ) - self.assertTrue(section.contains_page("foo")) - self.assertTrue(section.contains_page("bar")) - self.assertFalse(section.contains_page("bazz")) + self.assertTrue(section.opens_for_page("foo")) + self.assertTrue(section.opens_for_page("bar")) + self.assertFalse(section.opens_for_page("bazz")) + + def test_opens_for_page_index(self): + section = NavigationSection( + name="", + links=[ + DevdocsIndexEntry(name="Index", path="index", type=None), + ], + ) + + # Links to the index are special cases and shouldn't open. + self.assertFalse(section.opens_for_page("index")) class TestDevdocsIndex(TestCase):