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):
@register.filter
@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.
'''
......@@ -101,6 +101,13 @@ def fmttranslation(value, language=None, diff=None):
diffvalue = escape(force_unicode(diff[idx]))
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
value = NEWLINES_RE.sub('\n', value)
......@@ -143,6 +150,15 @@ def fmttranslationdiff(value, other):
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
@stringfilter
def fmtsourcediff(value, other):
......
......@@ -103,6 +103,7 @@ def search(translation, request):
search_form = SearchForm(request.GET)
review_form = ReviewForm(request.GET)
search_query = None
if review_form.is_valid():
# Review
allunits = translation.unit_set.review(
......@@ -125,9 +126,10 @@ def search(translation, request):
search_form.cleaned_data['tgt'],
)
search_query = search_form.cleaned_data['q']
name = get_search_name(
search_form.cleaned_data['search'],
search_form.cleaned_data['q'],
search_query,
)
else:
# Filtering by type
......@@ -159,6 +161,7 @@ def search(translation, request):
# Store in cache and return
search_id = str(uuid.uuid1())
search_result = {
'query': search_query,
'name': name,
'ids': unit_ids,
'search_id': search_id,
......@@ -466,6 +469,7 @@ def translate(request, project, subproject, lang):
'last_changes_url': urlencode(obj.get_kwargs()),
'total': obj.unit_set.all().count(),
'search_id': search_result['search_id'],
'search_query': search_result['query'],
'offset': offset,
'filter_name': search_result['name'],
'filter_count': num_results,
......
......@@ -54,7 +54,7 @@
{% endif %}
<tr>
<th class="source">{% trans "Source" %}</th>
<td class="translatetext">{{ unit.source|fmttranslation }}</td>
<td class="translatetext">{{ unit.source|fmtsearchmatch:search_query }}</td>
</tr>
{% if unit.previous_source %}
<tr>
......
......@@ -62,7 +62,9 @@ nav li {
.translation_comment {
white-space: pre-line;
}
.hlmatch {
background-color: #eb3;
}
.hlspace {
border-bottom: 1px dotted red;
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