diff --git a/CHANGES.md b/CHANGES.md index d8008741..e880d4fe 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,7 @@ ## python-markdown2 2.4.12 (not yet released) +- [pull #547] Update `markdown-in-html` extra to handle markdown on same line as HTML (#546) - [pull #550] Fix tables with trailing whitespace not being recognized (#549) diff --git a/lib/markdown2.py b/lib/markdown2.py index 1ec7da20..6ea3901e 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -783,8 +783,15 @@ def _detab(self, text): def _hash_html_block_sub(self, match, raw=False): if isinstance(match, str): html = match + tag = None else: html = match.group(1) + try: + tag = match.group(2) + except IndexError: + tag = None + + tag = tag or re.match(r'^<(\S).*?>', html).group(1) if raw and self.safe_mode: html = self._sanitize_html(html) @@ -793,6 +800,10 @@ 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] middle = '\n'.join(lines[1:-1]) last_line = lines[-1] first_line = first_line[:m.start()] + first_line[m.end():] diff --git a/test/tm-cases/markdown_in_html_on_same_line.html b/test/tm-cases/markdown_in_html_on_same_line.html new file mode 100644 index 00000000..658e944b --- /dev/null +++ b/test/tm-cases/markdown_in_html_on_same_line.html @@ -0,0 +1,23 @@ +

+ +

text

+ +

+ +

+ +

text

+ +

+ +

+ +

text

+ +

+ +

+ +

text

+ +

diff --git a/test/tm-cases/markdown_in_html_on_same_line.opts b/test/tm-cases/markdown_in_html_on_same_line.opts new file mode 100644 index 00000000..25fea79f --- /dev/null +++ b/test/tm-cases/markdown_in_html_on_same_line.opts @@ -0,0 +1 @@ +{"extras": ["markdown-in-html"]} diff --git a/test/tm-cases/markdown_in_html_on_same_line.text b/test/tm-cases/markdown_in_html_on_same_line.text new file mode 100644 index 00000000..09de8a4a --- /dev/null +++ b/test/tm-cases/markdown_in_html_on_same_line.text @@ -0,0 +1,12 @@ +

**text**

+ + +

+**text**

+ +

**text** +

+ +

+**text** +