Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django haystack highlight template tag issue

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 %}
like image 709
Rick Avatar asked Jul 01 '10 18:07

Rick


1 Answers

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

like image 58
second Avatar answered Oct 05 '22 16:10

second