Added word wrapping to email view

This commit is contained in:
Armin Ronacher 2010-05-05 17:37:44 +02:00
parent 749e9ce7ac
commit 7af20c15e9
3 changed files with 34 additions and 3 deletions

View file

@ -1,3 +1,4 @@
import re
import creoleparser
from genshi import builder
from functools import wraps
@ -13,6 +14,8 @@ from flask_website.database import User
pygments_formatter = HtmlFormatter(style=FlaskyStyle)
_ws_split_re = re.compile(r'(\s+)')
class CodeBlock(PreBlock):
@ -46,6 +49,34 @@ def format_creole(text):
return Markup(_parser.render(text, encoding=None))
def split_lines_wrapping(text, width=74, threshold=82):
lines = text.splitlines()
if all(len(line) <= threshold for line in lines):
return lines
result = []
for line in lines:
if len(line) <= threshold:
result.append(line)
continue
line_width = 0
line_buffer = []
for piece in _ws_split_re.split(line):
line_width += len(piece)
if line_width > width:
result.append(u''.join(line_buffer))
line_buffer = []
if not piece.isspace():
line_buffer.append(piece)
line_width = len(piece)
else:
line_width = 0
else:
line_buffer.append(piece)
if line_buffer:
result.append(u''.join(line_buffer))
return result
def requires_login(f):
@wraps(f)
def decorated_function(*args, **kwargs):