From 14a53e343085083d0ddf271adff2d13ca1c94f99 Mon Sep 17 00:00:00 2001 From: asciimoo Date: Sat, 9 Nov 2013 18:37:25 +0100 Subject: [PATCH] [enh] search query highlight in result texts --- searx/engines/__init__.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/searx/engines/__init__.py b/searx/engines/__init__.py index bcc52418..14d688c6 100644 --- a/searx/engines/__init__.py +++ b/searx/engines/__init__.py @@ -83,6 +83,7 @@ def default_request_params(): return {'method': 'GET', 'headers': {}, 'data': {}, 'url': '', 'cookies': {}} def make_callback(engine_name, results, callback, params): + # creating a callback wrapper for the search engine results def process_callback(response, **kwargs): cb_res = [] response.search_params = params @@ -100,6 +101,16 @@ def make_callback(engine_name, results, callback, params): results[engine_name] = cb_res return process_callback +def highlight_content(content, query): + # ignoring html contents + # TODO better html content detection + if content.find('<') != -1: + return content + for chunk in query.split(): + content = content.replace(chunk, '{0}'.format(chunk)) + + return content + def search(query, request, selected_engines): global engines, categories, number_of_searches requests = [] @@ -176,6 +187,8 @@ def search(query, request, selected_engines): results.append(res) for result in results: + if 'content' in result: + result['content'] = highlight_content(result['content'], query) for res_engine in result['engines']: engines[result['engine']].stats['score_count'] += result['score']