From 16acd2b763f820f70b33a12d4efc295a936ae714 Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 24 Apr 2022 10:59:22 +0200 Subject: [PATCH 1/3] typo in readme --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 8aa5b68..4ef0401 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. From 5f1b98e25d605f5a87c049cffabb59dd17b5489f Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 24 Apr 2022 11:00:04 +0200 Subject: [PATCH 2/3] added wrap option closes #66 --- README.rst | 5 +++++ markdownify/__init__.py | 8 ++++++++ tests/test_conversions.py | 5 +++++ 3 files changed, 18 insertions(+) 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('

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(): From 5adda130b8424bad9d0ddb5a2cd7e9fa74e817fb Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 24 Apr 2022 11:01:29 +0200 Subject: [PATCH 3/3] bump to v0.11.2 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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', }