Skip to content

Commit 69bbbec

Browse files
authored
Add favicons option for defining multiple favicons (#359)
1 parent 911f232 commit 69bbbec

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

docs/user_guide/configuring.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,49 @@ an external site. You can add external links to the nav bar like so:
121121
]
122122
}
123123
124+
Adding favicons
125+
===============
126+
127+
``pydata_sphinx_theme`` supports the
128+
`standard sphinx favicon configuration <https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_favicon>`_,
129+
using ``html_favicon``.
130+
131+
Additionally, ``pydata_sphinx_theme`` allows you to add any number of
132+
browser- or device-specific favicons of any size. To define arbitrary favicons,
133+
use the ``favicons`` configuration key. The ``href`` value can be either an
134+
absolute URL (beginning with ``http``) or a local path relative to your
135+
``html_static_path``:
136+
137+
.. code-block:: python
138+
139+
html_theme_options = {
140+
"favicons": [
141+
{
142+
"rel": "icon",
143+
"sizes": "16x16",
144+
"href": "https://secure.example.com/favicon/favicon-16x16.png",
145+
},
146+
{
147+
"rel": "icon",
148+
"sizes": "32x32",
149+
"href": "favicon-32x32.png",
150+
},
151+
{
152+
"rel": "apple-touch-icon",
153+
"sizes": "180x180",
154+
"href": "apple-touch-icon-180x180.png"
155+
},
156+
]
157+
}
158+
159+
``pydata_sphinx_theme`` will add ``link`` tags to your document's ``head``
160+
section, following this pattern:
161+
162+
.. code-block:: html+jinja
163+
164+
<link rel="{{ favicon.rel }}" sizes="{{ favicon.sizes }}" href="{{ favicon.href }}">
165+
166+
124167
.. _configure-sidebar:
125168

126169
Configure the sidebar

pydata_sphinx_theme/layout.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
{%- block extrahead %}
2525
<meta name="viewport" content="width=device-width, initial-scale=1" />
2626
<meta name="docsearch:language" content="en" />
27+
{% for favicon in theme_favicons %}
28+
{% if favicon.href[:4] == 'http'%}
29+
<link rel="{{ favicon.rel }}" sizes="{{ favicon.sizes }}" href="{{ favicon.href }}">
30+
{% else %}
31+
<link rel="{{ favicon.rel }}" sizes="{{ favicon.sizes }}" href="{{ pathto('_static/' + favicon.href, 1) }}">
32+
{% endif %}
33+
{% endfor %}
2734
{%- endblock %}
2835

2936
{# Silence the sidebar's, relbar's #}

pydata_sphinx_theme/theme.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ twitter_url =
1515
icon_links_label = Icon Links
1616
icon_links =
1717
google_analytics_id =
18+
favicons =
1819
show_prev_next = True
1920
search_bar_text = Search the docs ...
2021
search_bar_position = sidebar

tests/test_build.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,47 @@ def test_logo_name(sphinx_build_factory):
113113
assert "PyData Tests" in index_html.select(".navbar-brand")[0].text.strip()
114114

115115

116+
def test_favicons(sphinx_build_factory):
117+
"""Test that arbitrary favicons are included."""
118+
html_theme_options_favicons = {
119+
"favicons": [
120+
{
121+
"rel": "icon",
122+
"sizes": "16x16",
123+
"href": "https://secure.example.com/favicon/favicon-16x16.png",
124+
},
125+
{
126+
"rel": "icon",
127+
"sizes": "32x32",
128+
"href": "favicon-32x32.png",
129+
},
130+
{
131+
"rel": "apple-touch-icon",
132+
"sizes": "180x180",
133+
"href": "apple-touch-icon-180x180.png",
134+
},
135+
]
136+
}
137+
confoverrides = {"html_theme_options": html_theme_options_favicons}
138+
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
139+
140+
index_html = sphinx_build.html_tree("index.html")
141+
142+
icon_16 = (
143+
'<link href="https://secure.example.com/favicon/favicon-16x16.png" '
144+
'rel="icon" sizes="16x16"/>'
145+
)
146+
icon_32 = '<link href="_static/favicon-32x32.png" rel="icon" sizes="32x32"/>'
147+
icon_180 = (
148+
'<link href="_static/apple-touch-icon-180x180.png" '
149+
'rel="apple-touch-icon" sizes="180x180"/>'
150+
)
151+
152+
assert icon_16 in str(index_html.select("head")[0])
153+
assert icon_32 in str(index_html.select("head")[0])
154+
assert icon_180 in str(index_html.select("head")[0])
155+
156+
116157
def test_sidebar_default(sphinx_build_factory):
117158
"""The sidebar is shrunk when no sidebars specified in html_sidebars."""
118159
sphinx_build = sphinx_build_factory("base").build()

0 commit comments

Comments
 (0)