Updated webutils.highlight_content to ignore double-quotes when highlighting query parts

This commit is contained in:
Daniel Hones 2021-02-08 23:58:54 -05:00
parent ab8739809c
commit 138f32471c
3 changed files with 27 additions and 2 deletions

View File

@ -154,6 +154,6 @@ generally made searx better:
- @mrwormo - @mrwormo
- Xiaoyu WEI @xywei - Xiaoyu WEI @xywei
- @joshu9h - @joshu9h
- Daniel Hones

View File

@ -119,7 +119,10 @@ def highlight_content(content, query):
else: else:
regex_parts = [] regex_parts = []
for chunk in query.split(): for chunk in query.split():
if len(chunk) == 1: chunk = chunk.replace('"', '')
if len(chunk) == 0:
continue
elif len(chunk) == 1:
regex_parts.append('\\W+{0}\\W+'.format(re.escape(chunk))) regex_parts.append('\\W+{0}\\W+'.format(re.escape(chunk)))
else: else:
regex_parts.append('{0}'.format(re.escape(chunk))) regex_parts.append('{0}'.format(re.escape(chunk)))

View File

@ -34,6 +34,28 @@ class TestWebUtils(SearxTestCase):
query = 'a test' query = 'a test'
self.assertEqual(webutils.highlight_content(content, query), content) self.assertEqual(webutils.highlight_content(content, query), content)
data = (
('" test "',
'a test string',
'a <span class="highlight">test</span> string'),
('"a"',
'this is a test string',
'this is<span class="highlight"> a </span>test string'),
('a test',
'this is a test string that matches entire query',
'this is <span class="highlight">a test</span> string that matches entire query'),
('this a test',
'this is a string to test.',
('<span class="highlight">this</span> is<span class="highlight"> a </span>'
'string to <span class="highlight">test</span>.')),
('match this "exact phrase"',
'this string contains the exact phrase we want to match',
('<span class="highlight">this</span> string contains the <span class="highlight">exact</span>'
' <span class="highlight">phrase</span> we want to <span class="highlight">match</span>'))
)
for query, content, expected in data:
self.assertEqual(webutils.highlight_content(content, query), expected)
class TestUnicodeWriter(SearxTestCase): class TestUnicodeWriter(SearxTestCase):