Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
converse.js
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
converse.js
Commits
081e44c6
Commit
081e44c6
authored
Jan 26, 2016
by
Michal Čihař
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Split individual highlighting steps to separate code
Signed-off-by:
Michal Čihař
<
michal@cihar.com
>
parent
8e22224f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
33 deletions
+53
-33
weblate/trans/templatetags/translations.py
weblate/trans/templatetags/translations.py
+53
-33
No files found.
weblate/trans/templatetags/translations.py
View file @
081e44c6
...
...
@@ -48,9 +48,11 @@ register = template.Library()
SPACE_NL
=
'<span class="hlspace space-nl" title="{0}"></span><br />'
SPACE_TAB
=
'<span class="hlspace space-tab" title="{0}"></span>'
HL_CHECK
=
'''
<span class="hlcheck">{0}<span class="highlight-number"></span></span>
'''
HL_CHECK
=
(
'<span class="hlcheck">{0}'
'<span class="highlight-number"></span>'
'</span>'
)
WHITESPACE_RE
=
re
.
compile
(
r'( +| $|^ )'
)
NEWLINES_RE
=
re
.
compile
(
r'\r\n|\r|\n'
)
...
...
@@ -86,6 +88,49 @@ def fmt_whitespace(value):
return
value
def
fmt_diff
(
value
,
diff
,
idx
):
"""Format diff if there is any"""
if
diff
is
None
:
return
value
diffvalue
=
escape
(
force_text
(
diff
[
idx
]))
return
html_diff
(
diffvalue
,
value
)
def
fmt_highlights
(
raw_value
,
value
,
unit
):
"""Formats check highlights"""
print
'HL'
,
unit
if
unit
is
None
:
return
value
highlights
=
highlight_string
(
raw_value
,
unit
)
print
highlights
start_search
=
0
for
highlight
in
highlights
:
htext
=
escape
(
force_text
(
highlight
[
2
]))
find_highlight
=
value
.
find
(
htext
,
start_search
)
if
find_highlight
>=
0
:
newpart
=
HL_CHECK
.
format
(
htext
)
next_part
=
value
[(
find_highlight
+
len
(
htext
)):]
value
=
value
[:
find_highlight
]
+
newpart
+
next_part
start_search
=
find_highlight
+
len
(
newpart
)
return
value
def
fmt_search
(
value
,
search_match
):
"""Formats search match"""
if
search_match
:
# Since the search ignored case, we need to highlight any
# combination of upper and lower case we find. This is too
# advanced for str.replace().
caseless
=
re
.
compile
(
re
.
escape
(
search_match
),
re
.
IGNORECASE
)
for
variation
in
re
.
findall
(
caseless
,
value
):
value
=
re
.
sub
(
caseless
,
'<span class="hlmatch">{0}</span>'
.
format
(
variation
),
value
,
)
return
value
@
register
.
inclusion_tag
(
'format-translation.html'
)
def
format_translation
(
value
,
language
,
diff
=
None
,
search_match
=
None
,
simple
=
False
,
num_plurals
=
2
,
unit
=
None
):
...
...
@@ -112,43 +157,18 @@ def format_translation(value, language, diff=None, search_match=None,
# We will collect part for each plural
parts
=
[]
for
idx
,
value
in
enumerate
(
plurals
):
highlights
=
[]
if
unit
is
not
None
:
highlights
=
highlight_string
(
value
,
unit
)
for
idx
,
raw_value
in
enumerate
(
plurals
):
# HTML escape
value
=
escape
(
force_text
(
value
))
value
=
escape
(
force_text
(
raw_
value
))
# Format diff if there is any
if
diff
is
not
None
:
diffvalue
=
escape
(
force_text
(
diff
[
idx
]))
value
=
html_diff
(
diffvalue
,
value
)
value
=
fmt_diff
(
value
,
diff
,
idx
)
# Create span for checks highlights
if
highlights
:
start_search
=
0
for
highlight
in
highlights
:
htext
=
escape
(
force_text
(
highlight
[
2
]))
find_highlight
=
value
.
find
(
htext
,
start_search
)
if
find_highlight
>=
0
:
newpart
=
HL_CHECK
.
format
(
htext
)
next_part
=
value
[(
find_highlight
+
len
(
htext
)):]
value
=
value
[:
find_highlight
]
+
newpart
+
next_part
start_search
=
find_highlight
+
len
(
newpart
)
value
=
fmt_highlights
(
raw_value
,
value
,
unit
)
# Format search term
if
search_match
:
# Since the search ignored case, we need to highlight any
# combination of upper and lower case we find. This is too
# advanced for str.replace().
caseless
=
re
.
compile
(
re
.
escape
(
search_match
),
re
.
IGNORECASE
)
for
variation
in
re
.
findall
(
caseless
,
value
):
value
=
re
.
sub
(
caseless
,
'<span class="hlmatch">{0}</span>'
.
format
(
variation
),
value
,
)
value
=
fmt_search
(
value
,
search_match
)
# Normalize newlines
value
=
NEWLINES_RE
.
sub
(
'
\
n
'
,
value
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment