Skip to content

Commit

Permalink
Merge pull request #529 from Crozzers/break-on-backslash
Browse files Browse the repository at this point in the history
Add `breaks` extra with ability to hard break on backslashes (issue #525)
  • Loading branch information
nicholasserra committed Sep 19, 2023
2 parents 2ef5653 + cda1e70 commit d61a7e1
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
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
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
@@ -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
@@ -0,0 +1 @@
{'extras': {'breaks': {'on_backslash': True}}}
5 changes: 5 additions & 0 deletions 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.
3 changes: 3 additions & 0 deletions test/tm-cases/break_on_newline_and_backslash.html
@@ -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
@@ -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
@@ -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.

0 comments on commit d61a7e1

Please sign in to comment.