Skip to content

Commit

Permalink
Merge pull request #250 from xxyzz/nowiki
Browse files Browse the repository at this point in the history
Fix misuse of `Match.start()` causes template expanded as text bug
  • Loading branch information
xxyzz committed Mar 11, 2024
2 parents 9645874 + 28d7737 commit 5f6ddbb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions src/wikitextprocessor/core.py
Expand Up @@ -660,10 +660,9 @@ def repl_templ(m: re.Match) -> Union[CookieChar, str]:
functions."""
whole_match = m.group(0)
nowiki = False
if (
MAGIC_NOWIKI_CHAR
in whole_match[: m.start(1)] + whole_match[m.end(1) :]
):
if whole_match.startswith(
"{" + MAGIC_NOWIKI_CHAR
) or whole_match.endswith(MAGIC_NOWIKI_CHAR + "}"):
nowiki = True # <nowiki/> inside `{{` or `}}`
args = vbar_split(m.group(1))
if len(args) == 0 or args[0] == "":
Expand All @@ -674,11 +673,13 @@ def repl_templ(m: re.Match) -> Union[CookieChar, str]:
+ "&vert;".join(args)
+ "&rbrace;&rbrace;"
)
if ":" not in args[0] and MAGIC_NOWIKI_CHAR in args[0]:
first_arg = args[0].strip()
if not first_arg.startswith("#") and MAGIC_NOWIKI_CHAR in args[0]:
nowiki = True # <nowiki/> before first pipe
if (
":" in args[0]
and MAGIC_NOWIKI_CHAR in args[0][: args[0].index(":")]
first_arg.startswith("#")
and ":" in first_arg
and MAGIC_NOWIKI_CHAR in first_arg[: first_arg.index(":")]
):
nowiki = True # <nowiki/> before parser function name

Expand Down
2 changes: 1 addition & 1 deletion tests/test_parser.py
Expand Up @@ -2843,7 +2843,7 @@ def test_nowiki_breaks_parsing_template(self):
"{{ t <nowiki/> }}",
"&lbrace;&lbrace; t <nowiki /> &rbrace;&rbrace;",
),
("{{ t | <nowiki/> }}", "template body"),
("random text {{ t | <nowiki/> }}", "random text template body"),
(
"{{ #ifeq<nowiki/>: inYes | inYes | outYes | outNo }}",
"&lbrace;&lbrace; #ifeq<nowiki />: inYes &vert; inYes &vert; outYes &vert; outNo &rbrace;&rbrace;", # noqa: E501
Expand Down

0 comments on commit 5f6ddbb

Please sign in to comment.