Skip to content

Commit

Permalink
Merge pull request #7226 from tk0miya/7220_main_indexentries
Browse files Browse the repository at this point in the history
Fix #7220: genindex: "main" index entries are not shown at first
  • Loading branch information
tk0miya committed Feb 29, 2020
2 parents c84f7bf + e3c8e88 commit dfaff26
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -63,6 +63,7 @@ Features added
``no-scaled-link`` class
* #7144: Add CSS class indicating its domain for each desc node
* #7211: latex: Use babel for Chinese document when using XeLaTeX
* #7220: genindex: Show "main" index entries at first

Bugs fixed
----------
Expand Down
15 changes: 12 additions & 3 deletions sphinx/environment/adapters/indexentries.py
Expand Up @@ -7,7 +7,7 @@
:copyright: Copyright 2007-2020 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
import bisect

import re
import unicodedata
from itertools import groupby
Expand Down Expand Up @@ -52,8 +52,7 @@ def add_entry(word: str, subword: str, main: str, link: bool = True,
except NoUri:
pass
else:
# maintain links in sorted/deterministic order
bisect.insort(entry[0], (main, uri))
entry[0].append((main, uri))

domain = cast(IndexDomain, self.env.get_domain('index'))
for fn, entries in domain.entries.items():
Expand Down Expand Up @@ -89,6 +88,16 @@ def add_entry(word: str, subword: str, main: str, link: bool = True,
except ValueError as err:
logger.warning(str(err), location=fn)

# sort the index entries for same keyword.
def keyfunc0(entry: Tuple[str, str]) -> Tuple[bool, str]:
main, uri = entry
return (not main, uri) # show main entries at first

for indexentry in new.values():
indexentry[0] = sorted(indexentry[0], key=keyfunc0)
for subentry in indexentry[1].values():
subentry[0] = sorted(subentry[0], key=keyfunc0) # type: ignore

# sort the index entries; put all symbols at the front, even those
# following the letters in ASCII, this is where the chr(127) comes from
def keyfunc(entry: Tuple[str, List]) -> Tuple[str, str]:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_environment_indexentries.py
Expand Up @@ -112,6 +112,21 @@ def test_create_seealso_index(app):
assert index[2] == ('S', [('Sphinx', [[], [('see also documentation tool', [])], None])])


@pytest.mark.sphinx('dummy', freshenv=True)
def test_create_main_index(app):
text = (".. index:: !docutils\n"
".. index:: docutils\n"
".. index:: pip; install\n"
".. index:: !pip; install\n")
restructuredtext.parse(app, text)
index = IndexEntries(app.env).create_index(app.builder)
assert len(index) == 2
assert index[0] == ('D', [('docutils', [[('main', '#index-0'),
('', '#index-1')], [], None])])
assert index[1] == ('P', [('pip', [[], [('install', [('main', '#index-3'),
('', '#index-2')])], None])])


@pytest.mark.sphinx('dummy', freshenv=True)
def test_create_index_with_name(app):
text = (".. index:: single: docutils\n"
Expand Down

0 comments on commit dfaff26

Please sign in to comment.