Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only parse external link as text if <nowiki/> directly after [ #304

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/wikitextprocessor/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,11 @@ def repl_link(m: re.Match) -> CookieChar:
def repl_extlink(m: re.Match) -> CookieChar:
"""Replacement function for external links [...]. This is also
used to replace bracketed sections, such as [...]."""
nowiki = MAGIC_NOWIKI_CHAR in m.group(0)

# parse as text if <nowiki/> tag at the start
nowiki = (
re.match(r"\[\s*" + MAGIC_NOWIKI_CHAR, m.group(0)) is not None
)
orig = m.group(1)
if not orig.startswith(URL_STARTS):
return MAGIC_LBRACKET_CHAR + orig + MAGIC_RBRACKET_CHAR
Expand Down
15 changes: 15 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2947,6 +2947,21 @@ def test_nowiki_in_html_attr_value(self):
self.assertIsInstance(span_node, HTMLNode)
self.assertEqual(span_node.tag, "span")

def test_nowiki_tag_in_external_link(self):
# https://zh.wiktionary.org/wiki/Template:RQ:Qur'an
self.ctx.start_page("محمد")
root = self.ctx.parse("[https://quran.com/3/144 3<nowiki/>:144]")
url_node = root.children[0]
self.assertIsInstance(url_node, WikiNode)
self.assertEqual(url_node.kind, NodeKind.URL)
self.assertEqual(
url_node.largs, [["https://quran.com/3/144"], ["3<nowiki />:144"]]
)

root = self.ctx.parse("[ <nowiki/> https://quran.com/3/144 3:144]")
text_node = root.children[0]
self.assertEqual(text_node, "[ <nowiki /> ")

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have confirmed that this is correct behavior. In addition to this, doing [https://goo<nowiki/>gle.com test] in the sandbox results in [https://goo^gle.com test] as text with https://goo parsed as an URL, but it seems extremely unlikely that this kind of thing is done on purpose.


# XXX implement <nowiki/> marking for links, templates
# - https://en.wikipedia.org/wiki/Help:Wikitext#Nowiki
Expand Down