def test_dont_highlight_highlightings(self): self.assertEquals( highlight(HTML_CONTENT.replace('world', 'highlight'), ['hello', 'highlight']), '<html><body><p><span class="highlight term-1">Hello</span> ' '<span class="highlight term-2">highlight</span> !</p>' '</body></html>')
def render(self, context): output = self.nodelist.render(context) terms = self.terms if terms[0] == terms[-1] and terms[0] in ("'", '"'): terms = terms[1:-1] else: terms = self.terms_var.resolve(context) return highlight(output, remove_duplicates(RE_ARG_SPLIT.split(terms)))
def process_response(self, request, response): """ Transform the HTML if keywords are present. """ if (response.status_code != 200 or not 'text/html' in response['Content-Type']): return response referrer = request.META.get('HTTP_REFERER') engine, domain, terms = self.parse_search(referrer) for GET_varname in GET_VARNAMES: if request.GET.get(GET_varname): terms.extend(request.GET[GET_varname].split()) if not terms: return response response.content = highlight(response.content, remove_duplicates(terms)) return response
def test_highlight_multiple_in_one_markup(self): self.assertEquals( highlight(HTML_CONTENT.replace('!', 'hello'), ['hello']), '<html><body><p><span class="highlight term-1">Hello</span> ' 'world <span class="highlight term-1">hello</span></p>' '</body></html>')
def test_highlight_none(self): self.assertEquals(highlight(HTML_CONTENT, []), HTML_CONTENT)
def test_highlight_case(self): self.assertEquals( highlight(HTML_CONTENT, ['HELLO', 'World']), '<html><body><p><span class="highlight term-1">Hello</span> ' '<span class="highlight term-2">world</span> !</p></body></html>')
def test_dont_highlight_protected_markups(self): self.assertEquals( highlight(HTML_CONTENT.replace('world', '<pre>world</pre>'), ['world']), '<html><body><p>Hello <pre>world</pre> !</p></body></html>')
def highlight_filter(value, terms, autoescape=None): esc = autoescape and conditional_escape or (lambda x: x) return mark_safe( highlight(esc(value), remove_duplicates(RE_ARG_SPLIT.split(terms))))
def test_highlight(self): self.assertEquals( highlight(HTML_CONTENT, ['Hello', 'world']), '<html><body><p><span class="highlight term-1">Hello</span> ' '<span class="highlight term-2">world</span> !</p></body></html>')
def test_highlight_none(self): self.assertEquals( highlight(HTML_CONTENT, []), HTML_CONTENT)
def highlight_filter(value, terms, autoescape=None): esc = autoescape and conditional_escape or (lambda x: x) return mark_safe(highlight(esc(value), remove_duplicates(RE_ARG_SPLIT.split(terms))))