forked from slack-ruby/slack-ruby-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatting.rb
41 lines (40 loc) · 1 KB
/
formatting.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true
module Slack
module Messages
module Formatting
class << self
#
# Unescape a message.
# @see https://api.slack.com/docs/formatting
#
def unescape(message)
CGI.unescapeHTML(message.gsub(/[“”]/, '"')
.gsub(/[‘’]/, "'")
.gsub(/<(?<sign>[?@#!]?)(?<dt>.*?)>/) do
sign = Regexp.last_match[:sign]
dt = Regexp.last_match[:dt]
rhs = dt.split('|', 2).last
case sign
when '@', '!'
"@#{rhs}"
when '#'
"##{rhs}"
else
rhs
end
end)
end
#
# Escape a message.
# @see https://api.slack.com/reference/surfaces/formatting#escaping
#
def escape(message)
message
.gsub('&', '&')
.gsub('>', '>')
.gsub('<', '<')
end
end
end
end
end