Commit 03a9715c authored by Michal Čihař's avatar Michal Čihař

Move logic out of presentation code

Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent eadde14c
...@@ -21,6 +21,37 @@ ...@@ -21,6 +21,37 @@
from weblate import appsettings from weblate import appsettings
from weblate.trans.util import load_class from weblate.trans.util import load_class
def highlight_string(source, unit):
"""Returns highlights for a string"""
if unit is None:
return []
highlights = []
for check in CHECKS:
if not CHECKS[check].target:
continue
highlights += CHECKS[check].check_highlight(source, unit)
# Sort by order in string
highlights.sort(key=lambda x: x[0])
# Remove overlapping ones
for hl_idx in xrange(0, len(highlights)):
if hl_idx >= len(highlights):
break
elref = highlights[hl_idx]
for hl_idx_next in xrange(hl_idx + 1, len(highlights)):
if hl_idx_next >= len(highlights):
break
eltest = highlights[hl_idx_next]
if eltest[0] >= elref[0] and eltest[0] < elref[1]:
highlights.pop(hl_idx_next)
elif eltest[0] > elref[1]:
break
return highlights
# Initialize checks list # Initialize checks list
CHECKS = {} CHECKS = {}
for path in appsettings.CHECK_LIST: for path in appsettings.CHECK_LIST:
......
...@@ -41,7 +41,7 @@ from weblate.lang.models import Language ...@@ -41,7 +41,7 @@ from weblate.lang.models import Language
from weblate.trans.models import ( from weblate.trans.models import (
Project, SubProject, Dictionary, Advertisement, WhiteboardMessage Project, SubProject, Dictionary, Advertisement, WhiteboardMessage
) )
from weblate.trans.checks import CHECKS from weblate.trans.checks import CHECKS, highlight_string
register = template.Library() register = template.Library()
...@@ -86,30 +86,8 @@ def fmt_check_highlights(value, unit): ...@@ -86,30 +86,8 @@ def fmt_check_highlights(value, unit):
"""Formats check highlights""" """Formats check highlights"""
if unit is None: if unit is None:
return [] return []
# Find all checks highlight
highlights = []
for check in CHECKS:
if not CHECKS[check].target:
continue
highlights += CHECKS[check].check_highlight(value, unit)
#Sort by order in string
if highlights:
highlights.sort(key=lambda tup: tup[0])
#remove probelmatics ones (overlapping or one inside another)
for hl_idx in xrange(0, len(highlights)):
if hl_idx >= len(highlights):
break
elref = highlights[hl_idx]
for hl_idx_next in xrange(hl_idx + 1, len(highlights)):
if hl_idx_next >= len(highlights):
break
eltest = highlights[hl_idx_next]
if eltest[0] >= elref[0] and eltest[0] < elref[1]:
highlights.pop(hl_idx_next)
elif eltest[0] > elref[1]:
break
#then transform highlights to escaped html #then transform highlights to escaped html
highlights = [(h[0], h[1], escape(force_text(h[2]))) for h in highlights] highlights = [(h[0], h[1], escape(force_text(h[2]))) for h in highlight_string(value, unit)]
return highlights return highlights
......
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