有没有办法让django-haystack的{% highlight %}
模板标签显示传入的完整变量,而不是在第一次匹配之前删除所有内容?
我正在使用它:
{% highlight thread.title with request.GET.q %}
答案 0 :(得分:9)
我从来没有使用干草堆,但是通过快速查看the docs和the source看起来你可以制作自己的自定义荧光笔并告诉haystack使用它而不是
from haystack.utils import Highlighter
from django.utils.html import strip_tags
class MyHighlighter(Highlighter):
def highlight(self, text_block):
self.text_block = strip_tags(text_block)
highlight_locations = self.find_highlightable_words()
start_offset, end_offset = self.find_window(highlight_locations)
# this is my only edit here, but you'll have to experiment
start_offset = 0
return self.render_html(highlight_locations, start_offset, end_offset)
然后设置
HAYSTACK_CUSTOM_HIGHLIGHTER = 'path.to.your.highligher.MyHighlighter'
你在settings.py中的
答案 1 :(得分:2)
@second的答案有效,但是如果你也不希望它切断字符串的结尾,你可以尝试这个。仍然测试它,但它似乎工作:
class MyHighlighter(Highlighter):
"""
Custom highlighter
"""
def highlight(self, text_block):
self.text_block = strip_tags(text_block)
highlight_locations = self.find_highlightable_words()
start_offset, end_offset = self.find_window(highlight_locations)
text_len = len(self.text_block)
if text_len <= self.max_length:
start_offset = 0
elif (text_len - 1 - start_offset) <= self.max_length:
end_offset = text_len
start_offset = end_offset - self.max_length
if start_offset < 0:
start_offset = 0
return self.render_html(highlight_locations, start_offset, end_offset)