Skip to content

Commit

Permalink
Add markdown link references check
Browse files Browse the repository at this point in the history
It does check whether link references match source.

Issue #2495

Signed-off-by: Michal Čihař <[email protected]>
  • Loading branch information
nijel committed Feb 7, 2019
1 parent e6c9012 commit 701600b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/user/checks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,20 @@ not desired result from changing the translation, but occasionally it is.

Checks that XML tags are replicated between both source and translation.


.. _check-md-reflink:

Markdown link references
~~~~~~~~~~~~~~~~~~~~~~~~

Markdown link references does not match source.

.. seealso::

`Markdown links`_

.. _Markdown links: https://daringfireball.net/projects/markdown/syntax#link

Source checks
-------------

Expand Down
31 changes: 31 additions & 0 deletions weblate/checks/markup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
re.MULTILINE
)

MD_REFLINK = re.compile(
r'^!?\[('
r'(?:\[[^^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*'
r')\]\s*\[([^^\]]*)\]'
)

XML_MATCH = re.compile(r'<[^>]+>')
XML_ENTITY_MATCH = re.compile(r'&#?\w+;')

Expand Down Expand Up @@ -176,3 +182,28 @@ def check_highlight(self, source, unit):
for match in XML_ENTITY_MATCH.finditer(source):
ret.append((match.start(), match.end(), match.group()))
return ret


class MarkdownBaseCheck(TargetCheck):
default_disabled = True

def __init__(self):
super(MarkdownBaseCheck, self).__init__()
self.enable_string = 'md-text'


class MarkdownRefLinkCheck(MarkdownBaseCheck):
check_id = 'md-reflink'
name = _('Markdown link references')
description = _('Markdown link references does not match source')

def check_single(self, source, target, unit):
src_match = MD_REFLINK.findall(source)
if not src_match:
return False
tgt_match = MD_REFLINK.findall(target)

src_tags = {x[1] for x in src_match}
tgt_tags = {x[1] for x in tgt_match}

return src_tags != tgt_tags
1 change: 1 addition & 0 deletions weblate/checks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class WeblateChecksConf(AppConf):
'weblate.checks.chars.ZeroWidthSpaceCheck',
'weblate.checks.markup.XMLValidityCheck',
'weblate.checks.markup.XMLTagsCheck',
'weblate.checks.markup.MarkdownRefLinkCheck',
'weblate.checks.source.OptionalPluralCheck',
'weblate.checks.source.EllipsisCheck',
'weblate.checks.source.MultipleFailingCheck',
Expand Down
12 changes: 12 additions & 0 deletions weblate/checks/tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def setUp(self):
self.test_good_matching = ('string', 'string', '')
self.test_good_none = ('string', 'string', '')
self.test_good_ignore = ()
self.test_good_flag = ()
self.test_failure_1 = ()
self.test_failure_2 = ()
self.test_failure_3 = ()
Expand Down Expand Up @@ -141,6 +142,17 @@ def test_single_failure_2(self):
def test_single_failure_3(self):
self.do_test(True, self.test_failure_3)

def test_check_good_flag(self):
if self.check is None or not self.test_good_flag:
return
self.assertFalse(
self.check.check_target(
[self.test_good_flag[0]],
[self.test_good_flag[1]],
MockUnit(None, self.test_good_flag[2])
)
)

def test_check_good_matching_singular(self):
if self.check is None:
return
Expand Down
12 changes: 12 additions & 0 deletions weblate/checks/tests/test_markup_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
BBCodeCheck,
XMLTagsCheck,
XMLValidityCheck,
MarkdownRefLinkCheck,
)
from weblate.checks.tests.test_checks import CheckTestCase

Expand Down Expand Up @@ -149,3 +150,14 @@ def test_root(self):
''
),
)


class MarkdownRefLinkCheckTest(CheckTestCase):
check = MarkdownRefLinkCheck()

def setUp(self):
super(MarkdownRefLinkCheckTest, self).setUp()
self.test_good_matching = ('[a][a1]', '[b][a1]', 'md-text')
self.test_good_none = ('string', 'string', 'md-text')
self.test_good_flag = ('[a][a1]', '[b][a2]', '')
self.test_failure_1 = ('[a][a1]', '[b][a2]', 'md-text')
1 change: 1 addition & 0 deletions weblate/settings_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@
# 'weblate.checks.chars.ZeroWidthSpaceCheck',
# 'weblate.checks.markup.XMLValidityCheck',
# 'weblate.checks.markup.XMLTagsCheck',
# 'weblate.checks.markup.MarkdownRefLinkCheck',
# 'weblate.checks.source.OptionalPluralCheck',
# 'weblate.checks.source.EllipsisCheck',
# 'weblate.checks.source.MultipleFailingCheck',
Expand Down

0 comments on commit 701600b

Please sign in to comment.