Skip to content

Commit

Permalink
Merge pull request #53 from mhmdkanj/feat/textwrap
Browse files Browse the repository at this point in the history
Fix #6: add option to wrap text on new lines, paragraphs and writes
  • Loading branch information
didix21 authored Jun 8, 2021
2 parents 9b0c528 + a055747 commit f876138
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
25 changes: 22 additions & 3 deletions mdutils/mdutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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=120):
"""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.
Expand All @@ -224,20 +225,26 @@ 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 number of characters. By default, long words are not broken.
Use width of 0 to disable wrapping.
: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, replace_whitespace=False, drop_whitespace=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:
self.___update_file_data('\n\n' + text)

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=120):
"""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.
Expand All @@ -248,19 +255,25 @@ 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 number of characters. By default, long words are not broken.
Use width of 0 to disable wrapping.
: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, replace_whitespace=False, drop_whitespace=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:
self.___update_file_data(' \n' + text)

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=120):
"""Write text in ``file_Data_text`` string.
:param text: a text a string.
Expand All @@ -271,10 +284,16 @@ 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 number of characters. By default, long words are not broken.
Use width of 0 to disable wrapping.
: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, replace_whitespace=False, drop_whitespace=False)

if bold_italics_code or color or align:
new_text = self.textUtils.text_format(text, bold_italics_code, color, align)
else:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_mdutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit f876138

Please sign in to comment.