diff --git a/README.rst b/README.rst index 4ef0401..5b0d530 100644 --- a/README.rst +++ b/README.rst @@ -130,6 +130,11 @@ keep_inline_images_in that should be allowed to contain inline images, for example ``['td']``. Defaults to an empty list. +wrap, wrap_width + If ``wrap`` is set to ``True``, all text paragraphs are wrapped at + ``wrap_width`` characters. Defaults to ``False`` and ``80``. + Use with ``newline_style=BACKSLASH`` to keep line breaks in paragraphs. + Options may be specified as kwargs to the ``markdownify`` function, or as a nested ``Options`` class in ``MarkdownConverter`` subclasses. diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 8461c1c..a49eed4 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -1,4 +1,5 @@ from bs4 import BeautifulSoup, NavigableString, Comment, Doctype +from textwrap import fill import re import six @@ -75,6 +76,8 @@ class MarkdownConverter(object): strong_em_symbol = ASTERISK sub_symbol = '' sup_symbol = '' + wrap = False + wrap_width = 80 class Options(DefaultOptions): pass @@ -331,6 +334,11 @@ class MarkdownConverter(object): def convert_p(self, el, text, convert_as_inline): if convert_as_inline: return text + if self.options['wrap']: + text = fill(text, + width=self.options['wrap_width'], + break_long_words=False, + break_on_hyphens=False) return '%s\n\n' % text if text else '' def convert_pre(self, el, text, convert_as_inline): diff --git a/tests/test_conversions.py b/tests/test_conversions.py index cef8cd6..04c8db7 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -177,6 +177,11 @@ def test_kbd(): def test_p(): assert md('
hello
') == 'hello\n\n' + assert md('123456789 123456789
') == '123456789 123456789\n\n' + assert md('123456789 123456789
', wrap=True, wrap_width=10) == '123456789\n123456789\n\n' + assert md('', wrap=True, wrap_width=10) == '[Some long\nlink](https://example.com)\n\n' + assert md('12345
67890
12345678901
12345