From 5d4cab62e54d2ec99fe746459f626390323bbc83 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Wed, 16 Nov 2016 14:18:18 -0500 Subject: [PATCH] Display links as footnotes Rather than including them in the text that gets wrapped. This prevents links from getting split, potentially breaking them. Signed-off-by: Anna Schumaker --- slack/chat.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/slack/chat.py b/slack/chat.py index ecb14da..6a1ec70 100644 --- a/slack/chat.py +++ b/slack/chat.py @@ -25,10 +25,20 @@ def _text_parse_channel(text): res = re.sub("<#%s(.*?)>" % chid, "#%s" % name, res) return res +def _text_parse_links(text): + res = text + links = [] + for link in re.findall("<(.*?)>", text): + split = link.split("|") + msg = split[1] + " " if len(split) > 1 else "" + res = re.sub("<(.*?)>", "%s[#####]" % msg, res) + links += [ split[0] ] + return (res, links) + def _text_parse_tags(text): text = _text_parse_uid(text) text = _text_parse_channel(text) - return text + return _text_parse_links(text) @@ -44,10 +54,23 @@ class Message: def __str__(self): lines = [] + links = [] + count = 0 for line in self.__text: - line = _text_parse_tags(line) + line, link = _text_parse_tags(line) + while line.count("[#####]") > 0: + line = line.replace("[#####]", "[%s]" % count, 1) + count += 1 + lines += [ "" ] + textwrap.wrap(line) + links += link + text = "\n ".join(lines) + if len(links) > 0: + text += "\n __________" + for link in enumerate(links): + text += "\n [%s] %s" % (link[0], link[1]) + time = self.__time.strftime("%I:%M:%S %p") return "\n%s | %s:\n%s\n" % (time, self.__user, text)