Skip to content

Commit

Permalink
Merge pull request tatuylonen#162 from xxyzz/language
Browse files Browse the repository at this point in the history
Implement `PAGELANGUAGE` and `#language` magic words
  • Loading branch information
xxyzz committed Dec 11, 2023
2 parents de7e291 + 3abd45d commit cb6e85f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/wikitextprocessor/parserfns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1635,6 +1651,7 @@ def statements_fn(
"#Abschnitt-x": unimplemented_fn,
"#trecho-x": unimplemented_fn,
"#section-x": unimplemented_fn,
"#language": language_fn,
}


Expand Down
9 changes: 8 additions & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2582,14 +2582,21 @@ 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)
self.assertEqual(tree.children[0].kind, NodeKind.LEVEL3)
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 <nowiki/> marking for links, templates
# - https://en.wikipedia.org/wiki/Help:Wikitext#Nowiki
# - fix test_nowiki11 and continue
Expand Down

0 comments on commit cb6e85f

Please sign in to comment.