From faa8b74bfbcf44324e007d5b3cf96566782f3ca0 Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Thu, 2 May 2024 11:09:58 +0200 Subject: [PATCH] split into 2 funcs --- src/pydata_sphinx_theme/toctree.py | 77 ++++++++++++------------------ 1 file changed, 30 insertions(+), 47 deletions(-) diff --git a/src/pydata_sphinx_theme/toctree.py b/src/pydata_sphinx_theme/toctree.py index f20e80cd8..73bf73778 100644 --- a/src/pydata_sphinx_theme/toctree.py +++ b/src/pydata_sphinx_theme/toctree.py @@ -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 @@ -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. @@ -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: @@ -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( @@ -198,19 +184,6 @@ def generate_header_nav_before_dropdown( ) ) - # create the html output - links_html.append( - dedent( - f""" - - """ - ) - ) - # Add external links defined in configuration as sibling list items for external_link in context["theme_external_links"]: links_data.append( @@ -221,20 +194,33 @@ def generate_header_nav_before_dropdown( is_external=True, ) ) - links_html.append( - dedent( - f""" - - """ - ) + + 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"""