aboutsummaryrefslogtreecommitdiffstats
path: root/wee_slack.py
diff options
context:
space:
mode:
authorcalve <calvinh34@gmail.com>2015-10-27 15:06:56 +0100
committercalve <calvinh34@gmail.com>2016-01-04 11:10:39 +0100
commit64415f6e19c05bbeea23bef57ef48c192d8762d9 (patch)
tree47e9003a5b4b2387b15923d31a8b51dfff8fc49b /wee_slack.py
parent24b5d143282f9d210b4aea8c9b18f3a65029dea1 (diff)
downloadwee-slack-64415f6e19c05bbeea23bef57ef48c192d8762d9.tar.gz
Rewrite ``unfurl_refs``
Dodge ``UnicodeDecodeException`` raised in some case I have not been able to reproduce.
Diffstat (limited to 'wee_slack.py')
-rw-r--r--wee_slack.py24
1 files changed, 7 insertions, 17 deletions
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