Skip to content

pull_request(list): prevent extra p tags inside list items #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 22 additions & 7 deletions mkdcomments.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@
class CommentsExtension(Extension):
def extendMarkdown(self, md):
md.registerExtension(self)
md.preprocessors.register(
CommentMunger(md), "comment_munger", 21)
md.preprocessors.register(
CommentRemover(md), "comment_remover", 19)
md.postprocessors.register(
RawCommentReplacer(md), "raw_comment_replacer", 29)
md.preprocessors.register(CommentMunger(md), "comment_munger", 21)
md.preprocessors.register(CommentRemover(md), "comment_remover", 19)
md.postprocessors.register(RawCommentReplacer(md), "raw_comment_replacer", 29)


class CommentMunger(Preprocessor):
Expand Down Expand Up @@ -58,7 +55,25 @@ def _unmultiliner(self, line):

class RawCommentReplacer(Postprocessor):
def run(self, text):
return re.sub(PREFIX_PLACEHOLDER, '<!---', text)
# Restore the placeholder
text = re.sub(PREFIX_PLACEHOLDER, '<!---', text)

# Remove unwanted <p> tags around list items.
# See issue #8 for details.
text = re.sub(r'<li>\s*<p>(.*?)</p>\s*</li>', r'<li>\1</li>', text)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per discussion in #8, the thing that makes me uncomfortable with this solution is that I believe this will "correct" undesirable output caused by blank lines and not just by comments. IMO that would not be a great side-effect because it would encourage people to write bad markdown by hiding their mistakes from them.


# Preserve comments inside <pre> tags
text = self._preserve_comments_in_pre(text)

return text

def _preserve_comments_in_pre(self, text):
"""Preserve comments within <pre> tags."""
return re.sub(r'(<pre>.*?</pre>)', self._replace_placeholder, text, flags=re.DOTALL)

def _replace_placeholder(self, match):
"""Replace the placeholder back to HTML comment."""
return match.group(0).replace(PREFIX_PLACEHOLDER, '<!---')


def makeExtension(*args, **kwargs):
Expand Down
57 changes: 56 additions & 1 deletion test/test_comments.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import re
import pathlib
import unittest
import textwrap
Expand Down Expand Up @@ -75,6 +74,62 @@ def test_multiline_ending_and_beginning_on_same_line(self):
<p>more text</p>"""
self.assertExpectedMarkdown(md_input, expected_result)

def test_comments_between_list_items(self):
"""Test the case where a comment between items inside the list.

See issue #8 for details."""
md_input = """\
1. Kira first
<!--- Kira comment -->
1. Kira second
1. Kira third
"""
expected_result = """\
<ol>
<li>Kira first</li>
<li>Kira second</li>
<li>Kira third</li>
</ol>"""
self.assertExpectedMarkdown(md_input, expected_result)

def test_comments_between_list_items_with_blank_lines(self):
"""Test the case where a comment and blank lines between items inside the list."""
md_input = """\
1. Kira first

<!--- Kira comment -->

1. Kira second
1. Kira third
"""
expected_result = """\
<ol>
<li>Kira first</li>
<li>Kira second</li>
<li>Kira third</li>
</ol>"""
self.assertExpectedMarkdown(md_input, expected_result)

def test_multiline_comments_between_list_items(self):
"""Test the case where a multiline comment and blank lines between the items inside the list."""
md_input = """\
1. Kira first

<!--- Kira
multiline
comment -->

1. Kira second
1. Kira third
"""
expected_result = """\
<ol>
<li>Kira first</li>
<li>Kira second</li>
<li>Kira third</li>
</ol>"""
self.assertExpectedMarkdown(md_input, expected_result)

def test_comments_in_html(self):
""" See issue #2. """
md_input = """\
Expand Down