简体   繁体   中英

Safe filter with highlight template tag django-haystack

I'm using Django Haystack to make search on my site, but i need to filter all html code of my TextField with the template filter "safe" and highlight the search results according to search criteria.

Is there a way to do this? I've tried with

{% highlight result.object.content|safe with query %}

but it doesn't work.

你不要忘记加载{%highlight%}模板标签吗?

What you really want is to highlight words in HTML document. This problem is hard (using safe will not help you). Let assume that your content is:

<h1>my title</h1>
my content

If user type content in search box you will want to get something like this:

<h1>my title</h1>
my <em>content</em>

But wait a minute, what if user type h1 in search. If you apply algorithm naively you will get:

<<em>h1</em>>my title</<em>h1</em>>
my content

So to solve you problem highlighter should:

  1. Parse HTML.
  2. highlight in parsed document.
  3. Print document.

Unfortunatly I don't know if someone have written such highliter for haystack. But you can write you own. Here is explained how: http://django-haystack.readthedocs.org/en/latest/highlighting.html

I also faced this issue and the workaround can be to use the with tag:

{% load highlight %}
{% with obj.text|safe as text %}
    {% highlight text with my_query %}
{% endwith %}

This works for me :)

This template tag will highlight words only for the text part of your html code.

import re

from django import template
from django.utils.safestring import mark_safe

register = template.Library()


@register.tag(name="highlight")
def do_highlight(parser, token):
    tag_name, words = token.contents.split(' ', 1)
    nodelist = parser.parse(('endhighlight',))
    parser.delete_first_token()
    return HighlightNode(nodelist, words)

class HighlightNode(template.Node):
    def __init__(self, nodelist, words):
        self.nodelist = nodelist
        self.words = words

    def render(self, context):
        # initial data
        html_data = self.nodelist.render(context)

        # prepare words to be highlighted
        words = context.get(self.words, "")
        if words:
            words = [w for w in re.split('[^\w]+', words) if w]
            pattern = re.compile(r"(?P<filter>%s)" % '|'.join(words), re.IGNORECASE)
        else :
            # no need to go further if there is nothing to highlight
            return html_data

        # parse the html
        chunks = html_data.split('<')
        parsed_data = [chunks.pop(0)]

        # separate tag and text
        for chunk in chunks:
            if chunk:
                if '>' in chunk:
                    tagdata, text = chunk.split('>', 1)
                    endtag = '>'
                else:
                    # the tag didn't end
                    tagdata, text, endtag = chunk, '', ''

                # rebuild tag
                parsed_data.append('<')
                parsed_data.append(tagdata)
                parsed_data.append(endtag)

                # highligh words in text
                if text:
                    text = mark_safe(re.sub(pattern,
                                            r'<span class="highlight">\g<filter></span>',
                                            text))
                    parsed_data.append(text)

        return ''.join(parsed_data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM