Skip to content

Commit

Permalink
Fixed failing examples 415 and 416 in the CommonMark 0.30 spec: empha…
Browse files Browse the repository at this point in the history
…sis delimiter lengths being multiples of 3. (#108)

Solved by adding a missing condition in Delimiter.closed_by().
Also refactored the function to make it more readable.
  • Loading branch information
anderskaplan committed Dec 18, 2022
1 parent 5b43db6 commit 8c90f9d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
14 changes: 10 additions & 4 deletions mistletoe/core_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,16 @@ def remove(self, n, left=True):
return True

def closed_by(self, other):
return not (self.type[0] != other.type[0]
or (self.open and self.close or other.open and other.close)
and (self.number + other.number) % 3 == 0)

if self.type[0] != other.type[0]:
return False
if self.open and self.close or other.open and other.close:
# if either of the delimiters can both open and close emphasis, then additional
# restrictions apply: the sum of the lengths of the delimiter runs
# containing the opening and closing delimiters must not be a multiple of 3
# unless both lengths are multiples of 3.
return ((self.number + other.number) % 3 != 0
or (self.number % 3 == 0 and other.number % 3 == 0))
return True

def __repr__(self):
if not self.type.startswith(('*', '_')):
Expand Down
5 changes: 5 additions & 0 deletions test/test_span_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def test_emphasis_for_asterisk_without_punctuation(self):
self._test_token(next(tokens), 'an example without', children=True)
self._test_token(next(tokens), 'punctuation', children=False)

def test_emphasis_when_both_delimiter_run_lengths_are_multiples_of_3(self):
tokens = iter(span_token.tokenize_inner('foo******bar*********baz'))
self._test_token(next(tokens), 'foo', children=False)
self._test_token(next(tokens), 'bar', children=True)
self._test_token(next(tokens), '***baz', children=False)

class TestInlineCode(TestBranchToken):
def _test_parse_enclosed(self, encl_type, encl_delimiter):
Expand Down

0 comments on commit 8c90f9d

Please sign in to comment.