diff options
author | Ryan Huber <rhuber@gmail.com> | 2015-09-10 07:20:35 -0700 |
---|---|---|
committer | Ryan Huber <rhuber@gmail.com> | 2015-09-10 07:20:35 -0700 |
commit | 2d50c4e59ae603706840ea42ec1e718c62232458 (patch) | |
tree | 749b9e5f47092395595529e49cc3b741bfb911f6 /wee_slack.py | |
parent | 1155d23c0e1c9f37551a9e74ce25efbc19a8107b (diff) | |
parent | d46b8366cb1e35fd939b4e3e6f8bf087b2208c1b (diff) | |
download | wee-slack-2d50c4e59ae603706840ea42ec1e718c62232458.tar.gz |
Merge pull request #106 from rawdigits/tfheen/unfurl-test-and-fixups
unfurl unit test and cleanups
Diffstat (limited to 'wee_slack.py')
-rw-r--r-- | wee_slack.py | 79 |
1 files changed, 48 insertions, 31 deletions
diff --git a/wee_slack.py b/wee_slack.py index f84946c..db4b178 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -1465,43 +1465,60 @@ def unwrap_attachments(message_json): # attachment_text = attachment_text.encode('ascii', 'ignore') return attachment_text +def resolve_ref(ref): + if ref.startswith('@U'): + if users.find(ref[1:]): + try: + return "@{}".format(users.find(ref[1:]).name) + except: + dbg("NAME: {}".format(ref)) + elif ref.startswith('#C'): + if channels.find(ref[1:]): + try: + return "{}".format(channels.find(ref[1:]).name) + except: + dbg("CHANNEL: {}".format(ref)) + + # Something else, just return as-is + return ref + +def unfurl_ref(ref, ignore_alt_text=False): + id = ref.split('|')[0] + display_text = ref + if ref.find('|') > -1: + if ignore_alt_text: + display_text = resolve_ref(id) + else: + if id.startswith("#C") or id.startswith("@U"): + display_text = ref.split('|')[1] + else: + url, desc = ref.split('|', 1) + display_text = "{} ({})".format(url, desc) + else: + display_text = resolve_ref(ref) + return display_text def unfurl_refs(text, ignore_alt_text=False): """ Worst code ever written. this needs work """ if text and text.find('<') > -1: - newtext = [] - text = text.split(" ") - for item in text: - # dbg(item) - prefix = "" - suffix = "" - start = item.find('<') - end = item.find('>') - if start > -1 and end > -1: - prefix = item[:start] - suffix = item[end+1:] - item = item[start + 1:end] - if item.find('|') > -1: - if ignore_alt_text: - item = item.split('|')[1] - else: - item = item.split('|')[0] - if item.startswith('@U'): - if users.find(item[1:]): - try: - item = "@{}".format(users.find(item[1:]).name) - except: - dbg("NAME: {}".format(item)) - if item.startswith('#C'): - if channels.find(item[1:]): - item = "{}".format(channels.find(item[1:]).name) - newtext.append(prefix + item + suffix) - text = " ".join(newtext) - return text - else: - return text + end = 0 + newtext = "" + 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 + return text def get_user(message_json, server): |