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

Methods

absolute?   as_textual   as_url   email?   image?   new   relative?   set  

Constants

ABSOLUTE = 1
RELATIVE = 2
EMAIL = 4
IMAGE = 8

Attributes

value  [RW] 

Public Class methods

Creates a new Url object

Parameters:

  texy:: association with root Texy object

[Source]

# File lib/texy/url.rb, line 52
        def initialize(texy)
            @texy =  texy
        end

Public Instance methods

Indicates whether URL is absolute

[Source]

# File lib/texy/url.rb, line 82
        def absolute?
            @flags & ABSOLUTE != 0
        end

Returns textual representation of URL

[Source]

# 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.

[Source]

# 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://)

[Source]

# File lib/texy/url.rb, line 91
        def email?
            @flags & EMAIL != 0
        end

Indicates whether URL is marked as ‘image’

[Source]

# File lib/texy/url.rb, line 96
        def image?
            @flags & IMAGE != 0
        end

[Source]

# File lib/texy/url.rb, line 86
        def relative?
            @flags & ABSOLUTE == 0
        end

Sets URL properties

Parameters:

  value::     text written in document
  root::      root, for relative URL's
  is_image::  image indicator (user usage)

[Source]

# 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

[Validate]