Class: Card::Content::Chunk::Uri
- Defined in:
- mod/content/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][iso3166] for country name suffixes. The generic names are from www.bnoack.com/data/countrycode2.html) [iso3166]: http://geotags.com/iso3166/
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
-
#link_text ⇒ Object
readonly
Returns the value of attribute link_text.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Attributes inherited from Abstract
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
#link_text ⇒ Object (readonly)
Returns the value of attribute link_text.
30 31 32 |
# File 'mod/content/lib/card/content/chunk/uri.rb', line 30 def link_text @link_text end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
30 31 32 |
# File 'mod/content/lib/card/content/chunk/uri.rb', line 30 def uri @uri end |
Class Method Details
.context_ok?(content, chunk_start) ⇒ Boolean
52 53 54 55 |
# File '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 '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 '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 |