import re
import textwrap
import time
-import sys
import weakref
from .. import chunks, messages, roost, util, zcode
## The fun part
-MSG_NO_WRAP_PATTERNS = [re.compile(x) for x in (
- r'\n(?: |>)',
+MSG_NO_WRAP_PATTERNS = [re.compile(x, re.MULTILINE) for x in (
+ r'^(?: |>)',
r'\t',
r'[^.] ',
)]
+MSG_LIST_WRAP_PATTERN = re.compile(r'(^\s*[-*]\s+)', re.MULTILINE)
+
REALM_MAP = {
'ANDREW.CMU.EDU': 'AN',
'CS.CMU.EDU': 'CS',
return (bool(force_wrap_tuples & msg_tuples) or
not any(x.search(msg.body) for x in MSG_NO_WRAP_PATTERNS))
+ @staticmethod
+ def list_rewrap_p(msg):
+ return len(MSG_LIST_WRAP_PATTERN.findall(msg.body)) >= 2
+
+ @classmethod
+ def partial_rewrap(cls, body, width, indent):
+ try:
+ ret = ''
+ bindent = ''
+ for m in MSG_LIST_WRAP_PATTERN.split(body):
+ if not m:
+ continue
+ if MSG_LIST_WRAP_PATTERN.match(m):
+ # Bullet
+ if ret:
+ ret += indent
+ ret += m
+ bindent = ' ' * util.glyphwidth(m)
+ else:
+ # Content
+ # collapse whitespace and wrap.
+ ret += textwrap.fill(re.sub(r'\s+', ' ', m), width,
+ initial_indent=indent+bindent,
+ subsequent_indent=indent+bindent,
+ break_long_words=False,
+ break_on_hyphens=False).lstrip()
+ ret += '\n'
+
+ return ret.rstrip()
+ except Exception as e:
+ return str(e)
+
@classmethod
def vt_decorate(cls, msg, decoration):
indent = ' ' * util.glyphwidth(prefix)
body = msg.body.rstrip()
- if cls.rewrap_p(msg):
+ if cls.list_rewrap_p(msg):
+ body = cls.partial_rewrap(body, width, indent)
+ elif cls.rewrap_p(msg):
body = textwrap.fill(body, width,
initial_indent=indent,
subsequent_indent=indent,