Merge pull request #7226 from tk0miya/7220_main_indexentries

Fix #7220: genindex: "main" index entries are not shown at first
This commit is contained in:
Takeshi KOMIYA 2020-03-01 02:14:18 +09:00 committed by GitHub
commit dfaff26688
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 3 deletions

View File

@ -63,6 +63,7 @@ Features added
``no-scaled-link`` class ``no-scaled-link`` class
* #7144: Add CSS class indicating its domain for each desc node * #7144: Add CSS class indicating its domain for each desc node
* #7211: latex: Use babel for Chinese document when using XeLaTeX * #7211: latex: Use babel for Chinese document when using XeLaTeX
* #7220: genindex: Show "main" index entries at first
Bugs fixed Bugs fixed
---------- ----------

View File

@ -7,7 +7,7 @@
:copyright: Copyright 2007-2020 by the Sphinx team, see AUTHORS. :copyright: Copyright 2007-2020 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.
""" """
import bisect
import re import re
import unicodedata import unicodedata
from itertools import groupby from itertools import groupby
@ -52,8 +52,7 @@ class IndexEntries:
except NoUri: except NoUri:
pass pass
else: else:
# maintain links in sorted/deterministic order entry[0].append((main, uri))
bisect.insort(entry[0], (main, uri))
domain = cast(IndexDomain, self.env.get_domain('index')) domain = cast(IndexDomain, self.env.get_domain('index'))
for fn, entries in domain.entries.items(): for fn, entries in domain.entries.items():
@ -89,6 +88,16 @@ class IndexEntries:
except ValueError as err: except ValueError as err:
logger.warning(str(err), location=fn) 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 # 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 # following the letters in ASCII, this is where the chr(127) comes from
def keyfunc(entry: Tuple[str, List]) -> Tuple[str, str]: def keyfunc(entry: Tuple[str, List]) -> Tuple[str, str]:

View File

@ -112,6 +112,21 @@ def test_create_seealso_index(app):
assert index[2] == ('S', [('Sphinx', [[], [('see also documentation tool', [])], None])]) 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) @pytest.mark.sphinx('dummy', freshenv=True)
def test_create_index_with_name(app): def test_create_index_with_name(app):
text = (".. index:: single: docutils\n" text = (".. index:: single: docutils\n"