Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add breaks extra with ability to hard break on backslashes (issue #525) #529

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

- [pull #524] Fix angles being escaped in style blocks (issue #523)
- [pull #527] Fix base64 images being corrupted in safe mode (issue #526)

- [pull #529] Add `breaks` extra with ability to hard break on backslashes (issue #525)

## python-markdown2 2.4.10

Expand Down
20 changes: 17 additions & 3 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@
see <https://github.com/trentm/python-markdown2/wiki/Extras> for details):

* admonitions: Enable parsing of RST admonitions.
* break-on-newline: Replace single new line characters with <br> when True
* breaks: Control where hard breaks are inserted in the markdown.
Options include:
- on_newline: Replace single new line characters with <br> when True
- on_backslash: Replace backslashes at the end of a line with <br>
* 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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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))\>)", "<br%s\n" % self.empty_element_suffix, text)
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)
else:
text = re.sub(r" {2,}\n", " <br%s\n" % self.empty_element_suffix, text)

Expand Down
5 changes: 5 additions & 0 deletions test/tm-cases/break_on_backslash.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>Github flavoured markdown allows<br />
you to insert a backslash with or
without a space, which results in<br />
a hard line break, unless it has \
been escaped.</p>
1 change: 1 addition & 0 deletions test/tm-cases/break_on_backslash.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'extras': {'breaks': {'on_backslash': True}}}
5 changes: 5 additions & 0 deletions test/tm-cases/break_on_backslash.text
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions test/tm-cases/break_on_newline_and_backslash.html
Original file line number Diff line number Diff line change
@@ -0,0 +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>
1 change: 1 addition & 0 deletions test/tm-cases/break_on_newline_and_backslash.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'extras': {'breaks': {'on_backslash': True, 'on_newline': True}}}
3 changes: 3 additions & 0 deletions test/tm-cases/break_on_newline_and_backslash.text
Original file line number Diff line number Diff line change
@@ -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.
Loading