Skip to content

Commit

Permalink
feat: change config option to enable bbcode Check via a flag (Weblate…
Browse files Browse the repository at this point in the history
…Org#13551)

* change config option to enable bbcode Check via a flag

* documentation update

* documentation update

* documentation update

* don't skip check if no bbcode in source
  • Loading branch information
gersona authored Jan 23, 2025
1 parent b4c476f commit 88e668f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 7 deletions.
2 changes: 2 additions & 0 deletions docs/admin/checks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ Here is a list of flags currently accepted:
``rst-text``
Treat a text as an reStructuredText document, affects :ref:`check-same`.
Turns on :ref:`check-rst-syntax` and :ref:`check-rst-references`.
``bbcode-text``
Treat a text as an Bulletin Board Code (BBCode) document, affects :ref:`check-same`.
``dos-eol``
Uses DOS end-of-line markers instead of Unix ones (``\r\n`` instead of ``\n``).
``read-only``
Expand Down
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Not yet released.
**Compatibility**

* Running tests using Django test executor is no longer supported, see :doc:`/contributing/tests`.
* :ref:`check-bbcode` check is now disabled by default. The `bbcode-text` flag is required to activate this check, see :ref:`custom-checks`.
* API error responses format has changed, see :ref:`api-errors`.

**Upgrading**
Expand Down
5 changes: 5 additions & 0 deletions docs/user/checks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,15 @@ good quality translations.
BBCode markup
~~~~~~~~~~~~~

.. versionchanged:: 5.10

This checks no longer relies on unreliable automatic detection, it now needs to be turned on using the ``bbcode-text`` flag.

:Summary: BBCode in translation does not match source
:Scope: translated strings
:Check class: ``weblate.checks.markup.BBCodeCheck``
:Check identifier: ``bbcode``
:Flag to enable: ``bbcode-text``
:Flag to ignore: ``ignore-bbcode``

BBCode represents simple markup, like for example highlighting important parts of a
Expand Down
9 changes: 6 additions & 3 deletions weblate/checks/markup.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,16 @@ class BBCodeCheck(TargetCheck):
check_id = "bbcode"
name = gettext_lazy("BBCode markup")
description = gettext_lazy("BBCode in translation does not match source")
default_disabled = True

def __init__(self):
super().__init__()
self.enable_string = "bbcode-text"

def check_single(self, source: str, target: str, unit: Unit):
# Parse source
src_match = BBCODE_MATCH.findall(source)
# Any BBCode in source?
if not src_match:
return False

# Parse target
tgt_match = BBCODE_MATCH.findall(target)
if len(src_match) != len(tgt_match):
Expand Down
3 changes: 3 additions & 0 deletions weblate/checks/same.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from weblate.checks.base import TargetCheck
from weblate.checks.data import IGNORE_WORDS
from weblate.checks.format import FLAG_RULES, PERCENT_MATCH
from weblate.checks.markup import BBCODE_MATCH
from weblate.checks.qt import QT_FORMAT_MATCH, QT_PLURAL_MATCH
from weblate.checks.ruby import RUBY_FORMAT_MATCH

Expand Down Expand Up @@ -89,6 +90,8 @@ def strip_format(
regex = RST_MATCH
elif "percent-placeholders" in flags:
regex = PERCENT_MATCH
elif "bbcode-text" in flags:
regex = BBCODE_MATCH
else:
return msg
return regex.sub(replacement, msg)
Expand Down
5 changes: 5 additions & 0 deletions weblate/checks/tests/test_duplicate_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ def test_same_bbcode(self) -> None:
self.assertFalse(self.check.check_single("", "for [em]x[/em]", MockUnit()))
self.assertTrue(self.check.check_single("", "em [em]x[/em]", MockUnit()))
self.assertTrue(self.check.check_single("", "em [em]x", MockUnit()))
self.assertFalse(
self.check.check_single(
"", "em [em]x[/em]", MockUnit(flags=["bbcode-text"])
)
)

def test_duplicated_punctuation(self) -> None:
self.assertFalse(
Expand Down
13 changes: 9 additions & 4 deletions weblate/checks/tests/test_markup_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ class BBCodeCheckTest(CheckTestCase):

def setUp(self) -> None:
super().setUp()
self.test_good_matching = ("[a]string[/a]", "[a]string[/a]", "")
self.test_failure_1 = ("[a]string[/a]", "[b]string[/b]", "")
self.test_failure_2 = ("[a]string[/a]", "string", "")
self.test_highlight = ("", "[a]string[/a]", [(0, 3, "[a]"), (9, 13, "[/a]")])
self.test_good_matching = ("[a]string[/a]", "[a]string[/a]", "bbcode-text")
self.test_failure_1 = ("[a]string[/a]", "[b]string[/b]", "bbcode-text")
self.test_failure_2 = ("[a]string[/a]", "string", "bbcode-text")
self.test_ignore_check = ("[a]string[/a]", "[a]string[/a]", "")
self.test_highlight = (
"bbcode-text",
"[a]string[/a]",
[(0, 3, "[a]"), (9, 13, "[/a]")],
)


class XMLValidityCheckTest(CheckTestCase):
Expand Down

0 comments on commit 88e668f

Please sign in to comment.