Skip to content

Commit 3c11b1a

Browse files
committed
issue2551094 - markdown mismatch - new config for embedded newine
Make earlier patch to fix mismatch between simplemde formatting of embedded newlines choosable. New option MARKDOWN_BREAK_ON_NEWLINE added to config.ini. If set to true all 4 renders: markdown, markdown2, mistune and simplemde will make a newline into a hard linebreak using <br> when displayed.
1 parent 958dd87 commit 3c11b1a

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

CHANGES.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ Features:
5151
- issue2550716 - Email address displayed after password reset request.
5252
This fix actually made it into 1.6 release. However this release
5353
documents how password reset works in user_guide.txt. (John Rouillard)
54-
54+
- issue2551094 - add new markdown config.ini setting to allow embedded
55+
newlines to cause a linebreak same as GitHub Flavored Markdown.
56+
(Patch: Cedric Krier; Doc change/checkin John Rouillard)
57+
5558
2020-07-13 2.0.0
5659

5760
Fixed:

roundup/cgi/templating.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ class Markdown(markdown2.Markdown):
6969
# don't allow disabled protocols in links
7070
_safe_protocols = re.compile('(?!' + ':|'.join([re.escape(s) for s in _disable_url_schemes]) + ':)', re.IGNORECASE)
7171

72-
markdown = lambda s: Markdown(safe_mode='escape', extras={ 'fenced-code-blocks' : True }).convert(s)
72+
def _extras(config):
73+
extras = { 'fenced-code-blocks' : True }
74+
if config['MARKDOWN_BREAK_ON_NEWLINE']:
75+
extras['break-on-newline'] = True
76+
return extras
77+
78+
markdown = lambda s, c: Markdown(safe_mode='escape', extras=_extras(c)).convert(s)
7379
except ImportError:
7480
markdown = None
7581

@@ -107,7 +113,13 @@ def extendMarkdown(self, md, md_globals=None):
107113
else:
108114
md.treeprocessors['restrict_links'] = RestrictLinksProcessor()
109115

110-
markdown = lambda s: markdown_impl(s, extensions=[SafeHtml(), 'fenced_code'])
116+
def _extensions(config):
117+
extensions = [SafeHtml(), 'fenced_code']
118+
if config['MARKDOWN_BREAK_ON_NEWLINE']:
119+
extensions.append('nl2br')
120+
return extensions
121+
122+
markdown = lambda s, c: markdown_impl(s, extensions=_extensions(c))
111123
except ImportError:
112124
markdown = None
113125

@@ -117,7 +129,14 @@ def _import_mistune():
117129
try:
118130
import mistune
119131
mistune._scheme_blacklist = [ s + ':' for s in _disable_url_schemes ]
120-
markdown = mistune.markdown
132+
133+
def _options(config):
134+
options = {}
135+
if config['MARKDOWN_BREAK_ON_NEWLINE']:
136+
options['hard_wrap'] = True
137+
return options
138+
139+
markdown = lambda s, c: mistune.markdown(s, **_options(c))
121140
except ImportError:
122141
markdown = None
123142

@@ -1731,7 +1750,7 @@ def markdown(self, hyperlink=1):
17311750
if hyperlink:
17321751
s = self.hyper_re.sub(self._hyper_repl_markdown, s)
17331752
try:
1734-
s = u2s(markdown(s2u(s)))
1753+
s = u2s(markdown(s2u(s), self._db.config))
17351754
except Exception: # when markdown formatting fails return markup
17361755
return self.plain(escape=0, hyperlink=hyperlink)
17371756
return s

roundup/configuration.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,12 @@ def str2value(self, value):
13291329
"won't be attached to nosy mails. They will be replaced by\n"
13301330
"a link to the tracker's download page for the file.")
13311331
), "Nosy messages sending"),
1332+
("markdown", (
1333+
(BooleanOption, "break_on_newline", "no",
1334+
"If yes/true, render single new line characters in markdown\n"
1335+
"text with <br>. Set true if you want GitHub Flavored Markdown\n"
1336+
"(GFM) handling of embedded newlines."),
1337+
), "Markdown rendering options."),
13321338
)
13331339

13341340
### Configuration classes

share/roundup/templates/jinja2/html/issue.item.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
var node = $('#change_note')[0];
3333
var initSimpleMde = function () {
3434
node.parentNode.appendChild($('<input/>', { type: 'hidden', name: 'msg-1@type', value: 'text/markdown'})[0]);
35-
var simplemde = new SimpleMDE({ element: node, status: false, styleSelectedText: false, renderingConfig: {singleLineBreaks: false}});
35+
var simplemde = new SimpleMDE({ element: node, status: false,
36+
styleSelectedText: false, renderingConfig: {singleLineBreaks: {{ config.MARKDOWN_BREAK_ON_NEWLINE and 'true' or 'false' }} }});
3637
simplemde.render();
3738
};
3839
{% if context.id %}

test/test_templating.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def setUp(self):
7474
# add client props for testing anti_csrf_nonce
7575
self.client.session_api = MockNull(_sid="1234567890")
7676
self.client.db.getuid = lambda : 10
77-
self.client.db.config = {'WEB_CSRF_TOKEN_LIFETIME': 10 }
77+
self.client.db.config = {'WEB_CSRF_TOKEN_LIFETIME': 10, 'MARKDOWN_BREAK_ON_NEWLINE': False }
7878

7979
class HTMLDatabaseTestCase(TemplatingTestCase):
8080
def test_HTMLDatabase___getitem__(self):
@@ -477,6 +477,16 @@ def test_string_markdown_code_block_attribute(self):
477477
# so processing it returns original text
478478
self.assertEqual(m.replace('\n\n', '\n'), u2s(u'embedded code block &lt;pre&gt;\n``` python\nline 1\nline 2\n```\nnew &lt;/pre&gt; paragraph'))
479479

480+
def test_break_on_newline(self):
481+
self.client.db.config['MARKDOWN_BREAK_ON_NEWLINE'] = True
482+
p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'A string with\nline break\ntwice.'))
483+
m = p.markdown()
484+
self.assertEqual(2, m.count('<br'))
485+
self.client.db.config['MARKDOWN_BREAK_ON_NEWLINE'] = False
486+
487+
m = p.markdown()
488+
self.assertEqual(0, m.count('<br'))
489+
480490

481491
@skip_mistune
482492
class MistuneTestCase(TemplatingTestCase, MarkdownTests) :

0 commit comments

Comments
 (0)