diff --git a/src/colab/custom_settings.py b/src/colab/custom_settings.py index cf40c21..03b3ed0 100644 --- a/src/colab/custom_settings.py +++ b/src/colab/custom_settings.py @@ -15,6 +15,10 @@ LANGUAGES = ( LANGUAGE_CODE = 'pt-br' + +# the following variable define how many characters should be shown before +# a highlighted word, to make sure that the highlighted word will appear +HIGHLIGHT_NUM_CHARS_BEFORE_MATCH = 30 HAYSTACK_CUSTOM_HIGHLIGHTER = 'colab.utils.highlighting.ColabHighlighter' HAYSTACK_CONNECTIONS = { diff --git a/src/colab/utils/highlighting.py b/src/colab/utils/highlighting.py index f90f713..353d84b 100644 --- a/src/colab/utils/highlighting.py +++ b/src/colab/utils/highlighting.py @@ -1,6 +1,31 @@ from haystack.utils import Highlighter - - +from django.conf import settings + + class ColabHighlighter(Highlighter): def find_window(self, highlight_locations): - return (0, self.max_length) + """Getting the HIGHLIGHT_NUM_CHARS_BEFORE_MATCH setting + to find how many characters before the first word found should + be removed from the window + """ + + if len(self.text_block) <= self.max_length: + return (0, self.max_length) + + num_chars_before = getattr( + settings, + 'HIGHLIGHT_NUM_CHARS_BEFORE_MATCH', + 0 + ) + + best_start, best_end = super(ColabHighlighter, self).find_window( + highlight_locations + ) + if best_start <= num_chars_before: + best_end -= best_start + best_start = 0 + else: + best_start -= num_chars_before + best_end -= num_chars_before + + return (best_start, best_end) diff --git a/src/templates/search/preview-search.html b/src/templates/search/preview-search.html new file mode 100644 index 0000000..179ccf1 --- /dev/null +++ b/src/templates/search/preview-search.html @@ -0,0 +1,31 @@ +{% load i18n %} +{% load highlight %} + + + +{% if result.mailinglist_url %} + +{% else %} + +{% endif %} + {% if result.tag %} + {{ result.tag }} + {% endif %} +{% if result.mailinglist_url %} + +{% endif %} + + {% if result.title %} + + + {% highlight result.title with query max_length "1000" %} + + {% endif %} +{% if not result.mailinglist_url %} + +{% endif %} + +{% if result.description %} + + - {% highlight result.description with query max_length "150" %} +{% endif %} diff --git a/src/templates/search/search-ticket-preview.html b/src/templates/search/search-ticket-preview.html index e286b53..eade62b 100644 --- a/src/templates/search/search-ticket-preview.html +++ b/src/templates/search/search-ticket-preview.html @@ -1,4 +1,5 @@ {% load i18n %} +{% load highlight %} @@ -12,4 +13,4 @@ -- {{ result.description|striptags|truncatechars:150 }} +- {% highlight result.description with query max_length "150" %} diff --git a/src/templates/search/search.html b/src/templates/search/search.html index 410de96..160b26c 100644 --- a/src/templates/search/search.html +++ b/src/templates/search/search.html @@ -1,6 +1,8 @@ {% extends "base.html" %} {% load i18n %} {% load append_to_get %} +{% load highlight %} + {% block main-content %}
@@ -47,23 +49,13 @@