2010-07-01 19 views
13

Czy istnieje sposób, aby znacznik szablonu Django-haystack o {% highlight %} wyświetlał pełną zmienną, zamiast usuwać wszystko przed pierwszym dopasowaniem?Znacznik wydania szablonu django stemplu django

używam go tak:

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

5 lat później i mam ten sam problem. Na Github jest nawet problem: https://github.com/django-haystack/django-haystack/issues/748 – weeheavy

Odpowiedz

9

nigdy nie używałem stogu siana, ale z szybkim spojrzeniem w the docs i the source wygląda można zrobić swój własny wyróżnienia i powiedzieć stogu siana w użyciu że zamiast

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) 

a następnie ustawić

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

w ustawieniach .py

2

Odpowiedź przez sekundę działa, ale jeśli nie chcesz, aby obcinała koniec napisu i jesteś poniżej maksymalnej długości, możesz spróbować. Wciąż testuję to, ale wydaje się działać:

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) 
Powiązane problemy