Skip to content

Commit

Permalink
chore(_balanced_quotes_shadow): rewrite to return spans
Browse files Browse the repository at this point in the history
This branch was intended to improves performance,
but my test results do not show meaningful enough performance improvements
to convince me to merge it into main branch.
  • Loading branch information
5j9 committed Apr 11, 2024
1 parent 2c761c5 commit c6a7b46
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 108 deletions.
45 changes: 29 additions & 16 deletions tests/wikitext/test_get_bolds_and_italics.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,10 @@ def test_get_bolds():
assert_bold("'''b'''", "'''b'''")
assert_no_bold("''i1'''s")
assert_no_bold("<!--'''b'''-->")
assert_bold(
"a<!---->'<!---->'<!---->'<!---->" "b<!---->'<!---->'<!---->'<!---->d",
"'<!---->'<!---->'<!---->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")]"""
)
assert_bold("'''<S>b</S>'''", "'''<S>b</S>'''")
assert_bold("'''b<S>r'''c</S>", "'''b<S>r'''")
assert_bold("'''''b'''i", "'''b'''")
Expand All @@ -53,19 +44,37 @@ def test_get_bolds():
assert_bold("{{text|{{text|'''b'''}}}}", "'''b'''")


def test_no_end_in_wikilink():
def test_hald_bolds_with_newline_in_between():
assert (
repr(parse("'''b\na'''c").get_bolds())
== """[Bold("'''b"), Bold("'''c")]"""
)


def test_half_bold_in_param():
assert_bold("{{{PARAM|'''b}}} c", "'''b") # ?


def test_half_bold_in_wikilink():
assert_bold("[[a|'''b]] c", "'''b")


def test_get_italics():
def ai(s: str, o: str, r: bool = True):
italics = parse(s).get_italics(r)
assert len(italics) == 1
assert italics[0].string == o
def test_comment_before_and_after_bold():
assert_bold(
"a<!---->'<!---->'<!---->'<!---->" "b<!---->'<!---->'<!---->'<!---->d",
"'<!---->'<!---->'<!---->b<!---->'<!---->'<!---->'",
)


def ai(s: str, o: str, r: bool = True):
italics = parse(s).get_italics(r)
assert len(italics) == 1
assert italics[0].string == o


def test_get_italics():
ai("''i'''", "''i'''")
ai("a''' '' b '' '''c", "'' b ''")
ai("'''''i'''''", "'''''i'''''")
ai("a'' ''' ib ''' ''c", "'' ''' ib ''' ''")
ai("''i''", "''i''")
ai(
Expand All @@ -81,6 +90,10 @@ def ai(s: str, o: str, r: bool = True):
ai("''' ''i'''", "''i'''")


def test_get_italics_2():
ai("'''''i'''''", "'''''i'''''")


def test_bold_italic_index_change():
p = parse("'''b1''' ''i1'' '''b2'''")
b1, b2 = p.get_bolds(recursive=False)
Expand Down
2 changes: 1 addition & 1 deletion wikitextparser/_comment_bold_italic.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def text(self, s: str):
self[b:e] = s

@property
def _content_span(self) -> Tuple[int, int]:
def _relative_content_span(self) -> Tuple[int, int]:
# noinspection PyUnresolvedReferences
return self._match.span(1)

Expand Down
2 changes: 1 addition & 1 deletion wikitextparser/_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ def parameters(self) -> List['Parameter']:
return super().parameters[1:]

@property
def _content_span(self) -> Tuple[int, int]:
def _relative_content_span(self) -> Tuple[int, int]:
return 3, -3
2 changes: 1 addition & 1 deletion wikitextparser/_parser_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SubWikiTextWithArgs(SubWikiText):
_first_arg_sep = 0

@property
def _content_span(self) -> Tuple[int, int]:
def _relative_content_span(self) -> Tuple[int, int]:
return 2, -2

@property
Expand Down
2 changes: 1 addition & 1 deletion wikitextparser/_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,6 @@ def get_tags(self, name=None) -> List['Tag']:
return super().get_tags(name)[1:]

@property
def _content_span(self) -> Tuple[int, int]:
def _relative_content_span(self) -> Tuple[int, int]:
s = self.string
return s.find('>') + 1, s.rfind('<')
2 changes: 1 addition & 1 deletion wikitextparser/_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Template(SubWikiTextWithArgs):
_first_arg_sep = 124

@property
def _content_span(self) -> Tuple[int, int]:
def _relative_content_span(self) -> Tuple[int, int]:
return 2, -2

def normal_name(
Expand Down
2 changes: 1 addition & 1 deletion wikitextparser/_wikilink.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class WikiLink(SubWikiText):
__slots__ = '_cached_match'

@property
def _content_span(self) -> Tuple[int, int]:
def _relative_content_span(self) -> Tuple[int, int]:
s = self.string
f = s.find
rf = s.rfind
Expand Down
Loading

0 comments on commit c6a7b46

Please sign in to comment.