Skip to content

Commit

Permalink
Merge pull request #304 from xxyzz/nowiki_external_link
Browse files Browse the repository at this point in the history
Only parse external link as text if `<nowiki/>` directly after `[`
  • Loading branch information
xxyzz authored Sep 11, 2024
2 parents a107042 + cd183df commit 4b37709
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
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 /> ")


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

0 comments on commit 4b37709

Please sign in to comment.