Skip to content

Commit

Permalink
Merge pull request #290 from xxyzz/rel2abs
Browse files Browse the repository at this point in the history
Implement `#rel2abs` parser function
  • Loading branch information
xxyzz committed Jun 19, 2024
2 parents 664a3bc + 40b65f5 commit 0136956
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/wikitextprocessor/parserfns.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import urllib.parse
from collections.abc import Callable, Sequence
from datetime import datetime, timezone
from pathlib import Path
from typing import TYPE_CHECKING, Optional, Union

import dateparser
Expand Down Expand Up @@ -1623,6 +1624,26 @@ def number_of_articles_fn(
return str(wtp.saved_page_nums([0], False))


def rel2abs_fn(
wtp: "Wtp", fn_name: str, args: list[str], expander: Callable[[str], str]
) -> str:
# https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##rel2abs
# https://github.com/wikimedia/mediawiki-extensions-ParserFunctions/blob/ea4d4d94ee0c55b6039e05650ccc322e106ae06b/includes/ParserFunctions.php#L319
original_path_str = args[0].strip()
path = Path(original_path_str.removeprefix("/"))
base_path = Path("/" + (wtp.title or ""))
if len(args) > 1:
base_path = Path("/" + args[1].strip())
# not relative path
if (
not original_path_str.startswith(("/", "./", "../"))
and original_path_str != ".."
):
base_path = Path("/")
path = base_path / path
return str(path.resolve()).removeprefix("/")


# 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 @@ -1733,7 +1754,7 @@ def number_of_articles_fn(
"anchorencode": anchorencode_fn,
"ns": ns_fn,
"nse": ns_fn, # We don't have spaces in ns names
"#rel2abs": unimplemented_fn,
"#rel2abs": rel2abs_fn,
"#titleparts": titleparts_fn,
"#expr": expr_fn,
"#if": if_fn,
Expand Down
27 changes: 27 additions & 0 deletions tests/test_parserfns.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,30 @@ def test_timel(self):
time = datetime.fromisoformat(expanded)
delta = datetime.now(timezone.utc) - time
self.assertLess(abs(delta.total_seconds()), 1)

def test_rel2abs(self):
# https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##rel2abs
self.wtp.start_page("test")
test_cases = (
(
"{{#rel2abs: /quok | Help:Foo/bar/baz }}",
"Help:Foo/bar/baz/quok",
),
(
"{{#rel2abs: ./quok | Help:Foo/bar/baz }}",
"Help:Foo/bar/baz/quok",
),
("{{#rel2abs: ../quok | Help:Foo/bar/baz }}", "Help:Foo/bar/quok"),
("{{#rel2abs: ../. | Help:Foo/bar/baz }}", "Help:Foo/bar"),
(
"{{#rel2abs: ../quok/. | Help:Foo/bar/baz }}",
"Help:Foo/bar/quok",
),
("{{#rel2abs: ../../quok | Help:Foo/bar/baz }}", "Help:Foo/quok"),
("{{#rel2abs: ../../../quok | Help:Foo/bar/baz }}", "quok"),
("{{#rel2abs: b }}", "b"),
("{{#rel2abs: /b }}", "test/b"),
)
for wikitext, result in test_cases:
with self.subTest(wikitext=wikitext, result=result):
self.assertEqual(self.wtp.expand(wikitext), result)

0 comments on commit 0136956

Please sign in to comment.