Skip to content

Commit

Permalink
Merge pull request #410 from chillipeper/html_link_converter_slack
Browse files Browse the repository at this point in the history
Html link converter slack
  • Loading branch information
chillipeper authored Nov 26, 2019
2 parents 43eaf29 + e2aa6ec commit 8948176
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
11 changes: 10 additions & 1 deletion will/backends/io_adapters/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ class SlackMarkdownConverter(MarkdownConverter):
def convert_strong(self, el, text):
return '*%s*' % text if text else ''

def convert_a(self, el, text):
href = el.get('href')
title = el.get('title')
if self.options['autolinks'] and text == href and not title:
# Shortcut syntax
return '<%s>' % href
title_part = ' "%s"' % title.replace('"', r'\"') if title else ''
return '<%s%s|%s>' % (href, title_part, text or '') if href else text or ''


class SlackBackend(IOBackend, SleepMixin, StorageMixin):
friendly_name = "Slack"
Expand Down Expand Up @@ -347,7 +356,7 @@ def send_message(self, event):
})
if hasattr(event, "kwargs") and "html" in event.kwargs and event.kwargs["html"]:
data.update({
"parse": "full",
"parse": "none",
})

headers = {'Accept': 'text/plain'}
Expand Down
23 changes: 23 additions & 0 deletions will/tests/backends/io_adapters/test_slack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from will.backends.io_adapters import slack


class TestSlackMarkdownConverter:
converter = slack.SlackMarkdownConverter()

def test_convert_strong_with_text(self):
slack_strong = self.converter.convert_strong("<b>", "hello")
assert slack_strong == "*hello*"

def test_convert_strong_with_empty_text(self):
slack_strong = self.converter.convert_strong("<b>", "")
assert slack_strong == ""

def test_convert_a_with_text(self):
slack_a = self.converter.convert_a({"href": "https://www.google.com"},
"Google")
assert slack_a == "<https://www.google.com|Google>"

def test_convert_a_with_text_same_as_href(self):
slack_a = self.converter.convert_a({"href": "https://www.google.com"},
"https://www.google.com")
assert slack_a == "<https://www.google.com>"

0 comments on commit 8948176

Please sign in to comment.