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.
Projectdidix21/mdutils
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
| 26 | 26 | from mdutils.tools.Link import Inline, Reference | |
| 27 | 27 | from mdutils.tools.TextUtils import TextUtils | |
| 28 | 28 | from mdutils.tools.MDList import MDList, MDCheckbox | |
| 29 | + | from textwrap import fill | |
| 29 | 30 | ||
| 30 | 31 | ||
| 31 | 32 | class MdUtils: | |
| ⋯ | |||
| 213 | 214 | ||
| 214 | 215 | return text_table | |
| 215 | 216 | ||
| 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): | |
| 217 | 218 | """Add a new paragraph to Markdown file. The text is saved to the global variable file_data_text. | |
| 218 | 219 | ||
| 219 | 220 | :param text: is a string containing the paragraph text. Optionally, the paragraph text is returned. | |
| ⋯ | |||
| 224 | 225 | :type color: str | |
| 225 | 226 | :param align: Using this parameter you can align text. | |
| 226 | 227 | :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 | |
| 227 | 231 | :return: ``'\\n\\n' + text``. Not necessary to take it, if only has to be written to | |
| 228 | 232 | the file. | |
| 229 | 233 | :rtype: str | |
| 230 | 234 | ||
| 231 | 235 | """ | |
| 232 | 236 | ||
| 237 | + | if wrap_width > 0: | |
| 238 | + | text = fill(text, wrap_width, break_long_words=False, replace_whitespace=False, drop_whitespace=False) | |
| 239 | + | ||
| 233 | 240 | if bold_italics_code or color != 'black' or align: | |
| 234 | 241 | self.___update_file_data('\n\n' + self.textUtils.text_format(text, bold_italics_code, color, align)) | |
| 235 | 242 | else: | |
| ⋯ | |||
| 237 | 244 | ||
| 238 | 245 | return self.file_data_text | |
| 239 | 246 | ||
| 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): | |
| 241 | 248 | """Add a new line to Markdown file. The text is saved to the global variable file_data_text. | |
| 242 | 249 | ||
| 243 | 250 | :param text: is a string containing the paragraph text. Optionally, the paragraph text is returned. | |
| ⋯ | |||
| 248 | 255 | :type color: str | |
| 249 | 256 | :param align: Using this parameter you can align text. For example ``'right'``, ``'left'`` or ``'center'``. | |
| 250 | 257 | :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 | |
| 251 | 261 | :return: return a string ``'\\n' + text``. Not necessary to take it, if only has to be written to the | |
| 252 | 262 | file. | |
| 253 | 263 | :rtype: str | |
| 254 | 264 | """ | |
| 255 | 265 | ||
| 266 | + | if wrap_width > 0: | |
| 267 | + | text = fill(text, wrap_width, break_long_words=False, replace_whitespace=False, drop_whitespace=False) | |
| 268 | + | ||
| 256 | 269 | if bold_italics_code or color != 'black' or align: | |
| 257 | 270 | self.___update_file_data(' \n' + self.textUtils.text_format(text, bold_italics_code, color, align)) | |
| 258 | 271 | else: | |
| ⋯ | |||
| 260 | 273 | ||
| 261 | 274 | return self.file_data_text | |
| 262 | 275 | ||
| 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): | |
| 264 | 277 | """Write text in ``file_Data_text`` string. | |
| 265 | 278 | ||
| 266 | 279 | :param text: a text a string. | |
| ⋯ | |||
| 271 | 284 | :type color: str | |
| 272 | 285 | :param align: Using this parameter you can align text. For example ``'right'``, ``'left'`` or ``'center'``. | |
| 273 | 286 | :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 | |
| 274 | 290 | :param marker: allows to replace a marker on some point of the file by the text. | |
| 275 | 291 | :type marker: str | |
| 276 | 292 | """ | |
| 293 | + | ||
| 294 | + | if wrap_width > 0: | |
| 295 | + | text = fill(text, wrap_width, break_long_words=False, replace_whitespace=False, drop_whitespace=False) | |
| 277 | 296 | ||
| 278 | 297 | if bold_italics_code or color or align: | |
| 279 | 298 | new_text = self.textUtils.text_format(text, bold_italics_code, color, align) | |
The check that tells the two apart
fail→pass·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
- 2025-10-18fix: Text starting with ** (bold) breaks new_list method
- 2022-10-27MarkDownFile
- 2020-09-12Fix #42 Default table's text_align should be '---'
- 2020-06-19MarkDownFile