Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing more of the highlighted context

I am wondering how to make django-haystack highlight template tag show more of the text preceeding the highlight?

It currently shows "highlighted text some other text here" where it must show a bit more of the previous context, like: "some other text here highlighted text some other text here"

Thanks.

like image 269
azio Avatar asked Dec 09 '25 00:12

azio


1 Answers

I think the best approach would be to extend Highlighter as described in the docs.

Something like:

from haystack.utils import Highlighter

class ShowMoreTextHighlighter(Highlighter):
    def find_window(self, highlight_locations):
        their_start, their_end = super(ShowMoreTextHighlighter, self).find_window(highlight_locations)
        # perform some clever operations here to find an earlier start location
        my_start = their_start/2 # or just do something simple
        return (my_start, their_end)
like image 71
Hamms Avatar answered Dec 11 '25 13:12

Hamms



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!