From e2a595c4d037f30f63f7da2a50025456e700ad22 Mon Sep 17 00:00:00 2001 From: Crozzers Date: Tue, 5 Sep 2023 18:49:00 +0100 Subject: [PATCH 1/2] Add `breaks` extra with ability to hard break on backslashes. See #525 --- lib/markdown2.py | 20 ++++++++++++++++--- test/tm-cases/break_on_backslash.html | 5 +++++ test/tm-cases/break_on_backslash.opts | 1 + test/tm-cases/break_on_backslash.text | 5 +++++ .../break_on_newline_and_backslash.html | 3 +++ .../break_on_newline_and_backslash.opts | 1 + .../break_on_newline_and_backslash.text | 3 +++ 7 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 test/tm-cases/break_on_backslash.html create mode 100644 test/tm-cases/break_on_backslash.opts create mode 100644 test/tm-cases/break_on_backslash.text create mode 100644 test/tm-cases/break_on_newline_and_backslash.html create mode 100644 test/tm-cases/break_on_newline_and_backslash.opts create mode 100644 test/tm-cases/break_on_newline_and_backslash.text diff --git a/lib/markdown2.py b/lib/markdown2.py index 87b9858c..f7a0e328 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -41,7 +41,11 @@ see for details): * admonitions: Enable parsing of RST admonitions. -* break-on-newline: Replace single new line characters with
when True +* breaks: Control where hard breaks are inserted in the markdown. + Options include: + - on_newline: Replace single new line characters with
when True + - on_backslash: Replace backslashes at the end of a line with
+* break-on-newline: Alias for the on_newline option in the breaks extra. * code-friendly: Disable _ and __ for em and strong. * cuddled-lists: Allow lists to be cuddled to the preceding paragraph. * fenced-code-blocks: Allows a code block to not have to be indented @@ -235,6 +239,11 @@ def __init__(self, html4tags=False, tab_width=4, safe_mode=None, self._toc_depth = 6 else: self._toc_depth = self.extras["toc"].get("depth", 6) + + if 'break-on-newline' in self.extras: + self.extras.setdefault('breaks', {}) + self.extras['breaks']['on_newline'] = True + self._instance_extras = self.extras.copy() if 'link-patterns' in self.extras: @@ -1318,8 +1327,13 @@ def _run_span_gamut(self, text): text = self._do_smart_punctuation(text) # Do hard breaks: - if "break-on-newline" in self.extras: - text = re.sub(r" *\n(?!\<(?:\/?(ul|ol|li))\>)", ")", break_tag, text) else: text = re.sub(r" {2,}\n", " 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.

diff --git a/test/tm-cases/break_on_backslash.opts b/test/tm-cases/break_on_backslash.opts new file mode 100644 index 00000000..52f81fdc --- /dev/null +++ b/test/tm-cases/break_on_backslash.opts @@ -0,0 +1 @@ +{'extras': {'breaks': {'on_backslash': True}}} diff --git a/test/tm-cases/break_on_backslash.text b/test/tm-cases/break_on_backslash.text new file mode 100644 index 00000000..0576c564 --- /dev/null +++ b/test/tm-cases/break_on_backslash.text @@ -0,0 +1,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. diff --git a/test/tm-cases/break_on_newline_and_backslash.html b/test/tm-cases/break_on_newline_and_backslash.html new file mode 100644 index 00000000..cb43d6db --- /dev/null +++ b/test/tm-cases/break_on_newline_and_backslash.html @@ -0,0 +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.

diff --git a/test/tm-cases/break_on_newline_and_backslash.opts b/test/tm-cases/break_on_newline_and_backslash.opts new file mode 100644 index 00000000..5ac49b03 --- /dev/null +++ b/test/tm-cases/break_on_newline_and_backslash.opts @@ -0,0 +1 @@ +{'extras': {'breaks': {'on_backslash': True, 'on_newline': True}}} diff --git a/test/tm-cases/break_on_newline_and_backslash.text b/test/tm-cases/break_on_newline_and_backslash.text new file mode 100644 index 00000000..4114125b --- /dev/null +++ b/test/tm-cases/break_on_newline_and_backslash.text @@ -0,0 +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. From f25bc21962b86fdb130e0bc6eed3ac88cc2f4fe4 Mon Sep 17 00:00:00 2001 From: Crozzers Date: Tue, 5 Sep 2023 19:00:09 +0100 Subject: [PATCH 2/2] Update changes.md --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 38983901..841980d1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,7 +3,7 @@ ## python-markdown2 2.4.11 (not yet released) - [pull #524] Fix angles being escaped in style blocks (issue #523) - +- [pull #529] Add `breaks` extra with ability to hard break on backslashes (issue #525) ## python-markdown2 2.4.10