Commit c5f6dc31 authored by Chris Oelmueller's avatar Chris Oelmueller

Start work on highlighting search results for nijel/weblate#42

So far this only knows about source strings.
parent 0575c854
...@@ -70,7 +70,7 @@ def fmt_whitespace(value): ...@@ -70,7 +70,7 @@ def fmt_whitespace(value):
@register.filter @register.filter
@stringfilter @stringfilter
def fmttranslation(value, language=None, diff=None): def fmttranslation(value, language=None, diff=None, search_match=None):
''' '''
Formats translation to show whitespace, plural forms or diff. Formats translation to show whitespace, plural forms or diff.
''' '''
...@@ -101,6 +101,13 @@ def fmttranslation(value, language=None, diff=None): ...@@ -101,6 +101,13 @@ def fmttranslation(value, language=None, diff=None):
diffvalue = escape(force_unicode(diff[idx])) diffvalue = escape(force_unicode(diff[idx]))
value = html_diff(diffvalue, value) value = html_diff(diffvalue, value)
# Format search term
if search_match is not None:
value = value.replace(
search_match,
"<span class='hlmatch'>%s</span>" % (search_match)
)
# Normalize newlines # Normalize newlines
value = NEWLINES_RE.sub('\n', value) value = NEWLINES_RE.sub('\n', value)
...@@ -143,6 +150,15 @@ def fmttranslationdiff(value, other): ...@@ -143,6 +150,15 @@ def fmttranslationdiff(value, other):
return fmttranslation(value, other.translation.language, other.target) return fmttranslation(value, other.translation.language, other.target)
@register.filter
@stringfilter
def fmtsearchmatch(value, term):
'''
Formats terms matching a search query.
'''
return fmttranslation(value, search_match=term)
@register.filter @register.filter
@stringfilter @stringfilter
def fmtsourcediff(value, other): def fmtsourcediff(value, other):
......
...@@ -103,6 +103,7 @@ def search(translation, request): ...@@ -103,6 +103,7 @@ def search(translation, request):
search_form = SearchForm(request.GET) search_form = SearchForm(request.GET)
review_form = ReviewForm(request.GET) review_form = ReviewForm(request.GET)
search_query = None
if review_form.is_valid(): if review_form.is_valid():
# Review # Review
allunits = translation.unit_set.review( allunits = translation.unit_set.review(
...@@ -125,9 +126,10 @@ def search(translation, request): ...@@ -125,9 +126,10 @@ def search(translation, request):
search_form.cleaned_data['tgt'], search_form.cleaned_data['tgt'],
) )
search_query = search_form.cleaned_data['q']
name = get_search_name( name = get_search_name(
search_form.cleaned_data['search'], search_form.cleaned_data['search'],
search_form.cleaned_data['q'], search_query,
) )
else: else:
# Filtering by type # Filtering by type
...@@ -159,6 +161,7 @@ def search(translation, request): ...@@ -159,6 +161,7 @@ def search(translation, request):
# Store in cache and return # Store in cache and return
search_id = str(uuid.uuid1()) search_id = str(uuid.uuid1())
search_result = { search_result = {
'query': search_query,
'name': name, 'name': name,
'ids': unit_ids, 'ids': unit_ids,
'search_id': search_id, 'search_id': search_id,
...@@ -466,6 +469,7 @@ def translate(request, project, subproject, lang): ...@@ -466,6 +469,7 @@ def translate(request, project, subproject, lang):
'last_changes_url': urlencode(obj.get_kwargs()), 'last_changes_url': urlencode(obj.get_kwargs()),
'total': obj.unit_set.all().count(), 'total': obj.unit_set.all().count(),
'search_id': search_result['search_id'], 'search_id': search_result['search_id'],
'search_query': search_result['query'],
'offset': offset, 'offset': offset,
'filter_name': search_result['name'], 'filter_name': search_result['name'],
'filter_count': num_results, 'filter_count': num_results,
......
...@@ -54,7 +54,7 @@ ...@@ -54,7 +54,7 @@
{% endif %} {% endif %}
<tr> <tr>
<th class="source">{% trans "Source" %}</th> <th class="source">{% trans "Source" %}</th>
<td class="translatetext">{{ unit.source|fmttranslation }}</td> <td class="translatetext">{{ unit.source|fmtsearchmatch:search_query }}</td>
</tr> </tr>
{% if unit.previous_source %} {% if unit.previous_source %}
<tr> <tr>
......
...@@ -62,7 +62,9 @@ nav li { ...@@ -62,7 +62,9 @@ nav li {
.translation_comment { .translation_comment {
white-space: pre-line; white-space: pre-line;
} }
.hlmatch {
background-color: #eb3;
}
.hlspace { .hlspace {
border-bottom: 1px dotted red; border-bottom: 1px dotted red;
color: gray; color: gray;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment