2010-07-01 18 views
13

¿Hay alguna manera de hacer que la etiqueta de la plantilla {% highlight %} de django-haystack muestre la variable completa pasada, en lugar de eliminar todo antes de la primera coincidencia?django haystack highlight template tag issue

lo estoy usando como esto:

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

5 años después y tengo el mismo problema. Incluso hay un problema en Github: https://github.com/django-haystack/django-haystack/issues/748 – weeheavy

Respuesta

9

nunca he utilizado pajar, pero desde una mirada rápida en the docs y the source parece que usted puede hacer su propio rotulador personalizada y contar pajar de usar que en lugar

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) 

y luego configurar

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

en su settings.py

2

La respuesta por @second funciona, sin embargo, si tampoco desea que corte el final de la cadena y está por debajo de la longitud máxima, puede intentarlo. Todavía lo estoy probando pero parece funcionar:

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)