14

{% highlight %}最初の一致の前にすべてを削除するのではなく、django-haystack のテンプレート タグに渡された完全な変数を表示する方法はありますか?

私はこのように使用しています:

{% highlight thread.title with request.GET.q %}
4

2 に答える 2

10

haystack を使用したことはありませんが、ドキュメントソースをざっと見てみると、独自のカスタム ハイライターを作成し、代わりにそれを使用するよう 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で

于 2011-10-29T11:21:19.317 に答える
2

The answer by @second works, however if you also don't want it to cut off the end of the string and you're under the max length you can try this. Still testing it but it seems to work:

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)
于 2014-05-08T21:14:52.560 に答える