From 7b4ec5c5e9a89fc1bc3b3fc8dfad26450530a2da Mon Sep 17 00:00:00 2001 From: asciimoo Date: Fri, 10 Jan 2014 23:38:08 +0100 Subject: [fix] highlighting only html --- searx/utils.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'searx/utils.py') diff --git a/searx/utils.py b/searx/utils.py index 670499805..53300181f 100644 --- a/searx/utils.py +++ b/searx/utils.py @@ -3,6 +3,32 @@ from HTMLParser import HTMLParser import csv import codecs import cStringIO +import re + +def highlight_content(content, query): + + if not content: + return None + # ignoring html contents + # TODO better html content detection + if content.find('<') != -1: + return content + + query = query.decode('utf-8') + if content.lower().find(query.lower()) > -1: + query_regex = u'({0})'.format(re.escape(query)) + content = re.sub(query_regex, '\\1', content, flags=re.I | re.U) + else: + regex_parts = [] + for chunk in query.split(): + if len(chunk) == 1: + regex_parts.append(u'\W+{0}\W+'.format(re.escape(chunk))) + else: + regex_parts.append(u'{0}'.format(re.escape(chunk))) + query_regex = u'({0})'.format('|'.join(regex_parts)) + content = re.sub(query_regex, '\\1', content, flags=re.I | re.U) + + return content class HTMLTextExtractor(HTMLParser): def __init__(self): -- cgit v1.2.3