Whole file

didix21/mdutils

The author described this change as Fix #6: add option to wrap text on new lines, paragraphs and writes. It counts as a record because the check below fails on the code as it stood at 9b0c5285f and passes on a0557475c, with nothing else changed between the two runs.

Fix saved2021-06-07
Sharing licenceMIT · LICENSE.txt
Change size+22 3

What the code was meant to do, written into the code itself as a save note

Fix #6: add option to wrap text on new lines, paragraphs and writes

The change

2626 from mdutils.tools.Link import Inline, Reference
2727 from mdutils.tools.TextUtils import TextUtils
2828 from mdutils.tools.MDList import MDList, MDCheckbox
29+from textwrap import fill
2930
3031
3132 class MdUtils:
213214
214215 return text_table
215216
216- def new_paragraph(self, text='', bold_italics_code='', color='black', align=''):
217+ def new_paragraph(self, text='', bold_italics_code='', color='black', align='', wrap_width=120):
217218 """Add a new paragraph to Markdown file. The text is saved to the global variable file_data_text.
218219
219220 :param text: is a string containing the paragraph text. Optionally, the paragraph text is returned.
224225 :type color: str
225226 :param align: Using this parameter you can align text.
226227 :type align: str
228+ :param wrap_width: wraps text with designated width by number of characters. By default, long words are not broken.
229+ Use width of 0 to disable wrapping.
230+ :type wrap_width: int
227231 :return: ``'\\n\\n' + text``. Not necessary to take it, if only has to be written to
228232 the file.
229233 :rtype: str
230234
231235 """
232236
237+ if wrap_width > 0:
238+ text = fill(text, wrap_width, break_long_words=False, replace_whitespace=False, drop_whitespace=False)
239+
233240 if bold_italics_code or color != 'black' or align:
234241 self.___update_file_data('\n\n' + self.textUtils.text_format(text, bold_italics_code, color, align))
235242 else:
237244
238245 return self.file_data_text
239246
240- def new_line(self, text='', bold_italics_code='', color='black', align=''):
247+ def new_line(self, text='', bold_italics_code='', color='black', align='', wrap_width=120):
241248 """Add a new line to Markdown file. The text is saved to the global variable file_data_text.
242249
243250 :param text: is a string containing the paragraph text. Optionally, the paragraph text is returned.
248255 :type color: str
249256 :param align: Using this parameter you can align text. For example ``'right'``, ``'left'`` or ``'center'``.
250257 :type align: str
258+ :param wrap_width: wraps text with designated width by number of characters. By default, long words are not broken.
259+ Use width of 0 to disable wrapping.
260+ :type wrap_width: int
251261 :return: return a string ``'\\n' + text``. Not necessary to take it, if only has to be written to the
252262 file.
253263 :rtype: str
254264 """
255265
266+ if wrap_width > 0:
267+ text = fill(text, wrap_width, break_long_words=False, replace_whitespace=False, drop_whitespace=False)
268+
256269 if bold_italics_code or color != 'black' or align:
257270 self.___update_file_data(' \n' + self.textUtils.text_format(text, bold_italics_code, color, align))
258271 else:
260273
261274 return self.file_data_text
262275
263- def write(self, text='', bold_italics_code='', color='black', align='', marker=''):
276+ def write(self, text='', bold_italics_code='', color='black', align='', marker='', wrap_width=120):
264277 """Write text in ``file_Data_text`` string.
265278
266279 :param text: a text a string.
271284 :type color: str
272285 :param align: Using this parameter you can align text. For example ``'right'``, ``'left'`` or ``'center'``.
273286 :type align: str
287+ :param wrap_width: wraps text with designated width by number of characters. By default, long words are not broken.
288+ Use width of 0 to disable wrapping.
289+ :type wrap_width: int
274290 :param marker: allows to replace a marker on some point of the file by the text.
275291 :type marker: str
276292 """
293+
294+ if wrap_width > 0:
295+ text = fill(text, wrap_width, break_long_words=False, replace_whitespace=False, drop_whitespace=False)
277296
278297 if bold_italics_code or color or align:
279298 new_text = self.textUtils.text_format(text, bold_italics_code, color, align)

The check that tells the two apart

failpass·tests/test_mdutils.py::TestMdUtils::test_wrap_text

Check file tests/test_mdutils.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before it9b0c5285fc700634d7d4a0756f05b0caa4df39d8
Broken version dated2020-12-03
Modulemdutils.mdutils
Units changedMdUtils
Fingerprint423281f70aecfeb5
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in didix21/mdutils