Added word wrapping to email view
This commit is contained in:
parent
749e9ce7ac
commit
7af20c15e9
3 changed files with 34 additions and 3 deletions
|
|
@ -27,7 +27,7 @@
|
|||
<dt>Date:
|
||||
<dd>{{ mail.date.strftime('%Y-%m-%d @ %H:%M') }}
|
||||
</dl>
|
||||
<pre>{{ mail.rendered_text }}</pre>
|
||||
<pre>{{ mail.rendered_text() }}</pre>
|
||||
{% if mail.children %}
|
||||
<div class=children>{{ loop(mail.children) }}</div>
|
||||
{% endif %}
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ from hashlib import md5
|
|||
from werkzeug import parse_date
|
||||
from jinja2.utils import urlize
|
||||
from flask import Module, render_template, json, url_for, abort, Markup
|
||||
from flask_website.utils import split_lines_wrapping
|
||||
from flask_website import config
|
||||
|
||||
mailinglist = Module(__name__, url_prefix='/mailinglist')
|
||||
|
|
@ -19,11 +20,10 @@ class Mail(object):
|
|||
self.children = [Mail(x) for x in d['children']]
|
||||
self.text = d['text']
|
||||
|
||||
@property
|
||||
def rendered_text(self):
|
||||
result = []
|
||||
in_sig = False
|
||||
for line in self.text.splitlines():
|
||||
for line in split_lines_wrapping(self.text):
|
||||
if line == u'-- ':
|
||||
in_sig = True
|
||||
if in_sig:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue