Skip to content

Commit f1e4d91

Browse files
Making navbar + a few other sections more modular (#355)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent 3532922 commit f1e4d91

28 files changed

+388
-111
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v{{ version }}

docs/conf.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
copyright = "2019, PyData Community"
2222
author = "PyData Community"
2323

24-
# The full version, including alpha/beta/rc tags
25-
release = "0.0.1dev0"
24+
import pydata_sphinx_theme
2625

26+
version = pydata_sphinx_theme.__version__.replace("dev0", "")
2727

2828
# -- General configuration ---------------------------------------------------
2929

@@ -50,7 +50,7 @@
5050
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
5151

5252
html_sidebars = {
53-
"contributing": ["sidebar-search-bs.html", "custom-template.html"],
53+
"contributing": ["search-field", "custom-template"],
5454
"changelog": [],
5555
}
5656

@@ -77,9 +77,15 @@
7777
],
7878
"use_edit_page_button": True,
7979
"show_toc_level": 1,
80-
# "navbar_align": "right", # For testing that the navbar items align properly
80+
# "search_bar_position": "navbar", # TODO: Deprecated - remove in future version
81+
# "navbar_align": "left", # [left, content, right] For testing that the navbar items align properly
82+
# "navbar_start": ["navbar-logo", "navbar-version"],
83+
# "navbar_center": ["navbar-nav", "navbar-version"], # Just for testing
84+
# "navbar_end": ["navbar-icon-links", "navbar-version"] # Just for testing
85+
# "footer_items": ["copyright", "sphinx-version", ""]
8186
}
8287

88+
8389
html_context = {
8490
"github_user": "pandas-dev",
8591
"github_repo": "pydata-sphinx-theme",

docs/user_guide/configuring.rst

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ Configure the sidebar
172172
``pydata_sphinx_theme`` provides two new sidebar items by default:
173173

174174
- ``sidebar-nav-bs.html`` - a bootstrap-friendly navigation section
175-
- ``sidebar-search-bs.html`` - a bootstrap-friendly search bar
175+
- ``search-field.html`` - a bootstrap-friendly search bar
176176

177177
By default, this theme's sidebar has these two elements in it. If you'd like to
178178
override this behavior and control the sidebar on a per-page basis, use the
@@ -229,18 +229,34 @@ and you do not need to specify it if you wish to use the default.
229229
Configure the search bar position
230230
=================================
231231

232-
To modify the position of the search bar, change the following variable in
233-
your configuration file ``conf.py``. Possible options are 'navbar' and 'sidebar'.
232+
To modify the position of the search bar, add the ``search-field.html``
233+
template to your **sidebar**, or to one of the **navbar** positions, depending
234+
on where you want it to be placed.
234235

235-
By default the search bar is positioned in the sidebar since this is more
236-
suitable for large navigation bars.
236+
For example, if you'd like the search field to be in your side-bar, add it to
237+
the sidebar templates like so:
237238

238239
.. code:: python
239240
240-
html_theme_options = {
241-
"search_bar_position": "navbar"
241+
html_sidebars = {
242+
"**": ["search-field.html", "sidebar-nav-bs.html", "sidebar-ethical-ads.html"]
242243
}
243244
245+
If instead you'd like to put the search bar in the top navbar, use the
246+
following configuration:
247+
248+
.. code:: python
249+
250+
html_theme_options = {
251+
"navbar_end": ["navbar-icon-links.html", "search-field.html"]
252+
}
253+
254+
255+
.. note::
256+
257+
By default the search bar is positioned in the sidebar since this is more
258+
suitable for large navigation bars.
259+
244260
Configure the search bar text
245261
=============================
246262

@@ -332,10 +348,10 @@ use this pattern:
332348
For information about configuring the sidebar's contents, see :ref:`configure-sidebar`.
333349

334350

335-
Configure navbar menu item alignment
336-
====================================
351+
Configure the navbar center alignment
352+
=====================================
337353

338-
By default, the navigation bar menu items will align with the content on your
354+
By default, the navigation bar center area will align with the content on your
339355
page. This equals the following default configuration:
340356

341357
.. code-block:: python
@@ -366,3 +382,22 @@ If you'd like these items to snap to the right of the page, use this configurati
366382
"navbar_align": "right"
367383
...
368384
}
385+
386+
387+
Adding ethical advertisements to your sidebar in ReadTheDocs
388+
============================================================
389+
390+
If you're hosting your documentation on ReadTheDocs, you should consider
391+
adding an explicit placement for their **ethical advertisements**. These are
392+
non-tracking advertisements from ethical companies, and they help ReadTheDocs
393+
sustain themselves and their free service.
394+
395+
Ethical advertisements are added to your sidebar by default. To ensure they are
396+
there if you manually update your sidebar, ensure that the ``sidebar-ethical-ads.html``
397+
template is added to your list. For example:
398+
399+
.. code:: python
400+
401+
html_sidebars = {
402+
"**": ["search-field.html", "sidebar-nav-bs.html", "sidebar-ethical-ads.html"]
403+
}

docs/user_guide/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ User Guide
88

99
install
1010
configuring
11+
sections
1112
customizing

docs/user_guide/sections.rst

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
====================================
2+
Add/Remove items from theme sections
3+
====================================
4+
5+
There are a few major theme sections that you can customize to add/remove
6+
components, or add your own components. Each section is configured with a
7+
list of *html templates* - these are snippets of HTML that are inserted into
8+
the section by Sphinx.
9+
10+
You can choose which templates show up in each section, as well as the order in
11+
which they appear. This page describes the major areas that you can customize.
12+
13+
.. note::
14+
15+
When configuring templates in each section, you may omit the ``.html``
16+
suffix after each template if you wish.
17+
18+
The navbar items
19+
================
20+
21+
The navbar is at the top of the page, and is broken up into three sections.
22+
Each section is configured in ``conf.py`` with the following configuration:
23+
24+
- Left section: ``html_theme_options['navbar_start']``
25+
- Middle menu: ``html_theme_options['navbar_center']``
26+
- Right section: ``html_theme_options['navbar_end']``
27+
28+
By default, the following configuration is used:
29+
30+
.. code-block:: python
31+
32+
html_theme_options = {
33+
...
34+
"navbar_start": ["navbar-logo"],
35+
"navbar_center": ["navbar-nav"],
36+
"navbar_end": ["navbar-icon-links"]
37+
...
38+
}
39+
40+
The left sidebar
41+
================
42+
43+
The left sidebar is just to the left of a page's main content.
44+
Configuring it is a bit different from configuring the other sections, because
45+
configuring the sidebar is natively supported in Sphinx, via the ``html_sidebars``
46+
configuration variable.
47+
48+
For the left sidebar only, you can configure templates so that they only show
49+
up on certain pages. You do so via a configuration like so in ``conf.py``:
50+
51+
.. code-block:: python
52+
53+
html_sidebars = {
54+
"<page_pattern>": ["list", "of", "templates"]
55+
}
56+
57+
Any pages that match ``<page_pattern>`` will have their respective templates
58+
inserted. You can also ``*`` to do ``glob``-style matching, and may use ``**``
59+
to match all pages.
60+
61+
By default, it has the following configuration:
62+
63+
.. code-block:: python
64+
65+
html_sidebars = {
66+
"**": ["search-field", "sidebar-nav-bs", "sidebar-ethical-ads"]
67+
}
68+
69+
70+
The right in-page sidebar
71+
=========================
72+
73+
The in-page sidebar is just to the right of a page's main content, and is
74+
configured in ``conf.py`` with ``html_theme_options['page_sidebar_items']``.
75+
76+
By default, it has the following templates:
77+
78+
.. code-block:: python
79+
80+
html_theme_options = {
81+
...
82+
"page_sidebar_items": ["page-toc", "edit-this-page"],
83+
...
84+
}
85+
86+
The footer
87+
==========
88+
89+
The footer is just below a page's main content, and is configured in ``conf.py``
90+
with ``html_theme_options['footer_items']``.
91+
92+
By default, it has the following templates:
93+
94+
.. code-block:: python
95+
96+
html_theme_options = {
97+
...
98+
"footer_items": ["copyright", "sphinx-version"],
99+
...
100+
}
101+
102+
A list of built-in templates you can insert into sections
103+
=========================================================
104+
105+
Below is a list of build-in templates that you can insert into any section.
106+
Note that some of them may have CSS rules that assume a specific section (and
107+
will be named accordingly).
108+
109+
- ``icon-links.html``
110+
- ``search-field.html``
111+
- ``copyright.html``
112+
- ``edit-this-page.html``
113+
- ``last-updated.html``
114+
- ``navbar-icon-links.html``
115+
- ``navbar-logo.html``
116+
- ``navbar-nav.html``
117+
- ``page-toc.html``
118+
- ``sidebar-ethical-ads.html``
119+
- ``sidebar-nav-bs.html``
120+
- ``sphinx-version.html``
121+
122+
Add your own HTML templates to theme sections
123+
=============================================
124+
125+
If you'd like to add your own custom template to any of these sections, you
126+
could do so with the following steps:
127+
128+
1. Create an HTML file in a folder called ``_templates``. For example, if
129+
you wanted to display the version of your documentation using a Jinja
130+
template, you could create a file: ``_templates/version.html`` and put the
131+
following in it:
132+
133+
.. code-block:: html
134+
135+
<!-- This will display the version of the docs -->
136+
{{ version }}
137+
138+
1. Now add the file to your menu items for one of the sections above. For example:
139+
140+
.. code-block:: python
141+
142+
html_theme_options = {
143+
...
144+
"navbar_start": ["navbar-logo", "version"],
145+
...
146+
}

pydata_sphinx_theme/__init__.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55

66
from sphinx.errors import ExtensionError
7+
from sphinx.util import logging
78
from sphinx.environment.adapters.toctree import TocTree
89
from sphinx import addnodes
910

@@ -13,6 +14,44 @@
1314

1415
__version__ = "0.5.3dev0"
1516

17+
logger = logging.getLogger(__name__)
18+
19+
20+
def update_config(app, env):
21+
theme_options = app.config["html_theme_options"]
22+
if theme_options.get("search_bar_position") == "navbar":
23+
logger.warn(
24+
(
25+
"Deprecated config `search_bar_position` used."
26+
"Use `search-field.html` in `navbar_end` template list instead."
27+
)
28+
)
29+
30+
31+
def update_templates(app, pagename, templatename, context, doctree):
32+
"""Update template names for page build."""
33+
template_sections = [
34+
"theme_navbar_start",
35+
"theme_navbar_center",
36+
"theme_navbar_end",
37+
"theme_footer_items",
38+
"theme_page_sidebar_items",
39+
"sidebars",
40+
]
41+
42+
for section in template_sections:
43+
if context.get(section):
44+
# Break apart `,` separated strings so we can use , in the defaults
45+
if isinstance(context.get(section), str):
46+
context[section] = [
47+
ii.strip() for ii in context.get(section).split(",")
48+
]
49+
50+
# Add `.html` to templates with no suffix
51+
for ii, template in enumerate(context.get(section)):
52+
if not os.path.splitext(template)[1]:
53+
context[section][ii] = template + ".html"
54+
1655

1756
def add_toctree_functions(app, pagename, templatename, context, doctree):
1857
"""Add functions so Jinja templates can add toctree objects."""
@@ -440,8 +479,10 @@ def setup(app):
440479
# our custom HTML builder
441480
app.set_translator("readthedocs", BootstrapHTML5Translator, override=True)
442481
app.set_translator("readthedocsdirhtml", BootstrapHTML5Translator, override=True)
482+
app.connect("env-updated", update_config)
443483
app.connect("html-page-context", setup_edit_url)
444484
app.connect("html-page-context", add_toctree_functions)
485+
app.connect("html-page-context", update_templates)
445486

446487
# Update templates for sidebar
447488
pkgdir = os.path.abspath(os.path.dirname(__file__))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<p class="copyright">
2+
{%- if hasdoc('copyright') %}
3+
{% trans path=pathto('copyright'), copyright=copyright|e %}&copy; <a href="{{ path }}">Copyright</a> {{ copyright }}.{% endtrans %}<br/>
4+
{%- else %}
5+
{% trans copyright=copyright|e %}&copy; Copyright {{ copyright }}.{% endtrans %}<br/>
6+
{%- endif %}
7+
</p>
File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p class="last-updated">
2+
{% trans last_updated=last_updated|e %}Last updated on {{ last_updated }}.{% endtrans %}<br/>
3+
</p>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{%- block icon_links -%}
2+
{%- include "icon-links.html" with context -%}
3+
{%- endblock %}

0 commit comments

Comments
 (0)