on master help sign in
[enh] incasesensitive query highlighting
commitcbb397939db355fb53da6bcf6bd8a2627f7a9584 authorasciimoo <asciimoo@gmail.com> dateSun 10 Nov 2013, 21:41 (13 years ago) parent14cd1d6
1 file changed+12−2
searx/engines/__init__.py+12−2
searx/engines/__init__.py+12−2
@@ -25,6 +25,7 @@ from urlparse import urlparse
2525from searx import settings
2626import ConfigParser
2727import sys
28+import re
2829from datetime import datetime
2930
3031engine_dir = dirname(realpath(__file__))
@@ -106,8 +107,17 @@ def highlight_content(content, query):
106107 # TODO better html content detection
107108 if content.find('<') != -1:
108109 return content
109− for chunk in query.split():
110− content = content.replace(chunk, '<b>{0}</b>'.format(chunk))
110+
111+ if content.lower().find(query.lower()) > -1:
112+ query_regex = '({0})'.format(re.escape(query))
113+ content = re.sub(query_regex, '<b>\\1</b>', content, flags=re.I)
114+ else:
115+ for chunk in query.split():
116+ if len(chunk) == 1:
117+ query_regex = '(\W+{0}\W+)'.format(re.escape(chunk))
118+ else:
119+ query_regex = '({0})'.format(re.escape(chunk))
120+ content = re.sub(query_regex, '<b>\\1</b>', content, flags=re.I)
111121
112122 return content
113123