From 3abd45d0d79e5ba656e4b8685d4e3ded07346a2e Mon Sep 17 00:00:00 2001 From: xxyzz Date: Mon, 11 Dec 2023 17:07:50 +0800 Subject: [PATCH] Implement `PAGELANGUAGE` and `#language` magic words Used in page "Star Trek" and "Coriandrum sativum" but not required for extracted JSON file. --- src/wikitextprocessor/parserfns.py | 17 +++++++++++++++++ tests/test_parser.py | 9 ++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/wikitextprocessor/parserfns.py b/src/wikitextprocessor/parserfns.py index 45a9a17d..81453b6a 100644 --- a/src/wikitextprocessor/parserfns.py +++ b/src/wikitextprocessor/parserfns.py @@ -1485,6 +1485,21 @@ def statements_fn( return _query_wikidata_statement(prop, wikidata_item, ctx.lang_code) +def pagelanguage_fn( + ctx: "Wtp", fn_name: str, args: List[str], expander: Callable[[str], str] +) -> str: + return ctx.lang_code + + +def language_fn( + ctx: "Wtp", fn_name: str, args: List[str], expander: Callable[[str], str] +) -> str: + if len(args) > 0: + from mediawiki_langcodes import code_to_name + + return code_to_name(args[0], ctx.lang_code) + return "" + # This list should include names of predefined parser functions and # predefined variables (some of which can take arguments using the same # syntax as parser functions and we treat them as parser functions). @@ -1571,6 +1586,7 @@ def statements_fn( "DISPLAYTITLE": displaytitle_fn, "displaytitle": displaytitle_fn, "DEFAULTSORT": defaultsort_fn, + "PAGELANGUAGE": pagelanguage_fn, "lc": lc_fn, "lcfirst": lcfirst_fn, "uc": uc_fn, @@ -1635,6 +1651,7 @@ def statements_fn( "#Abschnitt-x": unimplemented_fn, "#trecho-x": unimplemented_fn, "#section-x": unimplemented_fn, + "#language": language_fn, } diff --git a/tests/test_parser.py b/tests/test_parser.py index 2cb34632..d7d2fd42 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -2582,7 +2582,6 @@ def test_equal_sign_in_template_argument(self): "(the ability/inability to achieve a result is expressed with various verb complements, e.g. ⦃⦃t+¦cmn¦得了¦tr=-deliǎo⦄⦄)", ) - def test_hdr_italics(self): tree = self.parse("test", "=== ''nachklassisch'' ===") self.assertEqual(len(tree.children), 1) @@ -2590,6 +2589,14 @@ def test_hdr_italics(self): self.assertEqual(len(tree.children[0].largs), 1) self.assertEqual(tree.children[0].largs[0][0].kind, NodeKind.ITALIC) + def test_language_parser_function(self): + self.ctx.start_page("") + self.assertEqual(self.ctx.expand("{{PAGELANGUAGE}}"), "en") + self.assertEqual( + self.ctx.expand("{{#language:{{PAGELANGUAGE}}}}"), "English" + ) + + # XXX implement marking for links, templates # - https://en.wikipedia.org/wiki/Help:Wikitext#Nowiki # - fix test_nowiki11 and continue