Class: Card::Content::Chunk::Uri

Inherits:
Abstract show all
Defined in:
card-mod-content/lib/card/content/chunk/uri.rb

Direct Known Subclasses

EmailUri, HostUri

Constant Summary collapse

SCHEMES =
%w[irc http https ftp ssh git sftp file ldap ldaps mailto].freeze
REJECTED_PREFIX_RE =
%w{! ": " ' ](}.map { |s| Regexp.escape s } * "|"

Instance Attribute Summary collapse

Attributes inherited from Abstract

#process_chunk, #text

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

#as_json, #burn_after_reading, #burn_read, #card, #format, full_re, #initialize, #inspect, #reference_code, #result, #to_s

Constructor Details

This class inherits a constructor from Card::Content::Chunk::Abstract

Instance Attribute Details

Returns the value of attribute link_text.



30
31
32
# File 'card-mod-content/lib/card/content/chunk/uri.rb', line 30

def link_text
  @link_text
end

#uriObject (readonly)

Returns the value of attribute uri.



30
31
32
# File 'card-mod-content/lib/card/content/chunk/uri.rb', line 30

def uri
  @uri
end

Class Method Details

.context_ok?(content, chunk_start) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'card-mod-content/lib/card/content/chunk/uri.rb', line 52

def context_ok? content, chunk_start
  preceding_string = content[chunk_start - 2..chunk_start - 1]
  preceding_string !~ /(?:#{REJECTED_PREFIX_RE})$/
end

.full_match(content, prefix) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'card-mod-content/lib/card/content/chunk/uri.rb', line 41

def full_match content, prefix
  prepend_str = if prefix[-1, 1] != ":" && config[:prepend_str]
                  config[:prepend_str]
                else
                  ""
                end
  content = prepend_str + content
  match = super content, prefix
  [match, prepend_str.length]
end

Instance Method Details

#interpret(match, _content) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'card-mod-content/lib/card/content/chunk/uri.rb', line 58

def interpret match, _content
  chunk = match[0]
  last_char = chunk[-1, 1]
  chunk.gsub!(/(?: )+/, "")

  @trailing_punctuation =
    if %w[, . ) ! ? :].member?(last_char)
      @text.chop!
      chunk.chop!
      last_char
    end
  chunk.sub!(/\.$/, "")

  @link_text = chunk
  @uri = ::URI.parse(chunk)
  @process_chunk = process_uri_chunk
rescue ::URI::Error => e
  # warn "rescue parse #{chunk_class}:
  # '#{m}' #{e.inspect} #{e.backtrace*"\n"}"
  Rails.logger.warn "rescue parse #{self.class}: #{e.inspect}"
end