From 7097172049936be063219169a7a981b0820a9db1 Mon Sep 17 00:00:00 2001 From: Michael Maltsev <4129781+m417z@users.noreply.github.com> Date: Sat, 9 Dec 2023 10:49:28 +0200 Subject: [PATCH] Breaks improvements and code simplification * Using both a newline and a backslash no longer results in two line breaks. * Allow to keep using trailing spaces for a line break. * Remove redundant trailing space before a line break. --- lib/markdown2.py | 21 ++++++++++++------- test/tm-cases/admonitions.html | 2 +- test/tm-cases/break_on_backslash.html | 3 ++- test/tm-cases/break_on_backslash.text | 3 ++- .../break_on_newline_and_backslash.html | 4 ++-- .../break_on_newline_and_backslash.text | 2 +- test/tm-cases/smarty_pants.html | 6 +++--- test/tm-cases/spoiler.html | 8 +++---- test/tm-cases/wavedrom.html | 2 +- test/tm-cases/wavedrom_no_embed.html | 2 +- 10 files changed, 30 insertions(+), 23 deletions(-) diff --git a/lib/markdown2.py b/lib/markdown2.py index 34a270b3..c1954883 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -1362,15 +1362,20 @@ def _run_span_gamut(self, text): text = self._do_smart_punctuation(text) # Do hard breaks: - if 'breaks' in self.extras: - break_tag = ")", 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", " )", break_tag, text) return text diff --git a/test/tm-cases/admonitions.html b/test/tm-cases/admonitions.html index 2b65647c..e8a7d05e 100644 --- a/test/tm-cases/admonitions.html +++ b/test/tm-cases/admonitions.html @@ -26,7 +26,7 @@ diff --git a/test/tm-cases/break_on_backslash.html b/test/tm-cases/break_on_backslash.html index 81040802..71a26425 100644 --- a/test/tm-cases/break_on_backslash.html +++ b/test/tm-cases/break_on_backslash.html @@ -2,4 +2,5 @@ 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.

diff --git a/test/tm-cases/break_on_backslash.text b/test/tm-cases/break_on_backslash.text index 0576c564..8a94983d 100644 --- a/test/tm-cases/break_on_backslash.text +++ b/test/tm-cases/break_on_backslash.text @@ -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. diff --git a/test/tm-cases/break_on_newline_and_backslash.html b/test/tm-cases/break_on_newline_and_backslash.html index cb43d6db..17706d82 100644 --- a/test/tm-cases/break_on_newline_and_backslash.html +++ b/test/tm-cases/break_on_newline_and_backslash.html @@ -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.

+You can also insert hard breaks after backslashes
+which will result in a single break when both are enabled.

diff --git a/test/tm-cases/break_on_newline_and_backslash.text b/test/tm-cases/break_on_newline_and_backslash.text index 4114125b..84c2b826 100644 --- a/test/tm-cases/break_on_newline_and_backslash.text +++ b/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. diff --git a/test/tm-cases/smarty_pants.html b/test/tm-cases/smarty_pants.html index 200c8977..b44af4da 100644 --- a/test/tm-cases/smarty_pants.html +++ b/test/tm-cases/smarty_pants.html @@ -37,9 +37,9 @@

Edge case: contractions

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

-

Here is the full list: ’tis, ’twas, ’twer, ’neath, ’o, ’n, ’round, ’bout, ’twixt, ’nuff, ’fraid, ’sup
-The full list, capitalized: ’Tis, ’Twas, ’Twer, ’Neath, ’O, ’N, ’Round, ’Bout, ’Twixt, ’Nuff, ’Fraid, ’Sup
-And normal text: ‘random ‘stuff ‘that ‘shouldn’t ‘be ‘detected ‘as ‘contractions
+

Here is the full list: ’tis, ’twas, ’twer, ’neath, ’o, ’n, ’round, ’bout, ’twixt, ’nuff, ’fraid, ’sup
+The full list, capitalized: ’Tis, ’Twas, ’Twer, ’Neath, ’O, ’N, ’Round, ’Bout, ’Twixt, ’Nuff, ’Fraid, ’Sup
+And normal text: ‘random ‘stuff ‘that ‘shouldn’t ‘be ‘detected ‘as ‘contractions
And years: ’29 ’91 ‘1942 ‘2001 ‘2010

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’t entered very often.

diff --git a/test/tm-cases/spoiler.html b/test/tm-cases/spoiler.html index 71e1c8ae..b0d5a3fb 100644 --- a/test/tm-cases/spoiler.html +++ b/test/tm-cases/spoiler.html @@ -7,22 +7,22 @@

[Second]

-

! But this is
+

! But this is
not a spoiler

[Third]

-

A multi-line spoiler
+

A multi-line spoiler
has ! multiple times

[Fourth]

-

Alignment
- has no effect
+

Alignment
+ has no effect
on spoilers

diff --git a/test/tm-cases/wavedrom.html b/test/tm-cases/wavedrom.html index e7a99a47..158fa133 100644 --- a/test/tm-cases/wavedrom.html +++ b/test/tm-cases/wavedrom.html @@ -174,7 +174,7 @@