Skip to content

Commit be0530a

Browse files
committed
feat(checks): report uninteded list conversion in rst
Starting translation with a list marker will make docutils treat it as list what will either look weird or break the build.
1 parent eb6f67d commit be0530a

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

docs/changes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Weblate 5.15.2
99

1010
* :ref:`addon-weblate.generate.generate` is now triggered upon installation.
1111
* Screenshots updated from the repository have proper history.
12+
* :ref:`check-rst-syntax` now reports unintended list conversion.
1213

1314
.. rubric:: Bug fixes
1415

docs/locales/ja/LC_MESSAGES/docs.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48921,7 +48921,7 @@ msgstr ""
4892148921

4892248922
#: ../../security/passwords.rst:33
4892348923
msgid "Social or third-party authentication"
48924-
msgstr "- ソーシャル認証またはサードパーティ認証"
48924+
msgstr "ソーシャル認証またはサードパーティ認証"
4892548925

4892648926
#: ../../security/passwords.rst:35
4892748927
msgid ""

weblate/checks/markup.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@
114114
re.compile(r"""Interpreted text role "([^"]*)" not implemented\."""),
115115
]
116116

117+
RST_LIST_START = ("- ", "* ", "+ ")
118+
117119

118120
def strip_entities(text):
119121
"""Strip all HTML entities (we don't care about them)."""
@@ -577,7 +579,7 @@ def get_rst_publisher() -> Publisher:
577579
@lru_cache(maxsize=512)
578580
def validate_rst_snippet(
579581
snippet: str, source_tags: tuple[str] | None = None
580-
) -> tuple[list[str], list[str]]:
582+
) -> tuple[tuple[str, ...], tuple[str, ...]]:
581583
publisher = get_rst_publisher()
582584
document = utils.new_document("", publisher.settings)
583585

@@ -632,7 +634,7 @@ def error_collector(data: system_message) -> None:
632634
transform = transform_class(transformer.document, startnode=pending)
633635
transform.apply(**kwargs)
634636
transformer.applied.append((priority, transform_class, pending, kwargs))
635-
return errors, roles
637+
return tuple(errors), tuple(roles)
636638

637639

638640
class RSTSyntaxCheck(RSTBaseCheck):
@@ -644,7 +646,14 @@ def check_single(
644646
self, source: str, target: str, unit: Unit
645647
) -> bool | MissingExtraDict:
646648
_errors, source_roles = validate_rst_snippet(source)
647-
errors, _target_roles = validate_rst_snippet(target, tuple(source_roles))
649+
rst_errors, _target_roles = validate_rst_snippet(target, source_roles)
650+
errors = list(rst_errors)
651+
652+
# This is valid RST, but might mess up the document
653+
if not source.startswith(RST_LIST_START) and target.startswith(RST_LIST_START):
654+
errors.append(
655+
gettext("The translation should not start with a list marker.")
656+
)
648657

649658
if errors:
650659
return {"errors": errors}

weblate/checks/tests/test_markup_checks.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,3 +904,61 @@ def test_substitution(self) -> None:
904904
"rst-text",
905905
),
906906
)
907+
908+
def test_list(self) -> None:
909+
self.do_test(
910+
False,
911+
(
912+
"Text",
913+
"Text",
914+
"rst-text",
915+
),
916+
)
917+
self.do_test(
918+
True,
919+
(
920+
"Text",
921+
"* Text",
922+
"rst-text",
923+
),
924+
)
925+
self.do_test(
926+
True,
927+
(
928+
"Text",
929+
"- Text",
930+
"rst-text",
931+
),
932+
)
933+
self.do_test(
934+
True,
935+
(
936+
"Text",
937+
"+ Text",
938+
"rst-text",
939+
),
940+
)
941+
self.do_test(
942+
False,
943+
(
944+
"- Text",
945+
"- Text",
946+
"rst-text",
947+
),
948+
)
949+
self.do_test(
950+
False,
951+
(
952+
"- Text",
953+
"* Text",
954+
"rst-text",
955+
),
956+
)
957+
self.do_test(
958+
False,
959+
(
960+
"- Text",
961+
"+ Text",
962+
"rst-text",
963+
),
964+
)

0 commit comments

Comments
 (0)