From 535ee20ee3f1d919d91ccad371ec1adb397c7933 Mon Sep 17 00:00:00 2001
From: 5j9 <5j9@users.noreply.github.com>
Date: Thu, 21 Dec 2023 12:08:41 +0330
Subject: [PATCH] fix(_shadow): use _content_span to strip head and tail
fixes #126
---
CHANGELOG.rst | 1 +
tests/wikitext/test_get_bolds_and_italics.py | 66 +++++++++++---------
tests/wikitext/test_plain_text.py | 13 ++++
wikitextparser/_comment_bold_italic.py | 4 +-
wikitextparser/_parameter.py | 4 +-
wikitextparser/_parser_function.py | 10 +--
wikitextparser/_tag.py | 7 ++-
wikitextparser/_template.py | 4 ++
wikitextparser/_wikilink.py | 13 ++--
wikitextparser/_wikitext.py | 21 ++++---
10 files changed, 90 insertions(+), 53 deletions(-)
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 65b199e..3cafa53 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -1,5 +1,6 @@
Unreleased
----------
+- Fixed a bug in ``plain_text``. (#126)
- Fixed another bug in parsing tables that end without a ``|}`` mark. (#125)
v0.55.6
diff --git a/tests/wikitext/test_get_bolds_and_italics.py b/tests/wikitext/test_get_bolds_and_italics.py
index 47776dd..7b0ff17 100644
--- a/tests/wikitext/test_get_bolds_and_italics.py
+++ b/tests/wikitext/test_get_bolds_and_italics.py
@@ -1,36 +1,42 @@
from wikitextparser import Bold, Italic, parse
+def assert_bold(
+ input_string: str, expected_bold_string: str, recursive: bool = True, /
+):
+ assert (
+ parse(input_string).get_bolds(recursive)[0].string
+ == expected_bold_string
+ )
+
+
+def assert_no_bold(input_string: str):
+ assert not parse(input_string).get_bolds(True)
+
+
def test_get_bolds():
- def ab(s: str, o: str, r: bool = True):
- assert parse(s).get_bolds(r)[0].string == o
-
- def anb(s: str):
- assert not parse(s).get_bolds(True)
-
- ab("A''''''''''B", "'''B")
- ab("''''''a''''''", "'''a''''") # 'a'
- ab("a''''''BI", "'''BI")
- ab("'''b'''", "'''b'''")
- anb("''i1'''s")
- anb("")
- ab(
+ assert_bold("A''''''''''B", "'''B")
+ assert_bold("''''''a''''''", "'''a''''") # 'a'
+ assert_bold("a''''''BI", "'''BI")
+ assert_bold("'''b'''", "'''b'''")
+ assert_no_bold("''i1'''s")
+ assert_no_bold("")
+ assert_bold(
"a'''" "b'''d",
"'''b'''",
)
- ab("'''b{{a|'''}}", "'''b{{a|'''}}") # ?
- ab("a'''b{{text|c|d}}e'''f", "'''b{{text|c|d}}e'''")
- ab("{{text|'''b'''}}", "'''b'''")
- ab("{{text|'''b}}", "'''b") # ?
- ab("[[a|'''b]] c", "'''b")
- ab("{{{PARAM|'''b}}} c", "'''b") # ?
+ assert_bold("'''b{{a|'''}}", "'''b{{a|'''}}") # ?
+ assert_bold("a'''b{{text|c|d}}e'''f", "'''b{{text|c|d}}e'''")
+ assert_bold("{{text|'''b'''}}", "'''b'''")
+ assert_bold("{{text|'''b}}", "'''b") # ?
+ assert_bold("{{{PARAM|'''b}}} c", "'''b") # ?
assert (
repr(parse("'''b\na'''c").get_bolds())
== """[Bold("'''b"), Bold("'''c")]"""
)
- ab("'''b'''", "'''b'''")
- ab("'''br'''c", "'''br'''")
- ab("'''''b'''i", "'''b'''")
+ assert_bold("'''b'''", "'''b'''")
+ assert_bold("'''br'''c", "'''br'''")
+ assert_bold("'''''b'''i", "'''b'''")
assert (
repr(parse("'''b[r'''c]a").get_bolds())
== """[Bold("'''b[r'''c]a"), Bold("'''c")]"""
@@ -39,12 +45,16 @@ def anb(s: str):
repr(parse("'''b[r'''c]a").get_bolds(False))
== """[Bold("'''b[r'''c]a")]"""
)
- ab("'''b{{{p|'''}}}", "'''b{{{p|'''}}}") # ?
- ab("'''a'''b", "'''a")
- anb("' ' ' a ' ' '")
- ab("x''' '''y", "''' '''")
- ab("x''''''y", "'''y")
- ab("{{text|{{text|'''b'''}}}}", "'''b'''")
+ assert_bold("'''b{{{p|'''}}}", "'''b{{{p|'''}}}") # ?
+ assert_bold("'''a'''b", "'''a")
+ assert_no_bold("' ' ' a ' ' '")
+ assert_bold("x''' '''y", "''' '''")
+ assert_bold("x''''''y", "'''y")
+ assert_bold("{{text|{{text|'''b'''}}}}", "'''b'''")
+
+
+def test_no_end_in_wikilink():
+ assert_bold("[[a|'''b]] c", "'''b")
def test_get_italics():
diff --git a/tests/wikitext/test_plain_text.py b/tests/wikitext/test_plain_text.py
index 566e471..8258cb6 100644
--- a/tests/wikitext/test_plain_text.py
+++ b/tests/wikitext/test_plain_text.py
@@ -217,3 +217,16 @@ def test_file_links():
# Fails for the following cases:
# assert parse('[[Media:Example.jpg]]').plain_text() == 'Media:Example.jpg'
# assert parse('[[Media:n.jpg|Sunflowers]]').plain_text() == 'Sunflowers'
+
+
+def test_tag_containing_comment_with_no_end(): # 126
+ parsed = parse(
+ """
+ [[a|b]]
+
+