Commit 8ad61eccaa8449e03c99de82a7fdf2ad1d0d82bf
1 parent
ccd4fb6e
Exists in
master
and in
39 other branches
Adding highlight to search #22
Showing
5 changed files
with
69 additions
and
16 deletions
Show diff stats
src/colab/custom_settings.py
| @@ -15,6 +15,10 @@ LANGUAGES = ( | @@ -15,6 +15,10 @@ LANGUAGES = ( | ||
| 15 | 15 | ||
| 16 | LANGUAGE_CODE = 'pt-br' | 16 | LANGUAGE_CODE = 'pt-br' |
| 17 | 17 | ||
| 18 | + | ||
| 19 | +# the following variable define how many characters should be shown before | ||
| 20 | +# a highlighted word, to make sure that the highlighted word will appear | ||
| 21 | +HIGHLIGHT_NUM_CHARS_BEFORE_MATCH = 30 | ||
| 18 | HAYSTACK_CUSTOM_HIGHLIGHTER = 'colab.utils.highlighting.ColabHighlighter' | 22 | HAYSTACK_CUSTOM_HIGHLIGHTER = 'colab.utils.highlighting.ColabHighlighter' |
| 19 | 23 | ||
| 20 | HAYSTACK_CONNECTIONS = { | 24 | HAYSTACK_CONNECTIONS = { |
src/colab/utils/highlighting.py
| 1 | from haystack.utils import Highlighter | 1 | from haystack.utils import Highlighter |
| 2 | - | ||
| 3 | - | 2 | +from django.conf import settings |
| 3 | + | ||
| 4 | + | ||
| 4 | class ColabHighlighter(Highlighter): | 5 | class ColabHighlighter(Highlighter): |
| 5 | def find_window(self, highlight_locations): | 6 | def find_window(self, highlight_locations): |
| 6 | - return (0, self.max_length) | 7 | + """Getting the HIGHLIGHT_NUM_CHARS_BEFORE_MATCH setting |
| 8 | + to find how many characters before the first word found should | ||
| 9 | + be removed from the window | ||
| 10 | + """ | ||
| 11 | + | ||
| 12 | + if len(self.text_block) <= self.max_length: | ||
| 13 | + return (0, self.max_length) | ||
| 14 | + | ||
| 15 | + num_chars_before = getattr( | ||
| 16 | + settings, | ||
| 17 | + 'HIGHLIGHT_NUM_CHARS_BEFORE_MATCH', | ||
| 18 | + 0 | ||
| 19 | + ) | ||
| 20 | + | ||
| 21 | + best_start, best_end = super(ColabHighlighter, self).find_window( | ||
| 22 | + highlight_locations | ||
| 23 | + ) | ||
| 24 | + if best_start <= num_chars_before: | ||
| 25 | + best_end -= best_start | ||
| 26 | + best_start = 0 | ||
| 27 | + else: | ||
| 28 | + best_start -= num_chars_before | ||
| 29 | + best_end -= num_chars_before | ||
| 30 | + | ||
| 31 | + return (best_start, best_end) |
| @@ -0,0 +1,31 @@ | @@ -0,0 +1,31 @@ | ||
| 1 | +{% load i18n %} | ||
| 2 | +{% load highlight %} | ||
| 3 | + | ||
| 4 | +<span class="glyphicon glyphicon-{{ result.icon_name }}" title="{{ result.type }}"></span> | ||
| 5 | + | ||
| 6 | +{% if result.mailinglist_url %} | ||
| 7 | + <a href="{{ result.mailinglist_url }}"> | ||
| 8 | +{% else %} | ||
| 9 | + <a href="{{ result.url }}" {% if result.description %}title="{{ result.description|escape }}"{% endif %}> | ||
| 10 | +{% endif %} | ||
| 11 | + {% if result.tag %} | ||
| 12 | + <span class="label label-primary">{{ result.tag }}</span> | ||
| 13 | + {% endif %} | ||
| 14 | +{% if result.mailinglist_url %} | ||
| 15 | + </a> | ||
| 16 | +{% endif %} | ||
| 17 | + | ||
| 18 | + {% if result.title %} | ||
| 19 | + <span class="subject"> | ||
| 20 | + <!-- a striptags filter was raising an error here because using with highlight --> | ||
| 21 | + {% highlight result.title with query max_length "1000" %} | ||
| 22 | + </span> | ||
| 23 | + {% endif %} | ||
| 24 | +{% if not result.mailinglist_url %} | ||
| 25 | + </a> | ||
| 26 | +{% endif %} | ||
| 27 | + | ||
| 28 | +{% if result.description %} | ||
| 29 | + <!-- a striptags filter was raising an error here because using with highlight --> | ||
| 30 | + <span class="quiet">- {% highlight result.description with query max_length "150" %}</span> | ||
| 31 | +{% endif %} |
src/templates/search/search-ticket-preview.html
| 1 | {% load i18n %} | 1 | {% load i18n %} |
| 2 | +{% load highlight %} | ||
| 2 | 3 | ||
| 3 | <span class="glyphicon glyphicon-tag" title="{{ result.type }}"></span> | 4 | <span class="glyphicon glyphicon-tag" title="{{ result.type }}"></span> |
| 4 | 5 | ||
| @@ -12,4 +13,4 @@ | @@ -12,4 +13,4 @@ | ||
| 12 | </a> | 13 | </a> |
| 13 | </span> | 14 | </span> |
| 14 | 15 | ||
| 15 | -<span class="quiet">- {{ result.description|striptags|truncatechars:150 }}</span> | 16 | +<span class="quiet">- {% highlight result.description with query max_length "150" %}</span> |
src/templates/search/search.html
| 1 | {% extends "base.html" %} | 1 | {% extends "base.html" %} |
| 2 | {% load i18n %} | 2 | {% load i18n %} |
| 3 | {% load append_to_get %} | 3 | {% load append_to_get %} |
| 4 | +{% load highlight %} | ||
| 5 | + | ||
| 4 | {% block main-content %} | 6 | {% block main-content %} |
| 5 | <div class="row"> | 7 | <div class="row"> |
| 6 | <div class="col-lg-2"> | 8 | <div class="col-lg-2"> |
| @@ -47,23 +49,13 @@ | @@ -47,23 +49,13 @@ | ||
| 47 | <ul class="none indent list-unstyled"> | 49 | <ul class="none indent list-unstyled"> |
| 48 | {% for result in page.object_list %} | 50 | {% for result in page.object_list %} |
| 49 | <li class="preview-message"> | 51 | <li class="preview-message"> |
| 50 | - {% if result.model_name == 'message' %} | ||
| 51 | - {% include "search/search-message-preview.html" %} | ||
| 52 | - {% elif result.model_name == 'user' %} | ||
| 53 | - {% include "search/search-user-preview.html" %} | ||
| 54 | - {% elif result.model_name == 'wiki' %} | ||
| 55 | - {% include "search/search-wiki-preview.html" %} | ||
| 56 | - {% elif result.model_name == 'revision' %} | ||
| 57 | - {% include "search/search-revision-preview.html" %} | ||
| 58 | - {% elif result.model_name == 'ticket' %} | ||
| 59 | - {% include "search/search-ticket-preview.html" %} | ||
| 60 | - {% endif %} | 52 | + {% include "search/preview-search.html" %} |
| 61 | 53 | ||
| 62 | {% if result.author %} | 54 | {% if result.author %} |
| 63 | <div class="quiet"> | 55 | <div class="quiet"> |
| 64 | <span class="pull-left">{% trans "by" %} | 56 | <span class="pull-left">{% trans "by" %} |
| 65 | {% if result.author and result.author_url %} | 57 | {% if result.author and result.author_url %} |
| 66 | - <a href="{{ result.author_url }}">{{ result.author }}</a> | 58 | + <a href="{{ result.author_url }}">{% highlight result.author with query %}</a> |
| 67 | {% else %} | 59 | {% else %} |
| 68 | <span>{{ result.author }}</span> | 60 | <span>{{ result.author }}</span> |
| 69 | {% endif %} | 61 | {% endif %} |