Skip to content

Commit 54a9dd2

Browse files
Merge pull request #569 from Crozzers/process-escaped-html-comments
Process HTML comments as markdown in 'escape' safe mode
2 parents 796e57f + e5e73ca commit 54a9dd2

5 files changed

+27
-4
lines changed

Diff for: CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [pull #519] Add support for custom extras
66
- [pull #519] Drop Python 3.5 support
77
- [pull #568] Add `prepend` arg to toc extra (#397)
8+
- [pull #569] Process HTML comments as markdown in 'escape' safe mode
89
- [pull #570] Fix syntax warnings in test suite
910

1011

Diff for: lib/markdown2.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -1264,15 +1264,30 @@ def _is_code_span(index, token):
12641264

12651265
return re.match(r'<code>md5-[A-Fa-f0-9]{32}</code>', ''.join(peek_tokens))
12661266

1267+
def _is_comment(token):
1268+
if self.safe_mode == 'replace':
1269+
# don't bother processing each section of comment in replace mode. Just do the whole thing
1270+
return
1271+
return re.match(r'(<!--)(.*)(-->)', token)
1272+
1273+
def _hash(token):
1274+
key = _hash_text(token)
1275+
self.html_spans[key] = token
1276+
return key
1277+
12671278
tokens = []
12681279
split_tokens = self._sorta_html_tokenize_re.split(text)
12691280
is_html_markup = False
12701281
for index, token in enumerate(split_tokens):
12711282
if is_html_markup and not _is_auto_link(token) and not _is_code_span(index, token):
1272-
sanitized = self._sanitize_html(token)
1273-
key = _hash_text(sanitized)
1274-
self.html_spans[key] = sanitized
1275-
tokens.append(key)
1283+
is_comment = _is_comment(token)
1284+
if is_comment:
1285+
tokens.append(_hash(self._sanitize_html(is_comment.group(1))))
1286+
# sanitise but leave comment body intact for further markdown processing
1287+
tokens.append(self._sanitize_html(is_comment.group(2)))
1288+
tokens.append(_hash(self._sanitize_html(is_comment.group(3))))
1289+
else:
1290+
tokens.append(_hash(self._sanitize_html(token)))
12761291
else:
12771292
tokens.append(self._encode_incomplete_tags(token))
12781293
is_html_markup = not is_html_markup

Diff for: test/tm-cases/escape_html_comments_safe_mode.html

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p><em>foo</em> &lt;!-- <em>bar</em></p>
2+
3+
<p><em>foo</em> &lt;!-- <em>bar</em> --&gt;</p>

Diff for: test/tm-cases/escape_html_comments_safe_mode.opts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{'safe_mode': 'escape'}

Diff for: test/tm-cases/escape_html_comments_safe_mode.text

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*foo* <!-- *bar*
2+
3+
*foo* <!-- *bar* -->

0 commit comments

Comments
 (0)