From ded97d471a6dc2640108f5bb9522e1f632ff8bd3 Mon Sep 17 00:00:00 2001 From: Crozzers Date: Sun, 24 Dec 2023 16:21:09 +0000 Subject: [PATCH 1/2] Fix `markdown-in-html` not splitting HTML tags into separate lines for larger snippets --- lib/markdown2.py | 12 ++++++++---- .../tm-cases/markdown_in_html_on_same_line.html | 17 +++++++++++++++++ .../tm-cases/markdown_in_html_on_same_line.text | 10 ++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/markdown2.py b/lib/markdown2.py index f9cc64d3..7fc8d1aa 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**

From c119ef5bf90519cd855fb5215ff50630bec7686a Mon Sep 17 00:00:00 2001 From: Crozzers Date: Sun, 24 Dec 2023 16:22:58 +0000 Subject: [PATCH 2/2] Updte changelog --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 3bcc5955..21767bc9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,7 @@ ## python-markdown2 2.4.13 (not yet released) -(nothing yet) +- [pull #560] Fix `markdown-in-html` not always splitting HTML tags into separate lines (#558) ## python-markdown2 2.4.12