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

HTML: Assign glossary terms with grouping keys to single-character groupings #12862

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ Bugs fixed
file URL (user-defined base URL of an intersphinx project are left untouched
even if they end with double forward slashes).
Patch by Bénédikt Tran.
* #12707: HTML: Fix a bug where the grouping keys (categories) used in
glossaries would be used instead of the expected single-character groupings
displayed for all other index entries.
Patch by James Addison.

Testing
-------
Expand Down
5 changes: 4 additions & 1 deletion sphinx/environment/adapters/indexentries.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def create_index(
except ValueError:
entry, = _split_into(1, 'single', value)
sub_entry = ''
if category_key and not entry.startswith(category_key):
# TODO: can sub_entry be non-empty here? under what conditions?
entry, sub_entry = category_key, entry
_add_entry(entry, sub_entry, main,
dic=new, link=uri, key=category_key)
elif entry_type == 'pair':
Expand Down Expand Up @@ -219,7 +222,7 @@ def _group_by_func(entry: tuple[str, _IndexEntry]) -> str:
key, (targets, sub_items, category_key) = entry

if category_key is not None:
return category_key
key = category_key

# now calculate the key
if key.startswith('\N{RIGHT-TO-LEFT MARK}'):
Expand Down
6 changes: 3 additions & 3 deletions tests/roots/test-glossary/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ test-glossary
*fermion*
Particle with half-integer spin.

tauon
myon
electron
tauon : fermions
myon : fermions
electron : fermions
Examples for fermions.

über
Expand Down
6 changes: 3 additions & 3 deletions tests/roots/test-root/markup.txt
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,9 @@ This tests :CLASS:`role names in uppercase`.
*fermion*
Particle with half-integer spin.

tauon
myon
electron
tauon : fermions
myon : fermions
electron : fermions
Examples for fermions.

über
Expand Down
17 changes: 16 additions & 1 deletion tests/test_builders/test_build_html_5_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,28 @@
import pytest
from docutils import nodes

from sphinx import locale

from tests.test_builders.xpath_util import check_xpath

if TYPE_CHECKING:
from collections.abc import Callable, Iterable
from collections.abc import Callable, Iterable, Sequence
from typing import Literal
from xml.etree.ElementTree import Element


def _symbolic_link_check(nodes: Sequence[Element]) -> None:
"""Confirm that a series of nodes are HTML hyperlinks represented by individual symbols"""
assert nodes, 'Expected at least one node to check'
_ = locale.get_translation('sphinx')
for idx, node in enumerate(nodes):
assert node.tag == 'a', 'Attempted to check hyperlink on a non-anchor element'
hyperlink_label = ''.join(node.itertext())
if idx == 0 and hyperlink_label == _('Symbols'):
continue # allow the first label to be a named group of symbols
assert len(hyperlink_label) == 1


def tail_check(check: str) -> Callable[[Iterable[Element]], Literal[True]]:
rex = re.compile(check)

Expand Down Expand Up @@ -481,6 +495,7 @@ def checker(nodes: Iterable[Element]) -> Literal[True]:
('genindex.html', './/a/strong', 'Other'),
('genindex.html', './/a', 'entry'),
('genindex.html', './/li/a', 'double'),
('genindex.html', './/div[@class="genindex-jumpbox"]/a', _symbolic_link_check),
('otherext.html', './/h1', 'Generated section'),
('otherext.html', ".//a[@href='_sources/otherext.foo.txt']", ''),
('search.html', ".//meta[@name='robots'][@content='noindex']", ''),
Expand Down
Loading