diff --git a/README.rst b/README.rst index 8aa5b68..5b0d530 100644 --- a/README.rst +++ b/README.rst @@ -92,7 +92,7 @@ sub_symbol, sup_symbol newline_style Defines the style of marking linebreaks (``
``) in markdown. The default value ``SPACES`` of this option will adopt the usual two spaces and a newline, - while ``BACKSLASH`` will convert a linebreak to ``\\n`` (a backslash an a + while ``BACKSLASH`` will convert a linebreak to ``\\n`` (a backslash and a newline). While the latter convention is non-standard, it is commonly preferred and supported by a lot of interpreters. @@ -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/setup.py b/setup.py index fe4d41a..dd7c8b1 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read() pkgmeta = { '__title__': 'markdownify', '__author__': 'Matthew Tretter', - '__version__': '0.11.1', + '__version__': '0.11.2', } 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('

Some long link

', wrap=True, wrap_width=10) == '[Some long\nlink](https://example.com)\n\n' + assert md('

12345
67890

', wrap=True, wrap_width=10, newline_style=BACKSLASH) == '12345\\\n67890\n\n' + assert md('

12345678901
12345

', wrap=True, wrap_width=10, newline_style=BACKSLASH) == '12345678901\\\n12345\n\n' def test_pre():