django haystack突出显示模板标签问题

发布于 2024-09-08 01:53:30 字数 182 浏览 3 评论 0原文

有没有办法让 django-haystack 的 {%highlight %} 模板标签显示传入的完整变量,而不是删除第一个匹配之前的所有内容?

我这样使用它:

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

Is there a way to make django-haystack's {% highlight %} template tag show the full variable passed in, rather than removing everything before the first match?

I'm using it like this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮世清欢 2024-09-15 01:53:30

我从未使用过 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 中设置

i've never used haystack, but from a quick look in the docs and the source it looks like you can make your own custom highlighter and tell haystack to use that instead

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)

and then set

HAYSTACK_CUSTOM_HIGHLIGHTER = 'path.to.your.highligher.MyHighlighter'

in your settings.py

负佳期 2024-09-15 01:53:30

@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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文