Repository: Python-Markdown/markdown (python)

Issue:
admonition extension returns **invalid** html code if tab_length=2
```python
In [8]: import markdown
In [9]: some_text = """
   ...: !!! danger "Danger"
   ...:     don't try this at home.
   ...:
   ...: """
In [10]: markdown.markdown(some_text, tab_length=2, extensions=['admonition'])
Out[10]: '<div class="admonition danger">\n<p class="admonition-title">Danger<p>don\'t try this at home.</p>\n</p>\n</div>'
```


1. A paragraph inside a paragraph is **invalid** html and *etree* parser complains!
2. the simple solution of turning the title *paragraph* into a *div* will work, but it wraps the inner paragraph, resulting in a CSS mess.

Recommended working solution:

```diff
     CLASSNAME_TITLE = 'admonition-title'
+    CLASSNAME_BODY = 'admonition-body'
     RE = re.compile(r'(?:^|\n)!!! ?([\w\-]+(?: +[\w\-]+)*)(?: +"(.*?)")? *(?:\n|$)')

@@ -130,9 +134,11 @@ class AdmonitionProcessor(BlockProcessor):
             div = etree.SubElement(parent, 'div')
             div.set('class', '{} {}'.format(self.CLASSNAME, klass))
             if title:
-                p = etree.SubElement(div, 'p')
+                p = etree.SubElement(div, 'div')
                 p.text = title
                 p.set('class', self.CLASSNAME_TITLE)
+                p = etree.SubElement(div, 'div')
+                p.set('class', self.CLASSNAME_BODY)

```

> **Note:**   the `parse_content()` method of your admonition extension is an unreadable code-mess ;-)

Pull request:
Fix admonition corner case with indented code blocks
Fix a corner case in admonitions where if an indented code block was provided as the first block, the output would be malformed.

Fixes #1329

Patch:
diff --git a/docs/changelog.md b/docs/changelog.md
index d233abf..50e868c 100644
--- a/docs/changelog.md
+++ b/docs/changelog.md
@@ -28,6 +28,11 @@ links to the header, placing them after all other header content.
 * Update the list of empty HTML tags (#1353).
 * Add customizable TOC title class to TOC extension (#1293).
 
+### Fixed
+
+* Fix a corner case in admonitions where if an indented code block was provided
+  as the first block, the output would be malformed (#1329).
+
 ## [3.4.4] -- 2023-07-25
 
 ### Fixed
diff --git a/markdown/extensions/admonition.py b/markdown/extensions/admonition.py
index ce8492f..0d1250f 100644
--- a/markdown/extensions/admonition.py
+++ b/markdown/extensions/admonition.py
@@ -69,7 +69,7 @@ class AdmonitionProcessor(BlockProcessor):
 
         sibling = self.lastChild(parent)
 
-        if sibling is None or sibling.get('class', '').find(self.CLASSNAME) == -1:
+        if sibling is None or sibling.tag != 'div' or sibling.get('class', '').find(self.CLASSNAME) == -1:
             sibling = None
         else:
             # If the last child is a list and the content is sufficiently indented


Test patch:
diff --git a/tests/test_syntax/extensions/test_admonition.py b/tests/test_syntax/extensions/test_admonition.py
index 44c70d1..268633d 100644
--- a/tests/test_syntax/extensions/test_admonition.py
+++ b/tests/test_syntax/extensions/test_admonition.py
@@ -243,3 +243,23 @@ class TestAdmonition(TestCase):
             ),
             extensions=['admonition']
         )
+
+    def test_admonition_first_indented(self):
+        self.assertMarkdownRenders(
+            self.dedent(
+                '''
+                !!! danger "This is not"
+                        one long admonition title
+                '''
+            ),
+            self.dedent(
+                '''
+                <div class="admonition danger">
+                <p class="admonition-title">This is not</p>
+                <pre><code>one long admonition title
+                </code></pre>
+                </div>
+                '''
+            ),
+            extensions=['admonition']
+        )