From 64415f6e19c05bbeea23bef57ef48c192d8762d9 Mon Sep 17 00:00:00 2001 From: calve Date: Tue, 27 Oct 2015 15:06:56 +0100 Subject: Rewrite ``unfurl_refs`` Dodge ``UnicodeDecodeException`` raised in some case I have not been able to reproduce. --- wee_slack.py | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) (limited to 'wee_slack.py') diff --git a/wee_slack.py b/wee_slack.py index c8d24ee..9760b28 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -1671,24 +1671,14 @@ def unfurl_ref(ref, ignore_alt_text=False): def unfurl_refs(text, ignore_alt_text=False): """ - Worst code ever written. this needs work + input : <@U096Q7CQM|someuser> has joined the channel + ouput : someuser has joined the channel """ - if text and text.find('<') > -1: - end = 0 - newtext = u"" - while text.find('<') > -1: - # Prepend prefix - newtext += text[:text.find('<')] - text = text[text.find('<'):] - end = text.find('>') - if end == -1: - newtext += text - break - # Format thingabob - newtext += unfurl_ref(text[1:end], ignore_alt_text) - text = text[end+1:] - newtext += text - return newtext + # Find all string enclosed by <> and starting with an @ + matches = re.findall(r"(<@(?:\S*)>)", text) + for m in matches: + # Replace them with human readable strings + text = text.replace(m, unfurl_ref(m[1:-1])) return text -- cgit