Skip to content

Commit aacaac3

Browse files
choldgrafdrammock
andauthored
Fix extra whitespace in sidebars (#1115)
* Fix extra whitespace in sidebars * Searchbox * Update src/pydata_sphinx_theme/__init__.py Co-authored-by: Daniel McCloy <[email protected]> * make test pass * Fix template filter to remove empty files * ABlog in template test * Move clear search button to primary sidebar * Move search clear button to top of article Co-authored-by: Daniel McCloy <[email protected]>
1 parent 30a0b34 commit aacaac3

File tree

13 files changed

+113
-54
lines changed

13 files changed

+113
-54
lines changed

docs/_static/contributors.yaml

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
- header: "@bollwyvl"
22
image: https://avatars.githubusercontent.com/u/45380
3-
link: https://github.com/bollwyvl
3+
website: https://github.com/bollwyvl
44
- header: "@jarrodmillman"
55
image: https://avatars.githubusercontent.com/u/123428
6-
link: https://github.com/jarrodmillman
6+
website: https://github.com/jarrodmillman
77
- header: "@hoetmaaiers"
88
image: https://avatars.githubusercontent.com/u/468045
9-
link: https://github.com/hoetmaaiers
9+
website: https://github.com/hoetmaaiers
1010
- header: "@jorisvandenbossche"
1111
image: https://avatars.githubusercontent.com/u/1020496
12-
link: https://github.com/jorisvandenbossche
12+
website: https://github.com/jorisvandenbossche
1313
- header: "@damianavila"
1414
image: https://avatars.githubusercontent.com/u/1640669
15-
link: https://github.com/damianavila
15+
website: https://github.com/damianavila
1616
- header: "@drammock"
1717
image: https://avatars.githubusercontent.com/u/1810515
18-
link: https://github.com/drammock
18+
website: https://github.com/drammock
1919
- header: "@choldgraf"
2020
image: https://avatars.githubusercontent.com/u/1839645
21-
link: https://github.com/choldgraf
21+
website: https://github.com/choldgraf
2222
- header: "@12rambau"
2323
image: https://avatars.githubusercontent.com/u/12596392
24-
link: https://github.com/12rambau
24+
website: https://github.com/12rambau

docs/scripts/generate_collaborators_gallery.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
{
2323
"header": f"@{collaborator['login']}",
2424
"image": f"https://avatars.githubusercontent.com/u/{collaborator['id']}",
25-
"link": collaborator["html_url"],
25+
"website": collaborator["html_url"],
2626
}
2727
)
2828

docs/user_guide/source-buttons.rst

+4-7
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,10 @@ any other context values.
8383
View Source link
8484
================
8585

86-
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).
87-
To do so, add the following extension to your documentation:
86+
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).
87+
To disable it, use the following configuration:
88+
8889

8990
.. code-block:: python
9091
91-
extensions = [
92-
...
93-
"sphinx.ext.viewcode",
94-
...
95-
]
92+
html_show_sourcelink = False

noxfile.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""Automatically build our documentation or run tests.
2+
3+
Environments are re-used by default. Use the following-pattern to re-install them.
4+
5+
nox -s docs -- -r
6+
"""
17
import nox
28
from pathlib import Path
39

@@ -29,6 +35,7 @@ def _should_install(session):
2935

3036
@nox.session
3137
def compile(session):
38+
"""Compile the theme's web assets with sphinx-theme-builder."""
3239
if _should_install(session):
3340
session.install("-e", ".")
3441
session.install("sphinx-theme-builder[cli]")
@@ -37,13 +44,15 @@ def compile(session):
3744

3845
@nox.session
3946
def docs(session):
47+
"""Build the documentation and place in docs/_build/html."""
4048
if _should_install(session):
4149
session.install("-e", ".[doc]")
4250
session.run("sphinx-build", "-b=html", "docs/", "docs/_build/html")
4351

4452

4553
@nox.session(name="docs-live")
4654
def docs_live(session):
55+
"""Build the docs with a live server that re-loads as you make changes."""
4756
if _should_install(session):
4857
session.install("-e", ".[doc]")
4958
session.install("sphinx-theme-builder[cli]")
@@ -52,17 +61,15 @@ def docs_live(session):
5261

5362
@nox.session(name="test")
5463
def test(session):
64+
"""Run the test suite. Use `-- -r` to re-build the environment."""
5565
if _should_install(session):
5666
session.install("-e", ".[test]")
5767
session.run("pytest", *session.posargs)
5868

5969

6070
@nox.session(name="profile")
6171
def profile(session):
62-
"""Generate a profile chart with py-spy.
63-
64-
The chart will be placed at profile.svg and can be viewed in the browser.
65-
"""
72+
"""Generate a profile chart with py-spy. The chart will be placed at profile.svg."""
6673
import shutil as sh
6774
import tempfile
6875
from textwrap import dedent

