| Class | Texy::Url |
| In: |
lib/texy/url.rb
|
| Parent: | Object |
URL storage
Analyse type of URL and convert it to valid URL or textual representation
| ABSOLUTE | = | 1 |
| RELATIVE | = | 2 |
| = | 4 | |
| IMAGE | = | 8 |
| value | [RW] |
Indicates whether URL is absolute
# File lib/texy/url.rb, line 82 def absolute? @flags & ABSOLUTE != 0 end
Returns textual representation of URL
# File lib/texy/url.rb, line 140 def as_textual if email? @texy.obfuscate_email? ? value.gsub('@', " (at) ") : value elsif absolute? url = value lower = url.downcase if lower[0..4] == 'www.' url = "none://#{url}" elsif lower[0..4] == 'ftp.' url = "none://#{url}" end parts = URI.parse(url) return value unless parts res = '' if parts.scheme && parts.scheme != 'none' res += parts.scheme + '://' end if parts.host res += parts.host end if parts.path res += (parts.path.length > 16 ? '/...' + parts.path.gsub(/^.*?(.{0,12})$/, '\1') : parts.path) end if parts.query res += parts.query.length > 4 ? '?...' : "?#{parts.query}" elsif parts.fragment res += parts.fragment.length > 4 ? '#...' : "##{parts.fragment}" end res else value end end
Returns URL formatted for HTML attributes etc.
# File lib/texy/url.rb, line 103 def as_url # output is cached return @url if @url return @url = '' if value.to_s.empty? # email URL if email? # obfuscating against spam robots if @texy.obfuscate_email? @url = 'mai' s = "lto:#{value}" s.each_byte do |i| @url += "&##{i};" end else @url = "mailto:#{value}" end @url elsif absolute? # absolute URL lower = value.downcase # must begins with 'http://' or 'ftp://' if lower[0..3] == 'www.' return @url = "http://#{value}" elsif lower[0..3] == 'ftp.' return @url = "ftp://#{value}" end @url = value else # relative URL @url = @root.to_s + value end end
Indicates whether URL is email address (mailto://)
# File lib/texy/url.rb, line 91 def email? @flags & EMAIL != 0 end
Indicates whether URL is marked as ‘image’
# File lib/texy/url.rb, line 96 def image? @flags & IMAGE != 0 end
Sets URL properties
Parameters:
value:: text written in document root:: root, for relative URL's is_image:: image indicator (user usage)
# File lib/texy/url.rb, line 62 def set(value, root = nil, is_image = false) self.value = value.strip if value @root = root.gsub(/[\/\\]\Z/, '') + '/' if root # will be completed on demand @url = nil # detect URL type if /^#{PATTERN_EMAIL}$/i =~ value # email @flags = EMAIL elsif /^(https?:\/\/|ftp:\/\/|www\.|ftp\.|\/)/i =~ value # absolute URL @flags = ABSOLUTE | (is_image ? IMAGE : 0) else # relative @flags = RELATIVE | (is_image ? IMAGE : 0) end end