From 8ad61eccaa8449e03c99de82a7fdf2ad1d0d82bf Mon Sep 17 00:00:00 2001 From: Luan Date: Mon, 14 Oct 2013 15:10:20 -0300 Subject: [PATCH] Adding highlight to search #22 --- src/colab/custom_settings.py | 4 ++++ src/colab/utils/highlighting.py | 31 ++++++++++++++++++++++++++++--- src/templates/search/preview-search.html | 31 +++++++++++++++++++++++++++++++ src/templates/search/search-ticket-preview.html | 3 ++- src/templates/search/search.html | 16 ++++------------ 5 files changed, 69 insertions(+), 16 deletions(-) create mode 100644 src/templates/search/preview-search.html 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 @@
    {% for result in page.object_list %}
  • - {% if result.model_name == 'message' %} - {% include "search/search-message-preview.html" %} - {% elif result.model_name == 'user' %} - {% include "search/search-user-preview.html" %} - {% elif result.model_name == 'wiki' %} - {% include "search/search-wiki-preview.html" %} - {% elif result.model_name == 'revision' %} - {% include "search/search-revision-preview.html" %} - {% elif result.model_name == 'ticket' %} - {% include "search/search-ticket-preview.html" %} - {% endif %} + {% include "search/preview-search.html" %} {% if result.author %}
    {% trans "by" %} {% if result.author and result.author_url %} - {{ result.author }} + {% highlight result.author with query %} {% else %} {{ result.author }} {% endif %} -- libgit2 0.21.2