src/pydata_sphinx_theme/__init__.py

+22-5
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def prepare_html_config(app, pagename, templatename, context, doctree):
192192
context["theme_version"] = __version__
193193

194194

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

221+
# If this is the page TOC, check if it is empty and remove it if so
222+
def _remove_empty_templates(tname):
223+
# These templates take too long to render, so skip them.
224+
# They should never be empty anyway.
225+
SKIP_EMPTY_TEMPLATE_CHECKS = ["sidebar-nav-bs.html", "navbar-nav.html"]
226+
if not any(tname.endswith(temp) for temp in SKIP_EMPTY_TEMPLATE_CHECKS):
227+
# Render the template and see if it is totally empty
228+
rendered = app.builder.templates.render(tname, context)
229+
if len(rendered.strip()) == 0:
230+
return False
231+
return True
232+
233+
context[section] = list(filter(_remove_empty_templates, context[section]))
234+
221235
# Remove a duplicate entry of the theme CSS. This is because it is in both:
222236
# - theme.conf
223237
# - manually linked in `webpack-macros.html`
@@ -902,9 +916,12 @@ def _overwrite_pygments_css(app, exception=None):
902916
# see if user specified a light/dark pygments theme, if not, use the
903917
# one we set in theme.conf
904918
style_key = f"pygment_{light_or_dark}_style"
905-
theme_name = app.config.html_theme_options.get(
906-
style_key, app.builder.globalcontext.get(f"theme_{style_key}")
907-
)
919+
920+
# globalcontext sometimes doesn't exist so this ensures we do not error
921+
theme_name = app.config.html_theme_options.get(style_key, None)
922+
if theme_name is None and hasattr(app.builder, "globalcontext"):
923+
theme_name = app.builder.globalcontext.get(f"theme_{style_key}")
924+
908925
# make sure we can load the style
909926
if theme_name not in pygments_styles:
910927
logger.warning(
@@ -1051,8 +1068,8 @@ def setup(app):
10511068
app.connect("builder-inited", update_config)
10521069
app.connect("html-page-context", setup_edit_url)
10531070
app.connect("html-page-context", add_toctree_functions)
1054-
app.connect("html-page-context", update_templates)
10551071
app.connect("html-page-context", prepare_html_config)
1072+
app.connect("html-page-context", update_and_remove_templates)
10561073
app.connect("build-finished", _overwrite_pygments_css)
10571074

10581075
# Include component templates
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* The 'Hide Search Matches' button.
3+
* This only shows up when a person lands on a page after clicking a search result.
4+
* Clicking it removes the highlighting of the search term from the page.
5+
* We want it to behave like a button.
6+
*/
7+
div#searchbox {
8+
// Leave `#searchbox` rules empty so that it doesn't show at all when it is empty
9+
p.highlight-link {
10+
margin: 1rem 0;
11+
width: fit-content;
12+
13+
// A bit more margin on wide screens to mimic article behavior
14+
@include media-breakpoint-up($breakpoint-sidebar-secondary) {
15+
margin-left: 2rem;
16+
}
17+
18+
// Put outer shadow on this one so that we can darken the link w/ an inner shadow
19+
@include box-shadow();
20+
21+
// Style the button to look like a Sphinx Design button
22+
a {
23+
border-radius: 0.25rem;
24+
font-size: 1.25rem;
25+
padding: 0.75rem;
26+
background-color: var(--pst-color-primary);
27+
// Button text is always white with Sphinx Design buttons
28+
color: white;
29+
30+
// The box shadow is inset so that it darkens the button on hover
31+
transition: box-shadow 0.25s ease-out;
32+
&:hover {
33+
box-shadow: inset 0px 0px 50px 50px rgba(0, 0, 0, 0.25);
34+
35+
// Remove underline for link
36+
text-decoration: none;
37+
}
38+
39+
// add icon via CSS because the link is created by javascript
40+
// match padding to .toc-item > i above
41+
&:before {
42+
content: var(--pst-icon-search-minus);
43+
color: unset;
44+
font-family: "Font Awesome 6 Free";
45+
font-weight: 900;
46+
margin-right: 0.5rem;
47+
}
48+
}
49+
}
50+
}

src/pydata_sphinx_theme/assets/styles/pydata-sphinx-theme.scss

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
@import "./components/navbar-links";
3838
@import "./components/prev-next";
3939
@import "./components/search";
40+
@import "./components/searchbox";
4041
@import "./components/switcher-theme";
4142
@import "./components/switcher-version";
4243
@import "./components/toc-inpage";

src/pydata_sphinx_theme/assets/styles/sections/_sidebar-secondary.scss

