on master help sign in
Updated webutils.highlight_content to ignore double-quotes when highlighting query parts
commit138f32471c5dfe12299471037782ac353462be74 authorDaniel Hones <danielhones@gmail.com> dateMon 8 Feb 2021, 23:58 (6 years ago) parentab87398
3 files changed+27−2
AUTHORS.rst+1−1 searx/webutils.py+4−1 tests/unit/test_webutils.py+22
AUTHORS.rst+1−1
@@ -154,6 +154,6 @@ generally made searx better:
154154- @mrwormo
155155- Xiaoyu WEI @xywei
156156- @joshu9h
157−
157+- Daniel Hones
158158
159159
searx/webutils.py+4−1
@@ -119,7 +119,10 @@ def highlight_content(content, query):
119119 else:
120120 regex_parts = []
121121 for chunk in query.split():
122− if len(chunk) == 1:
122+ chunk = chunk.replace('"', '')
123+ if len(chunk) == 0:
124+ continue
125+ elif len(chunk) == 1:
123126 regex_parts.append('\\W+{0}\\W+'.format(re.escape(chunk)))
124127 else:
125128 regex_parts.append('{0}'.format(re.escape(chunk)))
tests/unit/test_webutils.py+22
@@ -34,6 +34,28 @@ class TestWebUtils(SearxTestCase):
3434 query = 'a test'
3535 self.assertEqual(webutils.highlight_content(content, query), content)
3636
37+ data = (
38+ ('" test "',
39+ 'a test string',
40+ 'a <span class="highlight">test</span> string'),
41+ ('"a"',
42+ 'this is a test string',
43+ 'this is<span class="highlight"> a </span>test string'),
44+ ('a test',
45+ 'this is a test string that matches entire query',
46+ 'this is <span class="highlight">a test</span> string that matches entire query'),
47+ ('this a test',
48+ 'this is a string to test.',
49+ ('<span class="highlight">this</span> is<span class="highlight"> a </span>'
50+ 'string to <span class="highlight">test</span>.')),
51+ ('match this "exact phrase"',
52+ 'this string contains the exact phrase we want to match',
53+ ('<span class="highlight">this</span> string contains the <span class="highlight">exact</span>'
54+ ' <span class="highlight">phrase</span> we want to <span class="highlight">match</span>'))
55+ )
56+ for query, content, expected in data:
57+ self.assertEqual(webutils.highlight_content(content, query), expected)
58+
3759
3860class TestUnicodeWriter(SearxTestCase):
3961