diff --git a/CHANGES.md b/CHANGES.md index 42df2692..2dbd4712 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,7 @@ ## python-markdown2 2.4.13 (not yet released) - [pull #559] Allow cuddled tables (#557) +- [pull #560] Fix `markdown-in-html` not always splitting HTML tags into separate lines (#558) ## python-markdown2 2.4.12 diff --git a/lib/markdown2.py b/lib/markdown2.py index c6614db9..d92b77b6 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -800,13 +800,17 @@ def _hash_html_block_sub(self, match, raw=False): m = self._html_markdown_attr_re.search(first_line) if m: lines = html.split('\n') - if len(lines) < 3: # if MD is on same line as HTML - lines = re.split(r'(<%s.*markdown=.*?>)' % tag, lines[0])[1:] + lines[1:] - first_line = lines[0] - lines = lines[:-1] + re.split(r'(.*?$)' % tag, lines[-1])[:-1] + # if MD is on same line as opening tag then split across two lines + lines = list(filter(None, (re.split(r'(<%s.*markdown=.*?>)' % tag, lines[0])))) + lines[1:] + # if MD on same line as closing tag, split across two lines + lines = lines[:-1] + list(filter(None, re.split(r'(.*?$)' % tag, lines[-1]))) + # extract key sections of the match + first_line = lines[0] middle = '\n'.join(lines[1:-1]) last_line = lines[-1] + # remove `markdown="1"` attr from tag first_line = first_line[:m.start()] + first_line[m.end():] + # hash the HTML segments to protect them f_key = _hash_text(first_line) self.html_blocks[f_key] = first_line l_key = _hash_text(last_line) diff --git a/test/tm-cases/markdown_in_html_on_same_line.html b/test/tm-cases/markdown_in_html_on_same_line.html index 658e944b..2c220974 100644 --- a/test/tm-cases/markdown_in_html_on_same_line.html +++ b/test/tm-cases/markdown_in_html_on_same_line.html @@ -21,3 +21,20 @@

text

+ +

+ +

text +text +text

+ +

+ +

+ +

text +text +text +text

+ +

diff --git a/test/tm-cases/markdown_in_html_on_same_line.text b/test/tm-cases/markdown_in_html_on_same_line.text index 09de8a4a..80143618 100644 --- a/test/tm-cases/markdown_in_html_on_same_line.text +++ b/test/tm-cases/markdown_in_html_on_same_line.text @@ -10,3 +10,13 @@

**text**

+ +

**text** +**text** +**text** +

+ +

**text** +**text** +**text** +**text**