diff --git a/mdutils/mdutils.py b/mdutils/mdutils.py index 18e299b..510d274 100644 --- a/mdutils/mdutils.py +++ b/mdutils/mdutils.py @@ -26,6 +26,7 @@ from mdutils.tools.Link import Inline, Reference from mdutils.tools.TextUtils import TextUtils from mdutils.tools.MDList import MDList, MDCheckbox +from textwrap import fill class MdUtils: @@ -213,7 +214,7 @@ def new_table(self, columns, rows, text, text_align='center', marker=''): return text_table - def new_paragraph(self, text='', bold_italics_code='', color='black', align=''): + def new_paragraph(self, text='', bold_italics_code='', color='black', align='', wrap_width=0): """Add a new paragraph to Markdown file. The text is saved to the global variable file_data_text. :param text: is a string containing the paragraph text. Optionally, the paragraph text is returned. @@ -224,12 +225,17 @@ def new_paragraph(self, text='', bold_italics_code='', color='black', align=''): :type color: str :param align: Using this parameter you can align text. :type align: str + :param wrap_width: wraps text with designated width. By default, long words are not broken. + :type wrap_width: int :return: ``'\\n\\n' + text``. Not necessary to take it, if only has to be written to the file. :rtype: str """ + if wrap_width > 0: + text = fill(text, wrap_width, break_long_words=False) + if bold_italics_code or color != 'black' or align: self.___update_file_data('\n\n' + self.textUtils.text_format(text, bold_italics_code, color, align)) else: @@ -237,7 +243,7 @@ def new_paragraph(self, text='', bold_italics_code='', color='black', align=''): return self.file_data_text - def new_line(self, text='', bold_italics_code='', color='black', align=''): + def new_line(self, text='', bold_italics_code='', color='black', align='', wrap_width=0): """Add a new line to Markdown file. The text is saved to the global variable file_data_text. :param text: is a string containing the paragraph text. Optionally, the paragraph text is returned. @@ -248,11 +254,16 @@ def new_line(self, text='', bold_italics_code='', color='black', align=''): :type color: str :param align: Using this parameter you can align text. For example ``'right'``, ``'left'`` or ``'center'``. :type align: str + :param wrap_width: wraps text with designated width. By default, long words are not broken. + :type wrap_width: int :return: return a string ``'\\n' + text``. Not necessary to take it, if only has to be written to the file. :rtype: str """ + if wrap_width > 0: + text = fill(text, wrap_width, break_long_words=False) + if bold_italics_code or color != 'black' or align: self.___update_file_data(' \n' + self.textUtils.text_format(text, bold_italics_code, color, align)) else: @@ -260,7 +271,7 @@ def new_line(self, text='', bold_italics_code='', color='black', align=''): return self.file_data_text - def write(self, text='', bold_italics_code='', color='black', align='', marker=''): + def write(self, text='', bold_italics_code='', color='black', align='', marker='', wrap_width=0): """Write text in ``file_Data_text`` string. :param text: a text a string. @@ -271,10 +282,15 @@ def write(self, text='', bold_italics_code='', color='black', align='', marker=' :type color: str :param align: Using this parameter you can align text. For example ``'right'``, ``'left'`` or ``'center'``. :type align: str + :param wrap_width: wraps text with designated width. By default, long words are not broken. + :type wrap_width: int :param marker: allows to replace a marker on some point of the file by the text. :type marker: str """ + if wrap_width > 0: + text = fill(text, wrap_width, break_long_words=False) + if bold_italics_code or color or align: new_text = self.textUtils.text_format(text, bold_italics_code, color, align) else: diff --git a/tests/test_mdutils.py b/tests/test_mdutils.py index 8b1becb..caf52e4 100644 --- a/tests/test_mdutils.py +++ b/tests/test_mdutils.py @@ -137,6 +137,12 @@ def test_new_line(self): expected_value = ' \nThis is a new line created using new_line method.' self.assertEqual(created_value, expected_value) + def test_wrap_text(self): + md_file = MdUtils(file_name="Test_file", title="") + created_value = md_file.new_line("This is a new line created using new_line method with wrapping.", wrap_width=25) + expected_value = ' \nThis is a new line\ncreated using new_line\nmethod with wrapping.' + self.assertEqual(created_value, expected_value) + def test_insert_code(self): md_file = MdUtils(file_name='Test_file') code = ("mdFile.new_header(level=1, title='Atx Header 1')\n"