diff options
author | Ryan Huber <rhuber@gmail.com> | 2016-02-12 07:43:20 -0800 |
---|---|---|
committer | Ryan Huber <rhuber@gmail.com> | 2016-02-12 07:43:20 -0800 |
commit | 3ef1eb4051bb9f8da4009bcf22cee15dfaaa2f5b (patch) | |
tree | da5fc85adf0ef33540525e7e6b1578a590cefa7b | |
parent | 8a9363b86d73fcefef761478f1e1fb19cea8cccb (diff) | |
parent | 8276125ff03662d4b40a9027baa5fa4428d06dc1 (diff) | |
download | wee-slack-3ef1eb4051bb9f8da4009bcf22cee15dfaaa2f5b.tar.gz |
Merge pull request #121 from calve/master
Rewrite ``unfurl_refs``
-rw-r--r-- | _pytest/test_unfurl.py | 3 | ||||
-rw-r--r-- | wee_slack.py | 30 |
2 files changed, 15 insertions, 18 deletions
diff --git a/_pytest/test_unfurl.py b/_pytest/test_unfurl.py index c79c0f3..2b80eda 100644 --- a/_pytest/test_unfurl.py +++ b/_pytest/test_unfurl.py @@ -24,6 +24,9 @@ unfurl_map = [ { "input": "url: <https://example.com|example with spaces> suffix", "output": "url: https://example.com (example with spaces) suffix", }, + { "input": "<@U2147483697|@othernick> multiple unfurl <https://example.com|example with spaces>", + "output": "@othernick multiple unfurl https://example.com (example with spaces)", + }, ] diff --git a/wee_slack.py b/wee_slack.py index c6d9b40..159b306 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -1590,9 +1590,9 @@ def process_message(message_json, cache=True): except Exception: channel = channels.find(message_json["channel"]) + dbg("cannot process message {}\n{}".format(message_json, traceback.format_exc())) if channel and ("text" in message_json) and message_json['text'] is not None: channel.buffer_prnt('unknown', message_json['text']) - dbg("cannot process message {}\n{}".format(message_json, traceback.format_exc())) def process_message_changed(message_json): @@ -1675,24 +1675,18 @@ 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 strings enclosed by <> + # - <https://example.com|example with spaces> + # - <#C2147483705|#otherchannel> + # - <@U2147483697|@othernick> + # Test patterns lives in ./_pytest/test_unfurl.py + matches = re.findall(r"(<[@#]?(?:[^<]*)>)", text) + for m in matches: + # Replace them with human readable strings + text = text.replace(m, unfurl_ref(m[1:-1], ignore_alt_text)) return text |