on master help sign in
Updated webutils.highlight_content to ignore double-quotes when highlighting query parts
138f324Daniel Hones
AUTHORS.rst+1−1
@@ -154,6 +154,6 @@ generally made searx better:
154- @mrwormo
154- @mrwormo
155- Xiaoyu WEI @xywei
155- Xiaoyu WEI @xywei
156- @joshu9h
156- @joshu9h
157−
157+- Daniel Hones
158
158
159
159
searx/webutils.py+4−1
@@ -119,7 +119,10 @@ def highlight_content(content, query):
119 else:
119 else:
120 regex_parts = []
120 regex_parts = []
121 for chunk in query.split():
121 for chunk in query.split():
122+ chunk = chunk.replace('"', '')
122− if len(chunk) == 1:
123+ if len(chunk) == 0:
124+ continue
125+ elif len(chunk) == 1:
123 regex_parts.append('\\W+{0}\\W+'.format(re.escape(chunk)))
126 regex_parts.append('\\W+{0}\\W+'.format(re.escape(chunk)))
124 else:
127 else:
125 regex_parts.append('{0}'.format(re.escape(chunk)))
128 regex_parts.append('{0}'.format(re.escape(chunk)))
tests/unit/test_webutils.py+22
@@ -34,6 +34,28 @@ class TestWebUtils(SearxTestCase):
34 query = 'a test'
34 query = 'a test'
35 self.assertEqual(webutils.highlight_content(content, query), content)
35 self.assertEqual(webutils.highlight_content(content, query), content)
36
36
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+
37
59
38class TestUnicodeWriter(SearxTestCase):
60class TestUnicodeWriter(SearxTestCase):
39
61