Skip to content

Commit

Permalink
Ensure admonition content is detabbed properly
Browse files Browse the repository at this point in the history
  • Loading branch information
facelessuser committed Feb 5, 2021
1 parent 49e1d29 commit 0e6dc4c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/change_log/release-3.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ The following bug fixes are included in the 3.3 release:
* Fix unescaping of HTML characters `<>` in CodeHilite (#990).
* Fix complex scenarios involving lists and admonitions (#1004).
* Fix complex scenarios with nested ordered and unordered lists in a definition list (#918).
* Fix corner cases with lists under admonitions.

[spec]: https://www.w3.org/TR/html5/text-level-semantics.html#the-code-element
[fenced_code]: ../extensions/fenced_code_blocks.md
Expand Down
8 changes: 5 additions & 3 deletions markdown/blockprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ def lastChild(self, parent):
else:
return None

def detab(self, text):
def detab(self, text, length=None):
""" Remove a tab from the front of each line of the given text. """
if length is None:
length = self.tab_length
newtext = []
lines = text.split('\n')
for line in lines:
if line.startswith(' '*self.tab_length):
newtext.append(line[self.tab_length:])
if line.startswith(' ' * length):
newtext.append(line[length:])
elif not line.strip():
newtext.append('')
else:
Expand Down
20 changes: 12 additions & 8 deletions markdown/extensions/admonition.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,24 @@ def __init__(self, parser):
self.current_sibling = None
self.content_indention = 0

def get_sibling(self, parent, block):
def parse_content(self, parent, block):
"""Get sibling admontion.
Retrieve the appropriate siblimg element. This can get trickly when
dealing with lists.
"""

old_block = block
the_rest = ''

# We already acquired the block via test
if self.current_sibling is not None:
sibling = self.current_sibling
block = block[self.content_indent:]
block, the_rest = self.detab(block, self.content_indent)
self.current_sibling = None
self.content_indent = 0
return sibling, block
return sibling, block, the_rest

sibling = self.lastChild(parent)

Expand Down Expand Up @@ -96,17 +99,19 @@ def get_sibling(self, parent, block):
sibling = None

if sibling is not None:
indent += self.tab_length
block, the_rest = self.detab(old_block, indent)
self.current_sibling = sibling
self.content_indent = indent

return sibling, block
return sibling, block, the_rest

def test(self, parent, block):

if self.RE.search(block):
return True
else:
return self.get_sibling(parent, block)[0] is not None
return self.parse_content(parent, block)[0] is not None

def run(self, parent, blocks):
block = blocks.pop(0)
Expand All @@ -116,10 +121,9 @@ def run(self, parent, blocks):
if m.start() > 0:
self.parser.parseBlocks(parent, [block[:m.start()]])
block = block[m.end():] # removes the first line
block, theRest = self.detab(block)
else:
sibling, block = self.get_sibling(parent, block)

block, theRest = self.detab(block)
sibling, block, theRest = self.parse_content(parent, block)

if m:
klass, title = self.get_class_and_title(m)
Expand Down
30 changes: 30 additions & 0 deletions tests/test_syntax/extensions/test_admonition.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,33 @@ def test_with_preceding_text(self):
),
extensions=['admonition']
)

def test_admontion_detabbing(self):
self.assertMarkdownRenders(
self.dedent(
'''
!!! note "Admonition"
- Parent 1
- Child 1
- Child 2
'''
),
self.dedent(
'''
<div class="admonition note">
<p class="admonition-title">Admonition</p>
<ul>
<li>
<p>Parent 1</p>
<ul>
<li>Child 1</li>
<li>Child 2</li>
</ul>
</li>
</ul>
</div>
'''
),
extensions=['admonition']
)

0 comments on commit 0e6dc4c

Please sign in to comment.