Skip to content

Commit

Permalink
Add breaks extra with ability to hard break on backslashes.
Browse files Browse the repository at this point in the history
See #525
  • Loading branch information
Crozzers committed Sep 5, 2023
1 parent c94e417 commit e2a595c
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 3 deletions.
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.

0 comments on commit e2a595c

Please sign in to comment.