Skip to content

Commit

Permalink
Expand templates in hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
roysmith committed Mar 7, 2023
1 parent a4518f1 commit 61e0b7e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
7 changes: 5 additions & 2 deletions dyk_tools/wiki/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,21 @@ def _render_nodes(self, site: Site, wikicode: Wikicode) -> Iterable[str]:
else:
logger.warning("Unknown node type (%s=%s)", type(node), node)

def targets(self) -> Iterable[str]:
def targets(self, site) -> Iterable[str]:
"""Iterates over the bolded links in a hook. In theory, a hook must
have at least one such hook, but nothing actually enforces that, so
it's possible for this to return an empty iterator.
Templates in the hook are explanded on ''site''.
Note that this returns the titles as strings, so:
"'''[[Foo]]'''" => ["Foo"]
"'''[[Foo#bar]]'''" => ["Foo#bar"]
"""
wikicode = parse(self.text)
expanded_text = site.expand_text(self.text)
wikicode = parse(expanded_text)
for node in wikicode.filter_wikilinks():
ancestors = wikicode.get_ancestors(node)
if ancestors:
Expand Down
2 changes: 1 addition & 1 deletion dyk_tools/wiki/hook_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def hooks(self) -> Iterable[Hook]:

def targets(self) -> Iterable[Page]:
for hook in self.hooks():
for title in hook.targets():
for title in hook.targets(self.page.site):
yield Page(self.page.site, title)

@staticmethod
Expand Down
21 changes: 20 additions & 1 deletion tests/wiki/test_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,27 @@ class TestTargets:
("'''[[Article#section|pipe]]''',", ["Article#section"]),
("'''[[Art? icle]]'''", ["Art? icle"]),
("'''[[Talk:Foo]]'''", ["Talk:Foo"]),
("'''[[Foo|Bar]]'''", ["Foo"]),
],
)
def test_finds_targets(self, site, input, result):
site.expand_text.side_effect = lambda s: s
hook = Hook(input)
targets = hook.targets(site)
assert list(targets) == result

@pytest.mark.parametrize(
"input, result",
[
("'''{{HMS|Melpomene|1794|6}}'''", ["HMS Melpomene (1794)"]),
],
)
def test_expands_templates(self, site, input, result):
site.expand_text.return_value = (
"'''[[HMS Melpomene (1794)|HMS ''Melpomene'']]'''"
)

hook = Hook(input)
assert list(hook.targets()) == result
targets = hook.targets(site)
l = list(targets)
assert l == result
3 changes: 2 additions & 1 deletion tests/wiki/test_hook_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_has_hooks_and_image(self, page):


class TestTargets:
def test_with_no_hooks_returns_no_targets(self, page):
def test_with_no_hooks_returns_no_targets(self, site, page):
page.text = dedent(
"""
<!--Hooks-->
Expand All @@ -106,6 +106,7 @@ def test_with_mutiple_hooks__returns_targets(self, mocker, page):
<!--HooksEnd-->
"""
)
page.site.expand_text.side_effect = lambda s: s
hook_set = HookSet(page)

targets = list(hook_set.targets())
Expand Down

0 comments on commit 61e0b7e

Please sign in to comment.