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

ENH: adding sphinx sidebars functionality #248

Merged
merged 6 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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": ["sidebar-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:

- ``sidebar-nav-bs.html`` - a bootstrap-friendly navigation section
- ``sidebar-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 @@ -199,4 +199,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just wondering about this one: is there some documentation in sphinx that this is the recommended way to add custom templates as a package?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, well it's documented in a way that suggests you could add to it to increase the search scope for templates: https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-templates_path

and it seems this is how Sphinx adds templates in its own codebase, e.g.: https://github.com/sphinx-doc/sphinx/blob/cc25bac26445688c50efb9b8d5f30571940e27bd/sphinx/ext/autosummary/generate.py#L644

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's how they do it for the built-in extensions, that's certainly fine!


return {"parallel_read_safe": True, "parallel_write_safe": True}
32 changes: 32 additions & 0 deletions pydata_sphinx_theme/_templates/sidebar-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/sidebar-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 = sidebar-search-bs.html, sidebar-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": ["sidebar-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