diff options
author | Isaac Muse <faceless.shop@gmail.com> | 2021-02-05 13:25:45 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-05 15:25:45 -0500 |
commit | 0e6dc4c4f5dc8d6691eb95549657034f6d9abcb6 (patch) | |
tree | dcbc44dc186537d388a65335c568fe6ae7b14f1a | |
parent | 49e1d29d791926229e2bbafd8132666d74d14883 (diff) | |
download | python-markdown-0e6dc4c4f5dc8d6691eb95549657034f6d9abcb6.tar.gz |
Ensure admonition content is detabbed properly
-rw-r--r-- | docs/change_log/release-3.3.md | 1 | ||||
-rw-r--r-- | markdown/blockprocessors.py | 8 | ||||
-rw-r--r-- | markdown/extensions/admonition.py | 20 | ||||
-rw-r--r-- | tests/test_syntax/extensions/test_admonition.py | 30 |
4 files changed, 48 insertions, 11 deletions
diff --git a/docs/change_log/release-3.3.md b/docs/change_log/release-3.3.md index 79e22b2..12273bc 100644 --- a/docs/change_log/release-3.3.md +++ b/docs/change_log/release-3.3.md @@ -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 diff --git a/markdown/blockprocessors.py b/markdown/blockprocessors.py index 7d31a7f..8518e50 100644 --- a/markdown/blockprocessors.py +++ b/markdown/blockprocessors.py @@ -78,13 +78,15 @@ class BlockProcessor: 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: diff --git a/markdown/extensions/admonition.py b/markdown/extensions/admonition.py index 916d931..9c66b4f 100644 --- a/markdown/extensions/admonition.py +++ b/markdown/extensions/admonition.py @@ -48,7 +48,7 @@ class AdmonitionProcessor(BlockProcessor): 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 @@ -56,13 +56,16 @@ class AdmonitionProcessor(BlockProcessor): """ + 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) @@ -96,17 +99,19 @@ class AdmonitionProcessor(BlockProcessor): 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) @@ -116,10 +121,9 @@ class AdmonitionProcessor(BlockProcessor): 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) diff --git a/tests/test_syntax/extensions/test_admonition.py b/tests/test_syntax/extensions/test_admonition.py index b8d4f06..44c70d1 100644 --- a/tests/test_syntax/extensions/test_admonition.py +++ b/tests/test_syntax/extensions/test_admonition.py @@ -213,3 +213,33 @@ class TestAdmonition(TestCase): ), 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'] + ) |