Skip to content

Commit

Permalink
Add dropdown_text argument to generate_header_nav_html (pydata#1423)
Browse files Browse the repository at this point in the history
* Add dropdown_text argument to generate_header_nav_html

* Add a test, fix typo in theme.conf and remove header_dropdown_text from docs/conf.py

* fixed?

---------

Co-authored-by: Daniel McCloy <dan@mccloy.info>
  • Loading branch information
2 people authored and gabalafou committed Sep 19, 2023
1 parent 0125510 commit 402788f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
Expand Up @@ -12,8 +12,6 @@
{{ _("Site Navigation") }}
</p>
<ul class="bd-navbar-elements navbar-nav">
{{ generate_header_nav_html(
dropdown_id,
n_links_before_dropdown=theme_header_links_before_dropdown) }}
{{ generate_header_nav_html(n_links_before_dropdown=theme_header_links_before_dropdown, dropdown_text=theme_header_dropdown_text) }}
</ul>
</nav>
Expand Up @@ -30,6 +30,7 @@ show_nav_level = 1
show_toc_level = 1
navbar_align = content
header_links_before_dropdown = 5
header_dropdown_text = More
switcher =
check_switcher = True
pygment_light_style = a11y-high-contrast-light
Expand Down
6 changes: 4 additions & 2 deletions src/pydata_sphinx_theme/toctree.py
Expand Up @@ -13,6 +13,7 @@
from sphinx.addnodes import toctree as toctree_node
from sphinx.application import Sphinx
from sphinx.environment.adapters.toctree import TocTree
from sphinx.locale import _

from .utils import traverse_or_findall

Expand Down Expand Up @@ -54,7 +55,7 @@ def create_next_id(base_id: str):

@lru_cache(maxsize=None)
def generate_header_nav_html(
dropdown_id: str, n_links_before_dropdown: int = 5
n_links_before_dropdown: int = 5, dropdown_text: str = "More"
) -> str:
"""Generate top-level links that are meant for the header navigation.
Expand All @@ -72,6 +73,7 @@ def generate_header_nav_html(
Parameters:
n_links_before_dropdown:The number of links to show before nesting the remaining links in a Dropdown element.
dropdown_text:Text of the dropdown element button.
"""
try:
n_links_before_dropdown = int(n_links_before_dropdown)
Expand Down Expand Up @@ -168,7 +170,7 @@ def generate_header_nav_html(
out += f"""
<li class="nav-item dropdown">
<button class="btn dropdown-toggle nav-item" type="button" data-bs-toggle="dropdown" aria-expanded="false" aria-controls="{dropdown_id}">
More
{_(dropdown_text)}
</button>
<ul id="{dropdown_id}" class="dropdown-menu">
{links_dropdown_html}
Expand Down
28 changes: 28 additions & 0 deletions tests/test_build.py
Expand Up @@ -333,6 +333,34 @@ def test_navbar_header_dropdown(sphinx_build_factory, n_links) -> None:
assert standalone_links and not dropdowns


@pytest.mark.parametrize("dropdown_text", (None, "Other")) # None -> default "More"
def test_navbar_header_dropdown_button(sphinx_build_factory, dropdown_text) -> None:
"""Test whether dropdown button text is configurable."""
if dropdown_text:
confoverrides = {
"html_theme_options": {
"header_links_before_dropdown": 2,
"header_dropdown_text": dropdown_text,
}
}
else:
confoverrides = {
"html_theme_options": {
"header_links_before_dropdown": 2,
}
}
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
index_html = sphinx_build.html_tree("index.html")
navbar = index_html.select("ul.bd-navbar-elements")[0]
button_text = str(navbar.select("button")[0])
if dropdown_text:
# The dropdown text should be "Other"
assert dropdown_text in button_text
else:
# The dropdown text should be "More"
assert "More" in button_text


def test_sidebars_captions(sphinx_build_factory, file_regression) -> None:
"""Test that the captions are rendered."""
sphinx_build = sphinx_build_factory("sidebars").build()
Expand Down

0 comments on commit 402788f

Please sign in to comment.