Skip to content

Commit bdf8224

Browse files
authored
ENH: adding sphinx sidebars functionality (#248)
1 parent 15819a8 commit bdf8224

File tree

11 files changed

+66
-44
lines changed

11 files changed

+66
-44
lines changed

docs/_templates/custom-template.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div class="sidebar-message">
2+
This is a community-supported theme.
3+
If you'd like to contribute,
4+
<a href="https://github.com/pandas-dev/pydata-sphinx-theme">check out our GitHub repository</a>
5+
Your contributions are welcome!
6+
</div>

docs/conf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
# This pattern also affects html_static_path and html_extra_path.
5050
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
5151

52+
html_sidebars = {
53+
"contributing": ["sidebar-search-bs.html", "custom-template.html"],
54+
}
5255

5356
# -- Options for HTML output -------------------------------------------------
5457

docs/user_guide/configuring.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ an external site. You can add external links to the nav bar like so:
4848
]
4949
}
5050
51+
Configure the sidebar
52+
=====================
53+
54+
``pydata_sphinx_theme`` provides two new sidebar items by default:
55+
56+
- ``sidebar-nav-bs.html`` - a bootstrap-friendly navigation section
57+
- ``sidebar-search-bs.html`` - a bootstrap-friendly search bar
58+
59+
By default, this theme's sidebar has these two elements in it. If you'd like to
60+
override this behavior and control the sidebar on a per-page basis, use the
61+
`Sphinx html-sidebars configuration value <https://www.sphinx-doc.org/en/master/usage/configuration.html?highlight=html_sidebars#confval-html_sidebars>`_.
5162

5263
Hiding the previous and next buttons
5364
====================================

pydata_sphinx_theme/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,9 @@ def setup(app):
199199
app.connect("html-page-context", setup_edit_url)
200200
app.connect("html-page-context", add_toctree_functions)
201201

202+
# Update templates for sidebar
203+
pkgdir = os.path.abspath(os.path.dirname(__file__))
204+
path_templates = os.path.join(pkgdir, "_templates")
205+
app.config.templates_path.append(path_templates)
206+
202207
return {"parallel_read_safe": True, "parallel_write_safe": True}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<nav class="bd-links" id="bd-docs-nav" aria-label="Main navigation">
2+
3+
<div class="bd-toc-item active">
4+
{% set nav = get_nav_object(maxdepth=3, collapse=True) %}
5+
6+
<ul class="nav bd-sidenav">
7+
{% for main_nav_item in nav %}
8+
{% if main_nav_item.active %}
9+
{% for nav_item in main_nav_item.children %}
10+
{% if nav_item.children %}
11+
12+
<li class="{% if nav_item.active%}active{% endif %}">
13+
<a href="{{ nav_item.url }}">{{ nav_item.title }}</a>
14+
<ul>
15+
{% for nav_item in nav_item.children %}
16+
<li class="{% if nav_item.active%}active{% endif %}">
17+
<a href="{{ nav_item.url }}">{{ nav_item.title }}</a>
18+
</li>
19+
{% endfor %}
20+
</ul>
21+
</li>
22+
{% else %}
23+
<li class="{% if nav_item.active%}active{% endif %}">
24+
<a href="{{ nav_item.url }}">{{ nav_item.title }}</a>
25+
</li>
26+
{% endif %}
27+
{% endfor %}
28+
{% endif %}
29+
{% endfor %}
30+
</ul>
31+
32+
</nav>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% if theme_search_bar_position == "sidebar" %}
2+
{%- include "search-field.html" %}
3+
{% endif %}

pydata_sphinx_theme/docs-sidebar.html

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +0,0 @@
1-
2-
3-
{% if theme_search_bar_position == "sidebar" %}
4-
{%- include "search-field.html" %}
5-
{% endif %}
6-
7-
<nav class="bd-links" id="bd-docs-nav" aria-label="Main navigation">
8-
9-
<div class="bd-toc-item active">
10-
{% set nav = get_nav_object(maxdepth=3, collapse=True) %}
11-
12-
<ul class="nav bd-sidenav">
13-
{% for main_nav_item in nav %}
14-
{% if main_nav_item.active %}
15-
{% for nav_item in main_nav_item.children %}
16-
{% if nav_item.children %}
17-
18-
<li class="{% if nav_item.active%}active{% endif %}">
19-
<a href="{{ nav_item.url }}">{{ nav_item.title }}</a>
20-
<ul>
21-
{% for nav_item in nav_item.children %}
22-
<li class="{% if nav_item.active%}active{% endif %}">
23-
<a href="{{ nav_item.url }}">{{ nav_item.title }}</a>
24-
</li>
25-
{% endfor %}
26-
</ul>
27-
</li>
28-
{% else %}
29-
<li class="{% if nav_item.active%}active{% endif %}">
30-
<a href="{{ nav_item.url }}">{{ nav_item.title }}</a>
31-
</li>
32-
{% endif %}
33-
{% endfor %}
34-
{% endif %}
35-
{% endfor %}
36-
</ul>
37-
38-
</nav>

pydata_sphinx_theme/layout.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
<div class="row">
4747
{% block docs_sidebar %}
4848
<div class="col-12 col-md-3 bd-sidebar">
49-
{%- include "docs-sidebar.html" %}
49+
{%- for sidebartemplate in sidebars %}
50+
{%- include sidebartemplate %}
51+
{%- endfor %}
5052
</div>
5153
{% endblock %}
5254

pydata_sphinx_theme/theme.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[theme]
22
inherit = basic
33
pygments_style = tango
4+
sidebars = sidebar-search-bs.html, sidebar-nav-bs.html
45

56
[options]
67
sidebarwidth = 270

tests/sites/base/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@
1818

1919
# Base options, we can add other key/vals later
2020
html_theme_options = {}
21+
22+
html_sidebars = {"section1/index": ["sidebar-nav-bs.html"]}

tests/test_build/sidebar_subpage.html

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
<div class="col-12 col-md-3 bd-sidebar">
2-
<form action="../search.html" class="bd-search d-flex align-items-center" method="get">
3-
<i class="icon fas fa-search">
4-
</i>
5-
<input aria-label="Search the docs ..." autocomplete="off" class="form-control" id="search-input" name="q" placeholder="Search the docs ..." type="search"/>
6-
</form>
72
<nav aria-label="Main navigation" class="bd-links" id="bd-docs-nav">
83
<div class="bd-toc-item active">
94
<ul class="nav bd-sidenav">

0 commit comments

Comments
 (0)