Skip to content

Commit

Permalink
Wrap lines of list items
Browse files Browse the repository at this point in the history
Paragraphs are usually wrapped at 78 characters per line. This patch
applies that to list items as well. It contains elements from scumop who
posted aaronsw#13 (comment).
But it has been rewritten to fix the amount of newline characters and
increase readability and performance.
  • Loading branch information
mkllnk committed Mar 11, 2019
1 parent 8ddc844 commit c335bcf
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions html2text.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ def optwrap(self, text):
assert wrap, "Requires Python 2.3."
result = ''
newlines = 0
reList = re.compile('(^[ ]+[0-9]+\. )|(^[ ]+[%s] )' %(self.ul_item_mark))
for para in text.split("\n"):
if len(para) > 0:
if not skipwrap(para):
Expand All @@ -740,6 +741,17 @@ def optwrap(self, text):
else:
result += "\n\n"
newlines = 2
# Handle list item
elif reList.match(para):
list_prefix = reList.search(para).group()
indent_width = len(list_prefix)
indent_spaces = ' ' * indent_width
list_width = BODY_WIDTH - indent_width
wrapped = wrap(para, list_width)
result += wrapped[0] + "\n"
for line in wrapped[1:]:
result += indent_spaces + line + "\n"
newlines = 1
else:
if not onlywhite(para):
result += para + "\n"
Expand Down

0 comments on commit c335bcf

Please sign in to comment.