Skip to content

Commit

Permalink
Merge branch 'master' into custom-extras
Browse files Browse the repository at this point in the history
Merge recent changes to `breaks` functionality
  • Loading branch information
Crozzers committed Jan 6, 2024
2 parents cc34905 + 1c7902b commit a9fb886
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

- [pull #559] Allow cuddled tables (#557)
- [pull #560] Fix `markdown-in-html` not always splitting HTML tags into separate lines (#558)
- [pull #564] Fix incomplete comments in safe mode not being escaped (#563)
- [pull #519] Add support for custom extras
- [pull #519] Drop Python 3.5 support


## python-markdown2 2.4.12

- [pull #547] Update `markdown-in-html` extra to handle markdown on same line as HTML (#546)
Expand Down
25 changes: 17 additions & 8 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,8 +1199,8 @@ def _run_span_gamut(self, text):

text = self._do_italics_and_bold(text)

# Do hard breaks:
text = re.sub(r" {2,}\n", " <br%s\n" % self.empty_element_suffix, text)
# Do hard breaks
text = re.sub(r" {2,}\n(?!\<(?:\/?(ul|ol|li))\>)", "<br%s\n" % self.empty_element_suffix, text)

return text

Expand Down Expand Up @@ -2247,7 +2247,7 @@ def _encode_amps_and_angles(self, text):
text = self._naked_gt_re.sub('&gt;', text)
return text

_incomplete_tags_re = re.compile(r"<(/?\w+?(?!\w)\s*?.+?[\s/]+?)")
_incomplete_tags_re = re.compile(r"<(!--|/?\w+?(?!\w)\s*?.+?[\s/]+?)")

def _encode_incomplete_tags(self, text):
if self.safe_mode not in ("replace", "escape"):
Expand Down Expand Up @@ -2592,12 +2592,21 @@ class Breaks(Extra):
order = Stage.after(Stage.ITALIC_AND_BOLD)

def run(self, text):
on_backslash = self.options.get('on_backslash', False)
on_newline = self.options.get('on_newline', False)

if on_backslash and on_newline:
pattern = r' *\\?'
elif on_backslash:
pattern = r'(?: *\\| {2,})'
elif on_newline:
pattern = r' *'
else:
pattern = r' {2,}'

break_tag = "<br%s\n" % self.md.empty_element_suffix
# do backslashes first because on_newline inserts the break before the newline
if self.options.get('on_backslash', False):
text = re.sub(r' *\\\n', break_tag, text)
if self.options.get('on_newline', False):
text = re.sub(r" *\n(?!\<(?:\/?(ul|ol|li))\>)", break_tag, text)
text = re.sub(pattern + r"\n(?!\<(?:\/?(ul|ol|li))\>)", break_tag, text)

return text


Expand Down
2 changes: 1 addition & 1 deletion test/tm-cases/admonitions.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<aside class="admonition important">
<strong>IMPORTANT</strong>
<p>You can also use 3 or more empty lines after an admonition <br />
<p>You can also use 3 or more empty lines after an admonition<br />
to end it</p>
</aside>

Expand Down
2 changes: 2 additions & 0 deletions test/tm-cases/basic_safe_mode_escape.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
<p>&lt;div&gt;yowzer!&lt;/div&gt;</p>

<p>blah</p>

<p><em>foo</em> &lt;!-- <em>bar</em></p>
3 changes: 3 additions & 0 deletions test/tm-cases/basic_safe_mode_escape.text
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ blah <img src="dangerous"> blah
<div>yowzer!</div>

blah


*foo* <!-- *bar*
3 changes: 2 additions & 1 deletion test/tm-cases/break_on_backslash.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
you to insert a backslash with or
without a space, which results in<br />
a hard line break, unless it has \
been escaped.</p>
been escaped. It's still possible to<br />
end the line with two spaces for a line break.</p>
3 changes: 2 additions & 1 deletion test/tm-cases/break_on_backslash.text
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ Github flavoured markdown allows \
you to insert a backslash with or
without a space, which results in\
a hard line break, unless it has \\
been escaped.
been escaped. It's still possible to
end the line with two spaces for a line break.
4 changes: 2 additions & 2 deletions test/tm-cases/break_on_newline_and_backslash.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<p>The breaks extra allows you to insert a hard break on newlines.<br />
You can also insert hard breaks after backslashes<br /><br />
although this will result in a double break when both are enabled.</p>
You can also insert hard breaks after backslashes<br />
which will result in a single break when both are enabled.</p>
2 changes: 1 addition & 1 deletion test/tm-cases/break_on_newline_and_backslash.text
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
The breaks extra allows you to insert a hard break on newlines.
You can also insert hard breaks after backslashes \
although this will result in a double break when both are enabled.
which will result in a single break when both are enabled.
6 changes: 3 additions & 3 deletions test/tm-cases/smarty_pants.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ <h3>Edge case: contractions</h3>

<p>For common contractions, a space single-prime non-space combination should produce an apostrophe (&amp;#8217;) instead of an opening scare quote (&amp;#8216;).</p>

<p>Here is the full list: &#8217;tis, &#8217;twas, &#8217;twer, &#8217;neath, &#8217;o, &#8217;n, &#8217;round, &#8217;bout, &#8217;twixt, &#8217;nuff, &#8217;fraid, &#8217;sup <br />
The full list, capitalized: &#8217;Tis, &#8217;Twas, &#8217;Twer, &#8217;Neath, &#8217;O, &#8217;N, &#8217;Round, &#8217;Bout, &#8217;Twixt, &#8217;Nuff, &#8217;Fraid, &#8217;Sup <br />
And normal text: &#8216;random &#8216;stuff &#8216;that &#8216;shouldn&#8217;t &#8216;be &#8216;detected &#8216;as &#8216;contractions <br />
<p>Here is the full list: &#8217;tis, &#8217;twas, &#8217;twer, &#8217;neath, &#8217;o, &#8217;n, &#8217;round, &#8217;bout, &#8217;twixt, &#8217;nuff, &#8217;fraid, &#8217;sup<br />
The full list, capitalized: &#8217;Tis, &#8217;Twas, &#8217;Twer, &#8217;Neath, &#8217;O, &#8217;N, &#8217;Round, &#8217;Bout, &#8217;Twixt, &#8217;Nuff, &#8217;Fraid, &#8217;Sup<br />
And normal text: &#8216;random &#8216;stuff &#8216;that &#8216;shouldn&#8217;t &#8216;be &#8216;detected &#8216;as &#8216;contractions<br />
And years: &#8217;29 &#8217;91 &#8216;1942 &#8216;2001 &#8216;2010</p>

<p>Like quotation marks, the year shorthand expects a year, e.g. '29, to be followed by whitespace or sentence-ending punctuation. Numbers like '456.7 will throw it off, but those aren&#8217;t entered very often.</p>
Expand Down
8 changes: 4 additions & 4 deletions test/tm-cases/spoiler.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
<p>[Second]</p>

<blockquote>
<p>! But this is <br />
<p>! But this is<br />
not a spoiler</p>
</blockquote>

<p>[Third]</p>

<blockquote class="spoiler">
<p>A multi-line spoiler <br />
<p>A multi-line spoiler<br />
has ! multiple times</p>
</blockquote>

<p>[Fourth]</p>

<blockquote class="spoiler">
<p>Alignment <br />
has no effect <br />
<p>Alignment<br />
has no effect<br />
on spoilers</p>
</blockquote>

Expand Down

0 comments on commit a9fb886

Please sign in to comment.