Skip to content

Commit

Permalink
adding sphinx sidebars functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
choldgraf committed Sep 11, 2020
1 parent 772ef6a commit cc552c0
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 44 deletions.
6 changes: 6 additions & 0 deletions docs/_templates/custom-template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="sidebar-message">
This is a community-supported theme.
If you'd like to contribute,
<a href="https://github.com/pandas-dev/pydata-sphinx-theme">check out our GitHub repository</a>
Your contributions are welcome!
</div>
3 changes: 3 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]

html_sidebars = {
"contributing": ["search-bs.html", "custom-template.html"],
}

# -- Options for HTML output -------------------------------------------------

Expand Down
11 changes: 11 additions & 0 deletions docs/user_guide/configuring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ an external site. You can add external links to the nav bar like so:
]
}
Configure the sidebar
=====================

``pydata_sphinx_theme`` provides two new sidebar items by default:

- ``nav-bs.html`` - a bootstrap-friendly navigation section
- ``search-bs.html`` - a bootstrap-friendly search bar

By default, this theme's sidebar has these two elements in it. If you'd like to
override this behavior and control the sidebar on a per-page basis, use the
`Sphinx html-sidebars configuration value <https://www.sphinx-doc.org/en/master/usage/configuration.html?highlight=html_sidebars#confval-html_sidebars>`_.

Hiding the previous and next buttons
====================================
Expand Down
5 changes: 5 additions & 0 deletions pydata_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,9 @@ def setup(app):
app.connect("html-page-context", setup_edit_url)
app.connect("html-page-context", add_toctree_functions)

# Update templates for sidebar
pkgdir = os.path.abspath(os.path.dirname(__file__))
path_templates = os.path.join(pkgdir, "_templates")
app.config.templates_path.append(path_templates)

return {"parallel_read_safe": True, "parallel_write_safe": True}
32 changes: 32 additions & 0 deletions pydata_sphinx_theme/_templates/nav-bs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<nav class="bd-links" id="bd-docs-nav" aria-label="Main navigation">

<div class="bd-toc-item active">
{% set nav = get_nav_object(maxdepth=3, collapse=True) %}

<ul class="nav bd-sidenav">
{% for main_nav_item in nav %}
{% if main_nav_item.active %}
{% for nav_item in main_nav_item.children %}
{% if nav_item.children %}

<li class="{% if nav_item.active%}active{% endif %}">
<a href="{{ nav_item.url }}">{{ nav_item.title }}</a>
<ul>
{% for nav_item in nav_item.children %}
<li class="{% if nav_item.active%}active{% endif %}">
<a href="{{ nav_item.url }}">{{ nav_item.title }}</a>
</li>
{% endfor %}
</ul>
</li>
{% else %}
<li class="{% if nav_item.active%}active{% endif %}">
<a href="{{ nav_item.url }}">{{ nav_item.title }}</a>
</li>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
</ul>

</nav>
3 changes: 3 additions & 0 deletions pydata_sphinx_theme/_templates/search-bs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% if theme_search_bar_position == "sidebar" %}
{%- include "search-field.html" %}
{% endif %}
38 changes: 0 additions & 38 deletions pydata_sphinx_theme/docs-sidebar.html
Original file line number Diff line number Diff line change
@@ -1,38 +0,0 @@


{% if theme_search_bar_position == "sidebar" %}
{%- include "search-field.html" %}
{% endif %}

<nav class="bd-links" id="bd-docs-nav" aria-label="Main navigation">

<div class="bd-toc-item active">
{% set nav = get_nav_object(maxdepth=3, collapse=True) %}

<ul class="nav bd-sidenav">
{% for main_nav_item in nav %}
{% if main_nav_item.active %}
{% for nav_item in main_nav_item.children %}
{% if nav_item.children %}

<li class="{% if nav_item.active%}active{% endif %}">
<a href="{{ nav_item.url }}">{{ nav_item.title }}</a>
<ul>
{% for nav_item in nav_item.children %}
<li class="{% if nav_item.active%}active{% endif %}">
<a href="{{ nav_item.url }}">{{ nav_item.title }}</a>
</li>
{% endfor %}
</ul>
</li>
{% else %}
<li class="{% if nav_item.active%}active{% endif %}">
<a href="{{ nav_item.url }}">{{ nav_item.title }}</a>
</li>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
</ul>

</nav>
4 changes: 3 additions & 1 deletion pydata_sphinx_theme/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
<div class="row">
{% block docs_sidebar %}
<div class="col-12 col-md-3 bd-sidebar">
{%- include "docs-sidebar.html" %}
{%- for sidebartemplate in sidebars %}
{%- include sidebartemplate %}
{%- endfor %}
</div>
{% endblock %}

Expand Down
1 change: 1 addition & 0 deletions pydata_sphinx_theme/theme.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[theme]
inherit = basic
pygments_style = tango
sidebars = search-bs.html, nav-bs.html

[options]
sidebarwidth = 270
Expand Down
2 changes: 2 additions & 0 deletions tests/sites/base/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@

# Base options, we can add other key/vals later
html_theme_options = {}

html_sidebars = {"section1/index": ["nav-bs.html"]}
5 changes: 0 additions & 5 deletions tests/test_build/sidebar_subpage.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
<div class="col-12 col-md-3 bd-sidebar">
<form action="../search.html" class="bd-search d-flex align-items-center" method="get">
<i class="icon fas fa-search">
</i>
<input aria-label="Search the docs ..." autocomplete="off" class="form-control" id="search-input" name="q" placeholder="Search the docs ..." type="search"/>
</form>
<nav aria-label="Main navigation" class="bd-links" id="bd-docs-nav">
<div class="bd-toc-item active">
<ul class="nav bd-sidenav">
Expand Down

0 comments on commit cc552c0

Please sign in to comment.