Commit 34bf24b1 authored by Michal Čihař's avatar Michal Čihař

Improve bbcode highlighter and add testcase for it

Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 133cca7b
...@@ -26,7 +26,7 @@ from django.utils.translation import ugettext_lazy as _ ...@@ -26,7 +26,7 @@ from django.utils.translation import ugettext_lazy as _
from weblate.trans.checks.base import TargetCheck from weblate.trans.checks.base import TargetCheck
BBCODE_MATCH = re.compile( BBCODE_MATCH = re.compile(
r'\[(?P<tag>[^]]*)(?=(@[^]]*)?\](.*?)\[\/(?P=tag)\])', r'(?P<start>\[(?P<tag>[^]]+)(@[^]]*)?\])(.*?)(?P<end>\[\/(?P=tag)\])',
re.MULTILINE re.MULTILINE
) )
...@@ -61,8 +61,8 @@ class BBCodeCheck(TargetCheck): ...@@ -61,8 +61,8 @@ class BBCodeCheck(TargetCheck):
if len(src_match) != len(tgt_match): if len(src_match) != len(tgt_match):
return True return True
src_tags = set([x[0] for x in src_match]) src_tags = set([x[1] for x in src_match])
tgt_tags = set([x[0] for x in tgt_match]) tgt_tags = set([x[1] for x in tgt_match])
return src_tags != tgt_tags return src_tags != tgt_tags
...@@ -70,13 +70,12 @@ class BBCodeCheck(TargetCheck): ...@@ -70,13 +70,12 @@ class BBCodeCheck(TargetCheck):
if self.should_skip(unit): if self.should_skip(unit):
return [] return []
ret = [] ret = []
match_objects = BBCODE_MATCH.finditer(source) for match in BBCODE_MATCH.finditer(source):
for match in match_objects: ret.append((match.start('start'), match.group('start')))
if match.start() == match.end(): ret.append((match.start('end'), match.group('end')))
continue
ret.append((match.start(), match.group()))
return ret return ret
class XMLTagsCheck(TargetCheck): class XMLTagsCheck(TargetCheck):
''' '''
Check whether XML in target matches source. Check whether XML in target matches source.
......
...@@ -37,6 +37,10 @@ class BBCodeCheckTest(CheckTestCase): ...@@ -37,6 +37,10 @@ class BBCodeCheckTest(CheckTestCase):
self.test_good_matching = ('[a]string[/a]', '[a]string[/a]', '') self.test_good_matching = ('[a]string[/a]', '[a]string[/a]', '')
self.test_failure_1 = ('[a]string[/a]', '[b]string[/b]', '') self.test_failure_1 = ('[a]string[/a]', '[b]string[/b]', '')
self.test_failure_2 = ('[a]string[/a]', 'string', '') self.test_failure_2 = ('[a]string[/a]', 'string', '')
self.test_highlight = (
'[a]string[/a]',
[(0, '[a]'), (9, '[/a]')]
)
class XMLTagsCheckTest(CheckTestCase): class XMLTagsCheckTest(CheckTestCase):
......
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