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

Inherits:
Abstract
  • Object
show all
Defined in:
lib/card/content/chunk/uri.rb

Overview

This wiki chunk matches arbitrary URIs, using patterns from the Ruby URI modules. It parses out a variety of fields that could be used by formats to format the links in various ways (shortening domain names, hiding email addresses) It matches email addresses and host.com.au domains without schemes (http://) but adds these on as required.

The heuristic used to match a URI is designed to err on the side of caution. That is, it is more likely to not autolink a URI than it is to accidently autolink something that is not a URI. The reason behind this is it is easier to force a URI link by prefixing ‘http://’ to it than it is to escape and incorrectly marked up non-URI.

I’m using a part of the [ISO 3166-1 Standard] for country name suffixes. The generic names are from www.bnoack.com/data/countrycode2.html)

[iso3166]: http://geotags.com/iso3166/

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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

Returns the value of attribute link_text.



30
31
32
# File '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 '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 '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 '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 '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