Skip to content

Commit

Permalink
split into 2 funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed May 2, 2024
1 parent 3b6a066 commit faa8b74
Showing 1 changed file with 30 additions and 47 deletions.
77 changes: 30 additions & 47 deletions src/pydata_sphinx_theme/toctree.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,13 @@ def unique_html_id(base_id: str):
return next(get_or_create_id_generator(base_id))

@cache
def generate_header_nav_before_dropdown(
n_links_before_dropdown,
) -> Tuple[str, List[str]]:
"""Return html for navbar and dropdown.
Given the number of links before the dropdown, return the html for the navbar,
as well as the list of links to put in a dropdown.
def _generate_nav_info() -> List[LinkInfo]:
"""Generate informations necessary to generate nav.
Returns:
- HTML str for the navbar
- list of HTML str for the dropdown
Instead of messing with html later, having this as a util function
should make it slightly easier to generate different html snippet for
sidebar or navbar.
"""
try:
n_links_before_dropdown = int(n_links_before_dropdown)
except Exception:
raise ValueError(
f"n_links_before_dropdown is not an int: {n_links_before_dropdown}"
)
toctree = TocTree(app.env)

# Find the active header navigation item so we decide whether to highlight
Expand All @@ -156,7 +145,6 @@ def generate_header_nav_before_dropdown(
# just below the root of our site
root_toc = app.env.tocs[app.config.root_doc]

links_html = []
links_data = []

# Iterate through each node in the root document toc.
Expand All @@ -169,7 +157,6 @@ def generate_header_nav_before_dropdown(
page = toc.attributes["parent"] if page == "self" else page

# If this is the active ancestor page, add a class so we highlight it
current = "current active" if page == active_header_page else ""

# sanitize page title for use in the html output if needed
if title is None:
Expand All @@ -186,7 +173,6 @@ def generate_header_nav_before_dropdown(
# If it's an absolute one then we use the external class and
# the complete url.
is_absolute = bool(urlparse(page).netloc)
link_status = "nav-external" if is_absolute else "nav-internal"
link_href = page if is_absolute else context["pathto"](page)

links_data.append(
Expand All @@ -198,19 +184,6 @@ def generate_header_nav_before_dropdown(
)
)

# create the html output
links_html.append(
dedent(
f"""
<li class="nav-item pst-header-nav-item {current}">
<a class="nav-link {link_status}" href="{link_href}">
{title}
</a>
</li>
"""
)
)

# Add external links defined in configuration as sibling list items
for external_link in context["theme_external_links"]:
links_data.append(
Expand All @@ -221,20 +194,33 @@ def generate_header_nav_before_dropdown(
is_external=True,
)
)
links_html.append(
dedent(
f"""
<li class="nav-item pst-header-nav-item ">
<a class="nav-link nav-external" href="{ external_link["url"] }">
{ external_link["name"] }
</a>
</li>
"""
)

return links_data

@cache
def generate_header_nav_before_dropdown(
n_links_before_dropdown,
) -> Tuple[str, List[str]]:
"""Return html for navbar and dropdown.
Given the number of links before the dropdown, return the html for the navbar,
as well as the list of links to put in a dropdown.
Returns:
- HTML str for the navbar
- list of HTML str for the dropdown
"""
try:
n_links_before_dropdown = int(n_links_before_dropdown)
except Exception:
raise ValueError(
f"n_links_before_dropdown is not an int: {n_links_before_dropdown}"
)
lhtml = []
links_data = _generate_nav_info(n_links_before_dropdown)

links_html = []
for link in links_data:
lhtml.append(
links_html.append(
dedent(
f"""
<li class="nav-item pst-header-nav-item {"current active" if link.is_current else ""}">
Expand All @@ -245,9 +231,6 @@ def generate_header_nav_before_dropdown(
"""
)
)
for a, b, d in zip(links_html, lhtml, links_data):
assert a == b, (a, b, d)
assert links_html == lhtml, (links_html, lhtml)

# The first links will always be visible
links_solo = links_html[:n_links_before_dropdown]
Expand Down

0 comments on commit faa8b74

Please sign in to comment.