-20
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,3 @@
5353
padding-left: 1rem;
5454
}
5555
}
56-
57-
// The 'Hide Search Matches' link
58-
div#searchbox {
59-
p.highlight-link {
60-
// remove excess margin from p tag
61-
margin-bottom: 0px;
62-
a {
63-
// add icon via CSS because the link is created by javascript
64-
// match padding to .toc-item > i above
65-
&:before {
66-
content: var(--pst-icon-search-minus);
67-
color: unset; // make sure it uses the same color as the text
68-
font-family: "Font Awesome 6 Free";
69-
font-weight: 900;
70-
padding-right: 0.5rem;
71-
margin-right: 0;
72-
}
73-
}
74-
}
75-
}

src/pydata_sphinx_theme/theme/pydata_sphinx_theme/layout.html

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
{% if sidebar_nav_html | length == 0 %}
1414
{% set sidebars = sidebars | reject("in", "sidebar-nav-bs.html") | list %}
1515
{% endif %}
16-
{# Remove the page-toc from secondary sidebar if there are no links to show #}
17-
{% if generate_toc_html() | length == 0 %}
18-
{% set theme_secondary_sidebar_items = theme_secondary_sidebar_items | reject("in", "page-toc.html") | list %}
19-
{% endif %}
2016
{# A flag for whether we include a secondary sidebar based on the page metadata #}
2117
{% set remove_sidebar_secondary = (meta is defined and meta is not none
2218
and 'html_theme.sidebar_secondary.remove' in meta)
@@ -93,6 +89,8 @@
9389
<div class="bd-header-article">{% include "sections/header-article.html" %}</div>
9490
{# Article content #}
9591
{% block docs_body %}
92+
{# This is empty and only shows up if text has been highlighted by the URL #}
93+
{% include "components/searchbox.html" %}
9694
<article class="bd-article" role="main">
9795
{% block body %}{% endblock %}
9896
</article>

src/pydata_sphinx_theme/theme/pydata_sphinx_theme/theme.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ navbar_persistent = search-button.html
3535
header_links_before_dropdown = 5
3636
primary_sidebar_end = sidebar-ethical-ads.html
3737
footer_items = copyright.html, theme-version.html, sphinx-version.html
38-
secondary_sidebar_items = page-toc.html, searchbox.html, edit-this-page.html, sourcelink.html
38+
secondary_sidebar_items = page-toc.html, edit-this-page.html, sourcelink.html
3939
switcher =
4040
check_switcher = True
4141
pygment_light_style = a11y-high-contrast-light

tests/sites/deprecated/conf.py

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"logo_text": "DOCS",
2525
"page_sidebar_items": [
2626
"page-toc.html",
27-
"searchbox.html",
2827
"edit-this-page.html",
2928
"sourcelink.html",
3029
],

tests/test_build.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ def test_edit_page_url(sphinx_build_factory, html_context, edit_url):
541541
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides)
542542

543543
if edit_url is None:
544-
with pytest.raises(sphinx.errors.ThemeError):
544+
with pytest.raises(sphinx.errors.ExtensionError):
545545
sphinx_build.build()
546546
return
547547

@@ -765,6 +765,8 @@ def test_deprecated_build_html(sphinx_build_factory, file_regression):
765765
file_regression.check(navbar.prettify(), basename="navbar_ix", extension=".html")
766766

767767
# Sidebar subpage
768+
# This re-uses the same HTML template used above (w/o deprecated config)
769+
# because they should still be the same.
768770
sidebar = subpage_html.select(".bd-sidebar")[0]
769771
file_regression.check(
770772
sidebar.prettify(), basename="sidebar_subpage", extension=".html"
@@ -783,3 +785,13 @@ def test_ablog(sphinx_build_factory):
783785
confoverrides = {"extensions": ["ablog"]}
784786
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
785787
assert sphinx_build.app.config.fontawesome_included is True
788+
789+
790+
def test_empty_templates(sphinx_build_factory):
791+
"""If a template is empty (e.g., via a config), it should be removed."""
792+
# When configured to be gone, the template should be removed w/ its parent.
793+
# ABlog needs to be added so we can test that template rendering works w/ it.
794+
confoverrides = {"html_show_sourcelink": False, "extensions": ["ablog"]}
795+
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
796+
toc_items = sphinx_build.html_tree("page1.html").select(".toc-item")
797+
assert not any(ii.select(".tocsection.sourcelink") for ii in toc_items)

tests/test_build/sidebar_subpage.html

-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@
8181
</div>
8282
</div>
8383
<div class="sidebar-end-items sidebar-primary__section">
84-
<div class="sidebar-end-items__item">
85-
</div>
8684
</div>
8785
<div id="rtd-footer-container">
8886
</div>

0 commit comments

Comments
 (0)