Skip to content

Commit f817f0b

Browse files
Merge pull request #547 from Crozzers/markdown-in-html-same-line
Update `markdown-in-html` extra to handle markdown on same line as HTML (#546)
2 parents f7b4535 + f5d0f40 commit f817f0b

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## python-markdown2 2.4.12 (not yet released)
44

5+
- [pull #547] Update `markdown-in-html` extra to handle markdown on same line as HTML (#546)
56
- [pull #550] Fix tables with trailing whitespace not being recognized (#549)
67

78

lib/markdown2.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,15 @@ def _detab(self, text):
783783
def _hash_html_block_sub(self, match, raw=False):
784784
if isinstance(match, str):
785785
html = match
786+
tag = None
786787
else:
787788
html = match.group(1)
789+
try:
790+
tag = match.group(2)
791+
except IndexError:
792+
tag = None
793+
794+
tag = tag or re.match(r'^<(\S).*?>', html).group(1)
788795

789796
if raw and self.safe_mode:
790797
html = self._sanitize_html(html)
@@ -793,6 +800,10 @@ def _hash_html_block_sub(self, match, raw=False):
793800
m = self._html_markdown_attr_re.search(first_line)
794801
if m:
795802
lines = html.split('\n')
803+
if len(lines) < 3: # if MD is on same line as HTML
804+
lines = re.split(r'(<%s.*markdown=.*?>)' % tag, lines[0])[1:] + lines[1:]
805+
first_line = lines[0]
806+
lines = lines[:-1] + re.split(r'(</%s>.*?$)' % tag, lines[-1])[:-1]
796807
middle = '\n'.join(lines[1:-1])
797808
last_line = lines[-1]
798809
first_line = first_line[:m.start()] + first_line[m.end():]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<p>
2+
3+
<p><strong>text</strong></p>
4+
5+
</p>
6+
7+
<p>
8+
9+
<p><strong>text</strong></p>
10+
11+
</p>
12+
13+
<p>
14+
15+
<p><strong>text</strong></p>
16+
17+
</p>
18+
19+
<p>
20+
21+
<p><strong>text</strong></p>
22+
23+
</p>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"extras": ["markdown-in-html"]}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<p markdown="1">**text**</p>
2+
3+
4+
<p markdown="1">
5+
**text**</p>
6+
7+
<p markdown="1">**text**
8+
</p>
9+
10+
<p markdown="1">
11+
**text**
12+
</p>

0 commit comments

Comments
 (0)