[英]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. 我正在使用Django Haystack在我的网站上进行搜索,但我需要使用模板过滤器“safe”过滤我的TextField的所有html代码,并根据搜索条件突出显示搜索结果。
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. 你真正想要的是突出HTML文档中的单词。 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: 如果用户在搜索框中输入
content
,您将需要获得以下内容:
<h1>my title</h1>
my <em>content</em>
But wait a minute, what if user type h1
in search. 但是等一下,如果用户在搜索中输入
h1
怎么办? 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: 所以要解决你的问题,荧光笔应该:
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
这里解释如何: http : //django-haystack.readthedocs.org/en/latest/highlighting.html
This template tag will highlight words only for the text part of your html code. 此模板标记仅突出显示html代码文本部分的单词。
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)
看起来这可能对您有所帮助: Django - 在没有{%load ..%}的情况下,通过整个网站的自定义标记替换内置模板标签
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.