Skip to content

Commit

Permalink
Merge pull request #544 from m417z/breaks-improvements
Browse files Browse the repository at this point in the history
Breaks improvements and code simplification
  • Loading branch information
nicholasserra committed Jan 5, 2024
2 parents bb9d6b4 + 7097172 commit 1c7902b
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 23 deletions.
21 changes: 13 additions & 8 deletions lib/markdown2.py
Expand Up @@ -1366,15 +1366,20 @@ def _run_span_gamut(self, text):
text = self._do_smart_punctuation(text)

# Do hard breaks:
if 'breaks' in self.extras:
break_tag = "<br%s\n" % self.empty_element_suffix
# do backslashes first because on_newline inserts the break before the newline
if self.extras['breaks'].get('on_backslash', False):
text = re.sub(r' *\\\n', break_tag, text)
if self.extras['breaks'].get('on_newline', False):
text = re.sub(r" *\n(?!\<(?:\/?(ul|ol|li))\>)", break_tag, text)
on_backslash = self.extras.get('breaks', {}).get('on_backslash', False)
on_newline = self.extras.get('breaks', {}).get('on_newline', False)

if on_backslash and on_newline:
pattern = r' *\\?'
elif on_backslash:
pattern = r'(?: *\\| {2,})'
elif on_newline:
pattern = r' *'
else:
text = re.sub(r" {2,}\n", " <br%s\n" % self.empty_element_suffix, text)
pattern = r' {2,}'

break_tag = "<br%s\n" % self.empty_element_suffix
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
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
3 changes: 2 additions & 1 deletion test/tm-cases/break_on_backslash.html
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
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
@@ -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
@@ -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
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
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
2 changes: 1 addition & 1 deletion test/tm-cases/wavedrom.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/tm-cases/wavedrom_no_embed.html
Expand Up @@ -9,7 +9,7 @@
<ul>
<li>More complex diagram
<ul>
<li>Inside of nested list <br />
<li>Inside of nested list<br />
<script type="WaveDrom">
{ signal: [
{ name: "pclk", wave: 'p.......' },
Expand Down

0 comments on commit 1c7902b

Please sign in to comment.