Commit 8ad61eccaa8449e03c99de82a7fdf2ad1d0d82bf

Authored by Luan
1 parent ccd4fb6e

Adding highlight to search #22

src/colab/custom_settings.py
... ... @@ -15,6 +15,10 @@ LANGUAGES = (
15 15  
16 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 22 HAYSTACK_CUSTOM_HIGHLIGHTER = 'colab.utils.highlighting.ColabHighlighter'
19 23  
20 24 HAYSTACK_CONNECTIONS = {
... ...
src/colab/utils/highlighting.py
1 1 from haystack.utils import Highlighter
2   -
3   -
  2 +from django.conf import settings
  3 +
  4 +
4 5 class ColabHighlighter(Highlighter):
5 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)
... ...
src/templates/search/preview-search.html 0 → 100644
... ... @@ -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 1 {% load i18n %}
  2 +{% load highlight %}
2 3  
3 4 <span class="glyphicon glyphicon-tag" title="{{ result.type }}"></span>
4 5  
... ... @@ -12,4 +13,4 @@
12 13 </a>
13 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 1 {% extends "base.html" %}
2 2 {% load i18n %}
3 3 {% load append_to_get %}
  4 +{% load highlight %}
  5 +
4 6 {% block main-content %}
5 7 <div class="row">
6 8 <div class="col-lg-2">
... ... @@ -47,23 +49,13 @@
47 49 <ul class="none indent list-unstyled">
48 50 {% for result in page.object_list %}
49 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 54 {% if result.author %}
63 55 <div class="quiet">
64 56 <span class="pull-left">{% trans "by" %}
65 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 59 {% else %}
68 60 <span>{{ result.author }}</span>
69 61 {% endif %}
... ...