Sha256: 4733807401241f84593e4ca0a5f6c529ca4c767351e8010c88056b82dc398d66

Contents?: true

Size: 1.77 KB

Versions: 19

Compression:

Stored size: 1.77 KB

Contents

module ActionView::Helpers::TextHelper
  
  # Turns all email addresses into clickable links.  If a block is given,
  # each email is yielded and the result is used as the link text.
  # Example:
  #   auto_link_email_addresses(post.body) do |text|
  #     truncate(text, 15)
  #   end
  def auto_link_email_addresses(content)
    re = %r{
            (<\w+[^\<\>]*?\>|[\s[:punct:]]|mailto:|^) # leading text
            (
              [\w\.\!\#\$\%\-\+\.\/]+                 # username
              \@
              [-\w]+                                  # subdomain or domain
              (?:\.[-\w]+)+                           # remaining subdomains or domain
            ) 
            ([[:punct:]]|\s|\<|$)                     # trailing text
           }x
    
    content.gsub(re) do |all|
      # this seems incredibly stupid, but $1, $2, $3 weren't being set
      re.match(all)
      
      a, b, c = $1, $2, $3
      
      if a =~ /\<a\s|[='"]$/i
        all
      elsif a =~ /mailto:/i
        url_src = b
        
        url = ''
        url_src.length.times do |i|
          url << (i % 2 == 0 ? sprintf("%%%x", url_src[i].ord) : url_src[i])
        end
        
        "#{a}#{url}#{c}"
      else
        url_src = b
        text_src = b
        text_src = yield(text_src) if block_given?
        
        unless url_src && text_src
          all
        else
          url = ''
          text = ''
          url_src.length.times do |i|
            url << (i % 2 == 0 ? sprintf("%%%x", url_src[i].ord) : url_src[i])
          end
          text_src.length.times do |i|
            text << (i % 4 == 0 ? '<span>' << text_src[i] << '</span>' : text_src[i])
          end
          
          "#{a}<a href=\"mailto:#{url}\">#{text}</a>#{c}"
        end
      end
    end.html_safe
  end
  
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
imagine_cms-5.2.6 lib/auto_link_email_addresses.rb
imagine_cms-5.2.5 lib/auto_link_email_addresses.rb
imagine_cms-5.2.4 lib/auto_link_email_addresses.rb
imagine_cms-5.2.3 lib/auto_link_email_addresses.rb
imagine_cms-5.2.2 lib/auto_link_email_addresses.rb
imagine_cms-5.2.1 lib/auto_link_email_addresses.rb
imagine_cms-5.2.0 lib/auto_link_email_addresses.rb
imagine_cms-4.2.4 lib/auto_link_email_addresses.rb
imagine_cms-4.1.4 lib/auto_link_email_addresses.rb
imagine_cms-4.2.3 lib/auto_link_email_addresses.rb
imagine_cms-4.2.2 lib/auto_link_email_addresses.rb
imagine_cms-4.2.1 lib/auto_link_email_addresses.rb
imagine_cms-4.2.0 lib/auto_link_email_addresses.rb
imagine_cms-4.1.3 lib/auto_link_email_addresses.rb
imagine_cms-4.1.2 lib/auto_link_email_addresses.rb
imagine_cms-4.1.1 lib/auto_link_email_addresses.rb
imagine_cms-4.1.0 lib/auto_link_email_addresses.rb
imagine_cms-4.0.1 lib/auto_link_email_addresses.rb
imagine_cms-4.0.0 lib/auto_link_email_addresses.rb