Skip to content

Commit fea6d8c

Browse files
Merge pull request #716 from santhreal/fix/breaks-extra-typeerror-with-break-on-newline
Fix TypeError when 'breaks' and 'break-on-newline' extras are combined
2 parents dac7137 + b0cc58f commit fea6d8c

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

lib/markdown2.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,13 @@ def __init__(
366366
}
367367

368368
if 'break-on-newline' in self.extras:
369-
self.extras.setdefault('breaks', {})
369+
# `break-on-newline` is an alias for the breaks extra's `on_newline`
370+
# option. When both extras are given (e.g. extras=['breaks',
371+
# 'break-on-newline']) `breaks` is already a key mapped to None, so
372+
# setdefault would leave it None and the assignment below would
373+
# raise a TypeError. Normalise to a dict before setting the option.
374+
if not isinstance(self.extras.get('breaks'), dict):
375+
self.extras['breaks'] = {}
370376
self.extras['breaks']['on_newline'] = True
371377

372378
if 'link-patterns' in self.extras:

test/test_markdown2.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,30 @@ def test_russian(self):
272272
'<h2>%s</h2>\n' % ko)
273273
test_russian.tags = ["unicode", "issue3"]
274274

275+
def test_breaks_and_break_on_newline_together(self):
276+
# `break-on-newline` is an alias for the breaks extra; supplying both
277+
# in the common list form used to raise TypeError during setup.
278+
expected = '<p>a<br />\nb</p>\n'
279+
self.assertEqual(
280+
markdown2.markdown('a\nb', extras=['breaks', 'break-on-newline']),
281+
expected)
282+
self.assertEqual(
283+
markdown2.markdown('a\nb', extras=['break-on-newline', 'breaks']),
284+
expected)
285+
# Each alone keeps its documented behaviour.
286+
self.assertEqual(
287+
markdown2.markdown('a\nb', extras=['break-on-newline']), expected)
288+
self.assertEqual(
289+
markdown2.markdown('a\nb', extras=['breaks']), '<p>a\nb</p>\n')
290+
# A breaks dict with other options is preserved, not overwritten.
291+
self.assertEqual(
292+
markdown2.markdown(
293+
'a\\\nb',
294+
extras={'breaks': {'on_backslash': True},
295+
'break-on-newline': None}),
296+
'<p>a<br />\nb</p>\n')
297+
test_breaks_and_break_on_newline_together.tags = ["breaks", "extras"]
298+
275299
def test_metadata_no_blank_line(self):
276300
# A single-block document with no blank line (e.g. a tab-indented code
277301
# block) gives the metadata extra nothing to split on, so re.split

0 commit comments

Comments
 (0)