Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,11 @@ def _detab(self, text):
_html_markdown_attr_re = re.compile(
r'''\s+markdown=("1"|'1')''')
def _hash_html_block_sub(self, match, raw=False):
html = match.group(1)
if isinstance(match, str):
html = match
else:
html = match.group(1)

if raw and self.safe_mode:
html = self._sanitize_html(html)
elif 'markdown-in-html' in self.extras and 'markdown=' in html:
Expand Down Expand Up @@ -792,7 +796,7 @@ def _hash_html_blocks(self, text, raw=False):
# the inner nested divs must be indented.
# We need to do this before the next, more liberal match, because the next
# match will start at the first `<div>` and stop at the first `</div>`.
text = self._strict_tag_block_re.sub(hash_html_block_sub, text)
text = self._strict_tag_block_sub(text, self._block_tags_a, hash_html_block_sub)

# Now match more liberally, simply from `\n<tag>` to `</tag>\n`
text = self._liberal_tag_block_re.sub(hash_html_block_sub, text)
Expand Down Expand Up @@ -871,6 +875,39 @@ def _hash_html_blocks(self, text, raw=False):

return text

def _strict_tag_block_sub(self, text, html_tags_re, callback):
tag_count = 0
current_tag = html_tags_re
block = ''
result = ''

for chunk in text.splitlines(True):
is_markup = re.match(r'^(?:</code>(?=</pre>))?(</?(%s)\b>?)' % current_tag, chunk)
block += chunk

if is_markup:
if chunk.startswith('</'):
tag_count -= 1
else:
# if close tag is in same line
if '</%s>' % is_markup.group(2) in chunk[is_markup.end():]:
# we must ignore these
is_markup = None
else:
tag_count += 1
current_tag = is_markup.group(2)

if tag_count == 0:
if is_markup:
block = callback(block.rstrip('\n')) # remove trailing newline
current_tag = html_tags_re
result += block
block = ''

result += block

return result

def _strip_link_definitions(self, text):
# Strips link definitions from text, stores the URLs and titles in
# hash references.
Expand Down
7 changes: 7 additions & 0 deletions test/tm-cases/hash_nested_html_blocks.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="enclosing">
<div class="codehilite">
<pre><span></span><code><span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
</code></pre>
</div>

</div>
1 change: 1 addition & 0 deletions test/tm-cases/hash_nested_html_blocks.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"extras": ["fenced-code-blocks", "pygments"]}
1 change: 1 addition & 0 deletions test/tm-cases/hash_nested_html_blocks.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extra fenced-code-blocks pygments
5 changes: 5 additions & 0 deletions test/tm-cases/hash_nested_html_blocks.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="enclosing">
```python
x = 1
```
</div>
4 changes: 2 additions & 2 deletions test/tm-cases/sublist-para.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<li>Add Komodo chrome (XUL, JavaScript, CSS, DTDs).</li>
</ul>

<p><p>What this means is that work on and add significant functionality...</p></li>
<p>What this means is that work on and add significant functionality...</p></li>
<li><p>Komodo uses the same extension mechanisms as Firefox...</p></li>
<li><p>Komodo builds and runs on Windows, Linux and ...</p></li>
</ul></p>
</ul>