Skip to content

Commit

Permalink
fix(autofix): ignore rst syntax in punctuation spacing fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed Jan 16, 2025
1 parent 32c8c63 commit 20367c0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Not yet released.
* :envvar:`WEBLATE_REGISTRATION_CAPTCHA` is now available in Docker container.
* :guilabel:`Synchronize` on shared repository now operates on all its components.
* :ref:`check-punctuation-spacing` ignores markup such as Markdown or reStructuredText.
* :ref:`autofix-punctuation-spacing` does not alter reStructuredText markup.

**Bug fixes**

Expand Down
14 changes: 13 additions & 1 deletion weblate/trans/autofixes/chars.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
PunctuationSpacingCheck,
ZeroWidthSpaceCheck,
)
from weblate.checks.same import RST_MATCH
from weblate.formats.helpers import CONTROLCHARS_TRANS
from weblate.trans.autofixes.base import AutoFix

Expand Down Expand Up @@ -106,13 +107,24 @@ def get_related_checks():
def fix_single_target(
self, target: str, source: str, unit: Unit
) -> tuple[str, bool]:
def spacing_replace(matchobj: re.Match) -> str:
if "rst-text" in unit.all_flags:
offset = matchobj.start(2)
rst_position = RST_MATCH.search(target, offset)
if rst_position is not None and rst_position.start(0) == offset:
# Skip escaping inside rst tag
return matchobj.group(0)
return f"\u00a0{matchobj.group(2)}"

if (
unit.translation.language.is_base(("fr", "br"))
and unit.translation.language.code != "fr_CA"
and "ignore-punctuation-spacing" not in unit.all_flags
):
# Fix existing
new_target = re.sub(FRENCH_PUNCTUATION_FIXUP_RE_NBSP, "\u00a0\\2", target)
new_target = re.sub(
FRENCH_PUNCTUATION_FIXUP_RE_NBSP, spacing_replace, target
)
new_target = re.sub(
FRENCH_PUNCTUATION_FIXUP_RE_NNBSP, "\u202f\\2", new_target
)
Expand Down
11 changes: 11 additions & 0 deletions weblate/trans/tests/test_autofix.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,14 @@ def test_punctuation_spacing(self) -> None:
self.assertEqual(fix.fix_target(["Bar:"], fr_unit), (["Bar:"], False))
self.assertEqual(fix.fix_target(["Bar:"], fr_ca_unit), (["Bar:"], False))
self.assertEqual(fix.fix_target(["Bar:"], cs_unit), (["Bar:"], False))

def test_punctuation_spacing_rst(self) -> None:
fix = PunctuationSpacing()
fr_rst_unit = MockUnit(source="This :ref:`doc`", code="fr", flags="rst-text")
self.assertEqual(
fix.fix_target(["This :ref:`doc`"], fr_rst_unit),
(["This :ref:`doc`"], False),
)
self.assertEqual(
fix.fix_target(["This :"], fr_rst_unit), (["This\u00a0:"], True)
)

0 comments on commit 20367c0

Please sign in to comment.