Skip to content
Open
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
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ table_infer_header
wrap, wrap_width
If ``wrap`` is set to ``True``, all text paragraphs are wrapped at
``wrap_width`` characters. Defaults to ``False`` and ``80``.
List items containing only inline content are also wrapped; list markers
and indentation are added outside this width. Items containing block
elements retain their existing layout.
Use with ``newline_style=BACKSLASH`` to keep line breaks in paragraphs.
A `wrap_width` value of `None` reflows lines to unlimited line length.

Expand Down
44 changes: 28 additions & 16 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,14 @@ def convert_li(self, el, text, parent_tags):
if not text:
return "\n"

# Reflow inline-only items; block descendants already have their own
# layout and must not be flattened (including nested lists).
if self.options['wrap'] and not any(
should_remove_whitespace_outside(child)
or child.name in ('hr', 'figure', 'figcaption', 'caption')
for child in el.find_all(True)):
text = self._wrap_text(text)

# determine list item bullet character to use
parent = el.parent
if parent is not None and parent.name == 'ol':
Expand Down Expand Up @@ -667,24 +675,28 @@ def convert_p(self, el, text, parent_tags):
return ' ' + text.strip(' \t\r\n') + ' '
text = text.strip(' \t\r\n')
if self.options['wrap']:
# Preserve newlines (and preceding whitespace) resulting
# from <br> tags. Newlines in the input have already been
# replaced by spaces.
if self.options['wrap_width'] is not None:
lines = text.split('\n')
new_lines = []
for line in lines:
line = line.lstrip(' \t\r\n')
line_no_trailing = line.rstrip()
trailing = line[len(line_no_trailing):]
line = fill(line,
width=self.options['wrap_width'],
break_long_words=False,
break_on_hyphens=False)
new_lines.append(line + trailing)
text = '\n'.join(new_lines)
text = self._wrap_text(text)
return '\n\n%s\n\n' % text if text else ''

def _wrap_text(self, text):
# Preserve newlines (and preceding whitespace) resulting
# from <br> tags. Newlines in the input have already been
# replaced by spaces.
if self.options['wrap_width'] is not None:
lines = text.split('\n')
new_lines = []
for line in lines:
line = line.lstrip(' \t\r\n')
line_no_trailing = line.rstrip()
trailing = line[len(line_no_trailing):]
line = fill(line,
width=self.options['wrap_width'],
break_long_words=False,
break_on_hyphens=False)
new_lines.append(line + trailing)
text = '\n'.join(new_lines)
return text

def convert_pre(self, el, text, parent_tags):
if not text:
return ''
Expand Down
25 changes: 25 additions & 0 deletions tests/test_lists.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .utils import md
from markdownify import markdownify


nested_uls = """
Expand Down Expand Up @@ -87,3 +88,27 @@ def test_bullets():

def test_li_text():
assert md('<ul><li>foo <a href="#">bar</a></li><li>foo bar </li><li>foo <b>bar</b> <i>space</i>.</ul>') == '\n\n* foo [bar](#)\n* foo bar\n* foo **bar** *space*.\n'


def test_wrap_inline_list_items():
text = 'alpha beta gamma delta epsilon'
assert markdownify('<ul><li>' + text + '</li></ul>', wrap=True, wrap_width=16) == '* alpha beta gamma\n delta epsilon'
assert markdownify('<ol start="12"><li>' + text + '</li></ol>', wrap=True, wrap_width=16) == '12. alpha beta gamma\n delta epsilon'
assert markdownify('<ul><li>' + text + '</li></ul>', wrap=False, wrap_width=16) == '* ' + text
assert markdownify('<ul><li>' + text + '</li></ul>', wrap=True, wrap_width=None) == '* ' + text


def test_wrap_inline_list_formatting_and_breaks():
assert markdownify('<ul><li>alpha beta gamma<br>delta epsilon zeta</li></ul>', wrap=True, wrap_width=12, newline_style='backslash') == '* alpha beta\n gamma\\\n delta\n epsilon zeta'
assert markdownify('<ul><li><strong>alpha</strong> beta gamma delta</li></ul>', wrap=True, wrap_width=16) == '* **alpha** beta\n gamma delta'
assert markdownify('<ul><li>alpha beta gamma<br>delta epsilon zeta</li></ul>', wrap=True, wrap_width=12) == '* alpha beta\n gamma \n delta\n epsilon zeta'


def test_wrap_list_blocks_are_not_reflowed():
nested = '<ul><li>parent<ul><li>alpha beta gamma delta</li></ul></li></ul>'
assert markdownify(nested, wrap=True, wrap_width=12) == '* parent\n + alpha beta\n gamma delta'
for block in ['<pre>alpha beta gamma delta</pre>', '<hr>', '<figure><figcaption>alpha beta gamma delta</figcaption></figure>', '<span><pre>alpha beta gamma delta</pre></span>', '<table><tr><td>alpha beta gamma delta</td></tr></table>', '<blockquote>alpha beta gamma delta</blockquote>']:
html = '<ul><li>before' + block + 'after</li></ul>'
assert markdownify(html, wrap=True, wrap_width=12) == markdownify(html, wrap=False)
html = '<ul><li><p>alpha beta gamma delta</p></li></ul>'
assert markdownify(html, wrap=True, wrap_width=12) == '* alpha beta\n gamma delta'