diff --git a/roles/web-app-docs/files/python/infinito_docs/conf.py b/roles/web-app-docs/files/python/infinito_docs/conf.py index 8aa9b06f7b..f52c52f6d7 100644 --- a/roles/web-app-docs/files/python/infinito_docs/conf.py +++ b/roles/web-app-docs/files/python/infinito_docs/conf.py @@ -56,6 +56,7 @@ extensions = [ "infinito_docs.extensions.markdown_include", "infinito_docs.extensions.parallel_postprocess", "infinito_docs.extensions.untranslated_yaml", + "infinito_docs.extensions.untranslated_markup", "infinito_docs.extensions.yaml_source", "infinito_docs.extensions.directory_index", "sphinx.ext.autodoc", diff --git a/roles/web-app-docs/files/python/infinito_docs/extensions/untranslated_markup.py b/roles/web-app-docs/files/python/infinito_docs/extensions/untranslated_markup.py new file mode 100644 index 0000000000..4b87521f9b --- /dev/null +++ b/roles/web-app-docs/files/python/infinito_docs/extensions/untranslated_markup.py @@ -0,0 +1,51 @@ +"""Keep a message that carries no word out of the translation catalogs. + +A table cell holding only ``[`e2e/`](e2e/)`` has nothing to translate, yet it +becomes a msgid like any sentence: 2999 of the docs domain's 25742 messages, +11.7 percent, are of that kind. They can never be filled, so they count as +pending for good and skew every completeness figure. + +The messages are taken from sphinx's own ``extract_messages``, which is what the +gettext builder collects, so the transform judges exactly the strings that would +otherwise reach the catalog. The predicate is ``has_words``, the one the +translation client already applies before it sends a batch, so both ends share +one definition of translatable. + +The documentation site builds arbitrary refs. A ref older than ``utils/i18n/`` +cannot supply the predicate, and the transform then does nothing, which leaves +that build exactly as it was before this extension existed. +""" + +from __future__ import annotations + +from docutils import nodes +from sphinx.transforms import SphinxTransform +from sphinx.util.nodes import extract_messages + +try: + from utils.i18n.placeholders import has_words +except ImportError: + has_words = None + +LOCALE_TRANSFORM_PRIORITY = 10 + + +class UntranslatedMarkup(SphinxTransform): + """Mark every extracted message without a word untranslatable.""" + + default_priority = LOCALE_TRANSFORM_PRIORITY - 5 + + def apply(self, **kwargs) -> None: + """Flag the nodes whose message is markup and punctuation only.""" + if has_words is None: + return + for node, message in extract_messages(self.document): + if isinstance(node, nodes.image) or not isinstance(node, nodes.Element): + continue + if not has_words(message): + node["translatable"] = False + + +def setup(app): + app.add_transform(UntranslatedMarkup) + return {"version": "1.0", "parallel_read_safe": True} diff --git a/tests/unit/python/roles/web-app-docs/files/infinito_docs/extensions/test_untranslated_markup.py b/tests/unit/python/roles/web-app-docs/files/infinito_docs/extensions/test_untranslated_markup.py new file mode 100644 index 0000000000..9ae3261cf5 --- /dev/null +++ b/tests/unit/python/roles/web-app-docs/files/infinito_docs/extensions/test_untranslated_markup.py @@ -0,0 +1,112 @@ +from __future__ import annotations + +import importlib +import sys +import unittest + +from docutils import nodes +from docutils.frontend import get_default_settings +from docutils.parsers.rst import Parser +from docutils.utils import new_document + +from . import PROJECT_ROOT + +_TOOLING = str(PROJECT_ROOT / "roles" / "web-app-docs" / "files" / "python") +if _TOOLING not in sys.path: + sys.path.insert(0, _TOOLING) + +untranslated_markup = importlib.import_module( + "infinito_docs.extensions.untranslated_markup" +) + +SOURCE = "/src/docs/testing.md" + + +def _paragraph(raw: str) -> nodes.paragraph: + node = nodes.paragraph(raw, "") + node += nodes.Text(raw) + node.source = SOURCE + node.line = 1 + return node + + +def _applied(*raws: str) -> list[nodes.paragraph]: + document = new_document(SOURCE, get_default_settings(Parser)) + paragraphs = [_paragraph(raw) for raw in raws] + for node in paragraphs: + document += node + untranslated_markup.UntranslatedMarkup(document).apply() + return paragraphs + + +class TestUntranslatedMarkup(unittest.TestCase): + def test_a_message_of_pure_markup_is_flagged(self) -> None: + (node,) = _applied("[`e2e/`](e2e/)") + + self.assertIs(node["translatable"], False) + + def test_a_sentence_around_the_same_link_stays_translatable(self) -> None: + (node,) = _applied("Run the tests under [`e2e/`](e2e/) first.") + + self.assertNotIn( + "translatable", + node, + "only the word-free messages may be withheld from the translator", + ) + + def test_a_lone_code_span_is_flagged(self) -> None: + (node,) = _applied("`CLEANUP_FORCE_KEEP`") + + self.assertIs(node["translatable"], False) + + def test_several_nodes_are_judged_one_by_one(self) -> None: + markup, prose = _applied("[`lint/`](lint/)", "Structural lint checks.") + + self.assertIs(markup["translatable"], False) + self.assertNotIn("translatable", prose) + + def test_the_markup_is_judged_and_not_the_rendered_text(self) -> None: + document = new_document(SOURCE, get_default_settings(Parser)) + node = nodes.paragraph("[`e2e/`](e2e/)", "") + reference = nodes.reference("", "", refuri="e2e/") + reference += nodes.literal("`e2e/`", "e2e/") + node += reference + node.source = SOURCE + node.line = 1 + document += node + + untranslated_markup.UntranslatedMarkup(document).apply() + + self.assertEqual(node.astext(), "e2e/") + self.assertIs( + node["translatable"], + False, + "sphinx extracts a paragraph's rawsource, so judging astext() would " + "read 'e2e/', find a word in it and let the markup through", + ) + + def test_it_runs_before_the_sphinx_locale_transform(self) -> None: + from sphinx.transforms.i18n import Locale + + self.assertLess( + untranslated_markup.UntranslatedMarkup.default_priority, + Locale.default_priority, + ) + + def test_a_checkout_without_the_predicate_changes_nothing(self) -> None: + original = untranslated_markup.has_words + untranslated_markup.has_words = None + self.addCleanup(setattr, untranslated_markup, "has_words", original) + + (node,) = _applied("[`e2e/`](e2e/)") + + self.assertNotIn( + "translatable", + node, + "the docs site builds arbitrary refs, and one predating utils/i18n " + "must still build", + ) + + +if __name__ == "__main__": + unittest.main()