Skip to content

Commit 8fc9287

Browse files
gabalafoudrammock12rambau
authored
Change default logo alt text (#1472)
* Default logo alt text only if no extra text * change default logo * use docstitle as default logo alt text * update docs to reflect change * Apply suggestions from code review Co-authored-by: Daniel McCloy <[email protected]> * use string formatting operator * Update docs/user_guide/branding.rst * docs fixes * Update docs/user_guide/branding.rst * add test * Update pyproject.toml * revert to original --------- Co-authored-by: Daniel McCloy <[email protected]> Co-authored-by: Rambaud Pierrick <[email protected]>
1 parent 1741930 commit 8fc9287

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed

docs/user_guide/branding.rst

+41-14
Original file line numberDiff line numberDiff line change
@@ -77,40 +77,67 @@ To reference an external website, make sure your link starts with ``http``:
7777
}
7878
}
7979
80-
Customize logo alternative text
80+
Logo title and alternative text
8181
-------------------------------
8282

83-
You may set a custom ``alt text`` for your logo to replace the default ``"logo image"`` generic description.
84-
Adding a descriptive ``alt text`` can help make your documentation more accessible to readers using screen readers or another assistive tech.
85-
86-
To do so, customize the ``html_theme_options["logo"]["alt_text"]`` configuration option as in the following example:
83+
If you provide a logo image, it replaces ``project`` or ``html_title`` in the
84+
header nav bar. If you want to display both your site's logo and title (or any
85+
other text) next to the logo, you provide it with the ``text`` property like so:
8786

8887
.. code-block:: python
8988
:caption: conf.py
9089
9190
html_theme_options = {
9291
"logo": {
93-
# Because the logo is also a homepage link, including "home" in the alt text is good practice
94-
"alt_text": "My Project Name - Home",
92+
"text": "My awesome documentation",
93+
"image_light": "_static/logo-light.png",
94+
"image_dark": "_static/logo-dark.png",
9595
}
9696
}
9797
98-
Add a logo title
99-
----------------
100-
101-
To add a title in the brand section of your documentation, define a value for ``html_theme_options.logo["text"]``.
102-
This title will appear next to the logo image if set.
98+
But if you only want to display the logo and not the site title, then it's good
99+
practice to provide alt text, which helps blind visitors and others who rely on
100+
screen readers:
103101

104102
.. code-block:: python
103+
:caption: conf.py
105104
106105
html_theme_options = {
107106
"logo": {
108-
"text": "My awesome documentation",
107+
# Because the logo is also a homepage link, including "home" in the
108+
# alt text is good practice
109+
"alt_text": "My awesome documentation - Home",
110+
"image_light": "_static/logo-light.png",
111+
"image_dark": "_static/logo-dark.png",
109112
}
110113
}
111114
112-
.. note:: The ``html_title`` field will work as well if no logo images are specified.
115+
In most cases, you will provide either ``text`` or ``alt_text``, not both, but
116+
there are some circumstances in which it may make sense to provide both:
117+
118+
.. code-block:: python
119+
:caption: conf.py
120+
121+
html_theme_options = {
122+
"logo": {
123+
# In a left-to-right context, screen readers will read the alt text
124+
# first, then the text, so this example will be read as "P-G-G-P-Y
125+
# (short pause) Home A pretty good geometry package"
126+
"alt_text": "PggPy - Home",
127+
"text": "A pretty good geometry package",
128+
"image_light": "_static/logo-light.png",
129+
"image_dark": "_static/logo-dark.png",
130+
}
131+
}
113132
133+
If you do not provide ``text`` or ``alt_text``, the theme will provide some
134+
default alt text (otherwise, your homepage link would appear to assistive tech
135+
as something like "Unlabeled image"). The default alt text is "`docstitle
136+
<https://www.sphinx-doc.org/en/master/development/templating.html#docstitle>`_ -
137+
Home", but if you provide a logo title (``text``) the default alt text will be an
138+
empty string (the assumption is that if you provide a logo title, the title is
139+
probably doing the work of the alt text, and as shown above, you can override
140+
this assumption by supplying both ``text`` and ``alt_text``).
114141

115142
Add favicons
116143
============

src/pydata_sphinx_theme/theme/pydata_sphinx_theme/components/navbar-logo.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
<a class="navbar-brand logo" href="{{ href }}">
1212
{# get all the brand information from html_theme_option #}
1313
{% set is_logo = "light" in theme_logo["image_relative"] %}
14-
{% set alt = theme_logo.get("alt_text", "Logo image") %}
14+
{# use alt_text if given. If not, only add alt text if no additional branding text given. #}
15+
{% set alt = theme_logo.get("alt_text", "" if theme_logo.get("text") else "%s - Home" % docstitle) %}
1516
{% if is_logo %}
1617
{# Theme switching is only available when JavaScript is enabled.
1718
# Thus we should add the extra image using JavaScript, defaulting

tests/test_build.py

+16
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ def test_icon_links(sphinx_build_factory, file_regression) -> None:
115115
)
116116

117117

118+
@pytest.mark.parametrize(
119+
"confoverrides,expected_alt_text",
120+
(
121+
[dict(), "PyData Tests documentation - Home"],
122+
[dict(html_theme_options=dict(logo=dict(text="Foo"))), ""],
123+
[dict(html_theme_options=dict(logo=dict(text="Foo", alt_text="Bar"))), "Bar"],
124+
),
125+
)
126+
def test_logo_alt_text(sphinx_build_factory, confoverrides, expected_alt_text) -> None:
127+
"""Test our alt-text fallback mechanism."""
128+
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
129+
index_html = sphinx_build.html_tree("index.html")
130+
logo_image = index_html.select(".navbar-brand img")[0]
131+
assert logo_image.attrs["alt"] == expected_alt_text
132+
133+
118134
def test_logo_basic(sphinx_build_factory) -> None:
119135
"""Test that the logo is shown by default, project title if no logo."""
120136
sphinx_build = sphinx_build_factory("base").build()

0 commit comments

Comments
 (0)