Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix extra whitespace in sidebars #1115

Merged
merged 8 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions docs/user_guide/source-buttons.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,10 @@ any other context values.
View Source link
================

You can add a button that will direct users to view the source of a page (i.e., the underlying ``reStructuredText`` or ``MyST Markdown`` for the page).
To do so, add the following extension to your documentation:
By default, this theme adds a button link to view the source of a page (i.e., the underlying ``reStructuredText`` or ``MyST Markdown`` for the page).
To disable it, use the following configuration:


.. code-block:: python

extensions = [
...
"sphinx.ext.viewcode",
...
]
html_show_sourcelink = False
40 changes: 35 additions & 5 deletions src/pydata_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def prepare_html_config(app, pagename, templatename, context, doctree):
context["theme_version"] = __version__


def update_templates(app, pagename, templatename, context, doctree):
def update_and_remove_templates(app, pagename, templatename, context, doctree):
"""Update template names and assets for page build."""
# Allow for more flexibility in template names
template_sections = [
Expand All @@ -218,6 +218,30 @@ def update_templates(app, pagename, templatename, context, doctree):
if not os.path.splitext(template)[1]:
context[section][ii] = template + ".html"

# Remove templates if we know they've been disabled
def _filter_template_list(item):
# In case of `html_show_sourcelink = False`
if item.endswith("sourcelink.html"):
if context.get("show_source", True) is False:
return False
# In case of manually specifying False
elif item.endswith("edit-this-page.html"):
if context.get("theme_use_edit_page_button", True) is False:
return False
# If no conditions are hit then we keep the template
return True

context[section] = list(filter(_filter_template_list, context.get(section)))

# If this is the page TOC, check if it is empty and remove it if so
def _remove_empty_pagetoc(item):
if item.endswith("page-toc.html"):
if len(context["generate_toc_html"]()) == 0:
return False
return True

context[section] = list(filter(_remove_empty_pagetoc, context[section]))

# Remove a duplicate entry of the theme CSS. This is because it is in both:
# - theme.conf
# - manually linked in `webpack-macros.html`
Expand Down Expand Up @@ -902,9 +926,15 @@ def _overwrite_pygments_css(app, exception=None):
# see if user specified a light/dark pygments theme, if not, use the
# one we set in theme.conf
style_key = f"pygment_{light_or_dark}_style"
theme_name = app.config.html_theme_options.get(
style_key, app.builder.globalcontext.get(f"theme_{style_key}")
)

# globalcontext sometimes doesn't exist so this ensures we do not error
if hasattr(app.builder, "globalcontext"):
theme_name = app.config.html_theme_options.get(
style_key, app.builder.globalcontext.get(f"theme_{style_key}")
)
else:
theme_name = fallback
choldgraf marked this conversation as resolved.
Show resolved Hide resolved

# make sure we can load the style
if theme_name not in pygments_styles:
logger.warning(
Expand Down Expand Up @@ -1051,7 +1081,7 @@ def setup(app):
app.connect("builder-inited", update_config)
app.connect("html-page-context", setup_edit_url)
app.connect("html-page-context", add_toctree_functions)
app.connect("html-page-context", update_templates)
app.connect("html-page-context", update_and_remove_templates)
app.connect("html-page-context", prepare_html_config)
app.connect("build-finished", _overwrite_pygments_css)

Expand Down
6 changes: 2 additions & 4 deletions src/pydata_sphinx_theme/theme/pydata_sphinx_theme/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
{% if sidebar_nav_html | length == 0 %}
{% set sidebars = sidebars | reject("in", "sidebar-nav-bs.html") | list %}
{% endif %}
{# Remove the page-toc from secondary sidebar if there are no links to show #}
{% if generate_toc_html() | length == 0 %}
{% set theme_secondary_sidebar_items = theme_secondary_sidebar_items | reject("in", "page-toc.html") | list %}
{% endif %}
{# A flag for whether we include a secondary sidebar based on the page metadata #}
{% set remove_sidebar_secondary = (meta is defined and meta is not none
and 'html_theme.sidebar_secondary.remove' in meta)
Expand Down Expand Up @@ -93,6 +89,8 @@
<div class="bd-header-article">{% include "sections/header-article.html" %}</div>
{# Article content #}
{% block docs_body %}
{# This is empty and only shows up if text has been highlighted by the URL #}
{% include "components/searchbox.html" %}
<article class="bd-article" role="main">
{% block body %}{% endblock %}
</article>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ navbar_persistent = search-button.html
header_links_before_dropdown = 5
primary_sidebar_end = sidebar-ethical-ads.html
footer_items = copyright.html, theme-version.html, sphinx-version.html
secondary_sidebar_items = page-toc.html, searchbox.html, edit-this-page.html, sourcelink.html
secondary_sidebar_items = page-toc.html, edit-this-page.html, sourcelink.html
switcher =
check_switcher = True
pygment_light_style = a11y-high-contrast-light
Expand Down
1 change: 0 additions & 1 deletion tests/sites/deprecated/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"logo_text": "DOCS",
"page_sidebar_items": [
"page-toc.html",
"searchbox.html",
"edit-this-page.html",
"sourcelink.html",
],
Expand Down