Class: Slack::Messenger::Util::LinkFormatter
- Inherits:
-
Object
- Object
- Slack::Messenger::Util::LinkFormatter
- Defined in:
- lib/slack-messenger/util/link_formatter.rb
Constant Summary collapse
- HTML_PATTERN =
'<a' \ '(?:.*?)' \ 'href=[\'"](?P<link>.+?)[\'"]' \ '(?:.*?)>' \ '(?P<text>.+?)' \ '</a>'
- VALID_URI_CHARS =
'\w\-\.\~\:\/\?\#\[\]\@\!\$\&\'\*\+\,\;\='- MARKDOWN_PATTERN =
Attempt at only matching pairs of parens per the markdown spec spec.commonmark.org/0.27/#links regex101.com/r/komyFe/1
'\[' \ '(?P<text>[^\[\]]*?)' \ '\]' \ '\(' \ '(?P<link>' \ '(?:https?:\/\/|mailto:)' \ "(?:[#{VALID_URI_CHARS}]*?|[#{VALID_URI_CHARS}]*?\\([#{VALID_URI_CHARS}]*?\\)[#{VALID_URI_CHARS}]*?)" \ ')' \ '\)'
- HTML_REGEXP =
UntrustedRegexp.new(HTML_PATTERN, multiline: true)
- MARKDOWN_REGEXP =
UntrustedRegexp.new(MARKDOWN_PATTERN, multiline: true)
Instance Attribute Summary collapse
-
#formats ⇒ Object
readonly
Returns the value of attribute formats.
Class Method Summary collapse
Instance Method Summary collapse
-
#formatted ⇒ Object
rubocop:disable Lint/RescueWithoutErrorClass.
-
#initialize(string, formats: %i[html markdown]) ⇒ LinkFormatter
constructor
A new instance of LinkFormatter.
Constructor Details
#initialize(string, formats: %i[html markdown]) ⇒ LinkFormatter
Returns a new instance of LinkFormatter.
45 46 47 48 |
# File 'lib/slack-messenger/util/link_formatter.rb', line 45 def initialize string, formats: %i[html markdown] @formats = formats @orig = string.respond_to?(:scrub) ? string.scrub : string end |
Instance Attribute Details
#formats ⇒ Object (readonly)
Returns the value of attribute formats.
43 44 45 |
# File 'lib/slack-messenger/util/link_formatter.rb', line 43 def formats @formats end |
Class Method Details
.format(string, **opts) ⇒ Object
38 39 40 |
# File 'lib/slack-messenger/util/link_formatter.rb', line 38 def format string, **opts LinkFormatter.new(string, **opts).formatted end |
Instance Method Details
#formatted ⇒ Object
rubocop:disable Lint/RescueWithoutErrorClass
51 52 53 54 55 56 57 58 |
# File 'lib/slack-messenger/util/link_formatter.rb', line 51 def formatted return @orig unless @orig.respond_to?(:gsub) sub_markdown_links(sub_html_links(@orig)) rescue => e raise e unless RUBY_VERSION < "2.1" && e..include?("invalid byte sequence") raise e, "#{e.}. Consider including the 'string-scrub' gem to strip invalid characters" end |