Coverage for app/venv/lib/python3.14/site-packages/weblate/trans/autofixes/whitespace.py: 38%
31 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
1# Copyright © Michal Čihař <michal@weblate.org>
2#
3# SPDX-License-Identifier: GPL-3.0-or-later
5import re
7from django.utils.translation import gettext_lazy
9from weblate.checks.chars import BeginSpaceCheck, EndSpaceCheck
10from weblate.trans.autofixes.base import AutoFix
12NEWLINES = re.compile(r"\r\n|\r|\n")
13START = re.compile(r"^(\s+)")
14END = re.compile(r"(\s+)$")
17class SameBookendingWhitespace(AutoFix):
18 """Help non-techy translators with their whitespace."""
20 fix_id = "end-whitespace"
21 name = gettext_lazy("Trailing and leading whitespace")
23 @staticmethod
24 def get_related_checks():
25 return [BeginSpaceCheck(), EndSpaceCheck()]
27 def fix_single_target(self, target, source, unit):
28 # normalize newlines of source
29 source = NEWLINES.sub("\n", source)
31 flags = unit.all_flags
32 stripped = target
34 # Capture and strip leading space
35 if "ignore-begin-space" in flags:
36 head = ""
37 else:
38 start = START.search(source)
39 head = start.group() if start else ""
40 stripped = stripped.lstrip()
42 # Capture and strip trailing space
43 if "ignore-end-space" in flags:
44 tail = ""
45 else:
46 end = END.search(source)
47 tail = end.group() if end else ""
48 stripped = stripped.rstrip()
50 # add the whitespace around the target translation (ignore blanks)
51 if stripped:
52 newtarget = head + stripped + tail
53 return newtarget, newtarget != target
54 return target, False