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 Search: omit anchor reference from document titles in the search index. #12047

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ Bugs fixed
download cache.
Patch by James Addison and Adam Turner.

* #11961: Omit anchor references from document title entries in the search index,
removing duplication of search results.
Patch by James Addison.

Testing
-------

Expand Down
2 changes: 1 addition & 1 deletion sphinx/search/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ def _visit_nodes(node):
elif isinstance(node, nodes.title):
title = node.astext()
ids = node.parent['ids']
word_store.titles.append((title, ids[0] if ids else None))
word_store.titles.append((title, ids[0] if ids and word_store.titles else None)) # NoQA: E501
word_store.title_words.extend(split(title))
for child in node.children:
_visit_nodes(child)
Expand Down
36 changes: 29 additions & 7 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def is_registered_term(index, keyword):

.. test that comments are not indexed: boson

another_title
===============

test that non-comments are indexed: fermion
'''

Expand Down Expand Up @@ -172,7 +175,10 @@ def test_IndexBuilder():
'index': {'docname1_1', 'docname1_2', 'docname2_1', 'docname2_2'},
'test': {'docname1_1', 'docname1_2', 'docname2_1', 'docname2_2'},
}
assert index._title_mapping == {'section_titl': {'docname1_1', 'docname1_2', 'docname2_1', 'docname2_2'}}
assert index._title_mapping == {
'another_titl': {'docname1_1', 'docname1_2', 'docname2_1', 'docname2_2'},
'section_titl': {'docname1_1', 'docname1_2', 'docname2_1', 'docname2_2'},
}
assert index._objtypes == {}
assert index._objnames == {}

Expand All @@ -192,8 +198,14 @@ def test_IndexBuilder():
'non': [0, 1, 2, 3],
'test': [0, 1, 2, 3]},
'titles': ('title1_1', 'title1_2', 'title2_1', 'title2_2'),
'titleterms': {'section_titl': [0, 1, 2, 3]},
'alltitles': {'section_title': [(0, 'section-title'), (1, 'section-title'), (2, 'section-title'), (3, 'section-title')]},
'titleterms': {
'another_titl': [0, 1, 2, 3],
'section_titl': [0, 1, 2, 3],
},
'alltitles': {
'another_title': [(0, 'another-title'), (1, 'another-title'), (2, 'another-title'), (3, 'another-title')],
'section_title': [(0, None), (1, None), (2, None), (3, None)],
},
'indexentries': {},
}
assert index._objtypes == {('dummy1', 'objtype1'): 0, ('dummy2', 'objtype1'): 1}
Expand Down Expand Up @@ -234,7 +246,10 @@ def test_IndexBuilder():
'index': {'docname1_2', 'docname2_2'},
'test': {'docname1_2', 'docname2_2'},
}
assert index._title_mapping == {'section_titl': {'docname1_2', 'docname2_2'}}
assert index._title_mapping == {
'another_titl': {'docname1_2', 'docname2_2'},
'section_titl': {'docname1_2', 'docname2_2'},
}
assert index._objtypes == {('dummy1', 'objtype1'): 0, ('dummy2', 'objtype1'): 1}
assert index._objnames == {0: ('dummy1', 'objtype1', 'objtype1'), 1: ('dummy2', 'objtype1', 'objtype1')}

Expand All @@ -253,8 +268,14 @@ def test_IndexBuilder():
'non': [0, 1],
'test': [0, 1]},
'titles': ('title1_2', 'title2_2'),
'titleterms': {'section_titl': [0, 1]},
'alltitles': {'section_title': [(0, 'section-title'), (1, 'section-title')]},
'titleterms': {
'another_titl': [0, 1],
'section_titl': [0, 1],
},
'alltitles': {
'another_title': [(0, 'another-title'), (1, 'another-title')],
'section_title': [(0, None), (1, None)],
},
'indexentries': {},
}
assert index._objtypes == {('dummy1', 'objtype1'): 0, ('dummy2', 'objtype1'): 1}
Expand Down Expand Up @@ -343,6 +364,7 @@ def assert_is_sorted(item, path: str):
assert_is_sorted(value, f'{path}.{key}')
elif isinstance(item, list):
if not is_title_tuple_type(item) and path not in lists_not_to_sort:
assert item == sorted(item), f'{err_path} is not sorted'
# sort nulls last; http://stackoverflow.com/questions/19868767/
assert item == sorted(item, key=lambda x: (x is None, x)), f'{err_path} is not sorted'
for i, child in enumerate(item):
assert_is_sorted(child, f'{path}[{i}]')