Skip to content

Commit

Permalink
Account for code spans at start of line.
Browse files Browse the repository at this point in the history
Previously we didn't test for this because we did not allow raw blocks
to have any indent. Now, that we allow up to 3 spaces of indent, we need
to confirm those 3 chars are actually whitespace. Tests added.

Note: the case where the code span is not on the first line is still
failing. I havn't worked out why but ran out of time today.
  • Loading branch information
waylan committed Mar 26, 2019
1 parent 6dea522 commit 80668e3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
33 changes: 23 additions & 10 deletions markdown/htmlparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,30 @@ def close(self):
self.cleandoc.append(self.md.htmlStash.store(''.join(self._cache)))
self._cache = []

@property
def line_offset(self):
"""Returns char index in self.rawdata for the start of the current line. """
if self.lineno > 1:
return re.match(r'([^\n]*\n){{{}}}'.format(self.lineno-1), self.rawdata).end()
return 0

def at_line_start(self):
"""
Returns True if current position is at start of line.
Allows for up to three blank spaces at start of line.
"""
if self.offset == 0:
return True
if self.offset > 3:
return False
# Confirm up to first 3 chars are whitespace
return self.rawdata[self.line_offset:self.offset].strip() == ''

def handle_starttag(self, tag, attrs):
self.stack.append(tag)

line, col = self.getpos()
if col < 4 and self.md.is_block_level(tag) and not self.inraw:
if self.at_line_start() and self.md.is_block_level(tag) and not self.inraw:
# Started a new raw block
self.inraw = True
if len(self.cleandoc):
Expand All @@ -84,12 +103,7 @@ def handle_starttag(self, tag, attrs):

def handle_endtag(self, tag):
# Attempt to extract actual tag from raw source text
if self.lineno > 1:
# Find start position: char index for end of line at self.lineno + self.offset
start = re.match(r'([^\n]*\n){{{}}}'.format(self.lineno-1), self.rawdata).end() + self.offset
else:
# On first line. Just use self.offset for start position.
start = self.offset
start = self.line_offset + self.offset
m = parser.endendtag.search(self.rawdata, start)
if m:
text = self.rawdata[start:m.end()]
Expand Down Expand Up @@ -122,11 +136,10 @@ def handle_data(self, data):

def handle_empty_tag(self, data, is_block):
""" Handle empty tags (`<data>`). """
line, col = self.getpos()
if self.inraw:
# Append this to the existing raw block
self._cache.append(data)
elif col < 4 and is_block:
elif self.at_line_start() and is_block:
# Handle this as a standalone raw block
self.cleandoc.append(self.md.htmlStash.store(data))
# Insert blank line between this and next line.
Expand Down
29 changes: 27 additions & 2 deletions tests/test_syntax/blocks/test_html_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def test_raw_span(self):

def test_code_span(self):
self.assertMarkdownRenders(
'`<em>code span</em>`',
'<p><code>&lt;em&gt;code span&lt;/em&gt;</code></p>'
'`<p>code span</p>`',
'<p><code>&lt;p&gt;code span&lt;/p&gt;</code></p>'
)

def test_raw_empty(self):
Expand Down Expand Up @@ -227,6 +227,25 @@ def test_raw_surrounded_by_text_without_blank_lines(self):
)
)

# TODO: Fix this. Not sure why its failing...
def test_multiline_markdown_with_code_span(self):
self.assertMarkdownRenders(
self.dedent(
"""
A paragraph with a block-level
`<p>code span</p>`, which is
at the start of a line.
"""
),
self.dedent(
"""
<p>A paragraph with a block-level
<code>&lt;p&gt;code span&lt;/p&gt;</code>.
More <em>Markdown</em> text.</p>
"""
)
)

# Note: The blank line between the tags is a change in behavior.
def test_raw_one_line_followed_by_text(self):
self.assertMarkdownRenders(
Expand Down Expand Up @@ -645,6 +664,12 @@ def test_raw_comment_one_line_with_tag(self):
'<!-- <tag> -->'
)

def test_comment_in_code_span(self):
self.assertMarkdownRenders(
'`<!-- *foo* -->`',
'<p><code>&lt;!-- *foo* --&gt;</code></p>'
)

# Note: this is a change in behavior for Python-Markdown only in that a blank line is added.
# While it does not match the reference implementation, there is no difference in rendering.
def test_raw_comment_one_line_followed_by_text(self):
Expand Down

0 comments on commit 80668e3

Please sign in to comment.