logo

searx

Unnamed repository; edit this file 'description' to name the repository.
commit: 14a53e343085083d0ddf271adff2d13ca1c94f99
parent: e9467524741b67ef6ceaabf299931ae5bc32e9f6
Author: asciimoo <asciimoo@gmail.com>
Date:   Sat,  9 Nov 2013 18:37:25 +0100

[enh] search query highlight in result texts

Diffstat:

searx/engines/__init__.py | 13+++++++++++++
1 file changed, 13 insertions(+), 0 deletions(-)

diff --git 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, '<b>{0}</b>'.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']