From 5f1b98e25d605f5a87c049cffabb59dd17b5489f Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 24 Apr 2022 11:00:04 +0200 Subject: [PATCH] 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():