Ejemplo n.º 1
0
 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>')
Ejemplo n.º 2
0
 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>')
Ejemplo n.º 3
0
    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)))
Ejemplo n.º 4
0
    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)))
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
 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>')
Ejemplo n.º 8
0
 def test_highlight_none(self):
     self.assertEquals(highlight(HTML_CONTENT, []), HTML_CONTENT)
Ejemplo n.º 9
0
 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>')
Ejemplo n.º 10
0
 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>')
Ejemplo n.º 11
0
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))))
Ejemplo n.º 12
0
 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>')
Ejemplo n.º 13
0
 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>')
Ejemplo n.º 14
0
 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>')
Ejemplo n.º 15
0
 def test_highlight_none(self):
     self.assertEquals(
         highlight(HTML_CONTENT, []), HTML_CONTENT)
Ejemplo n.º 16
0
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))))