module Amber::Render::Filter::Autolink
Constants
- AUTO_EMAIL_LOCAL_RE
- AUTO_EMAIL_RE
- AUTO_LINK_CRE
regexps for determining context, used high-volume
- AUTO_LINK_RE
- BRACKETS
- WORD_PATTERN
Public Class Methods
run(text)
click to toggle source
# File lib/amber/render/filter/autolink.rb, line 10 def self.run(text) auto_link_email_addresses(auto_link_urls(text)) end
Private Class Methods
auto_link_email_addresses(text)
click to toggle source
Turns all email addresses into clickable links.
# File lib/amber/render/filter/autolink.rb, line 60 def self.auto_link_email_addresses(text) text.gsub(AUTO_EMAIL_RE) do text = $& if auto_linked?($`, $') text else #display_text = (block_given?) ? yield(text) : text #display_text = text text.gsub!('@', '@').gsub!('.', '.') %(<a href="mailto:#{text}">#{text}</a>) end end end
auto_link_urls(text)
click to toggle source
Turns all urls into clickable links. If a block is given, each url is yielded and the result is used as the link text.
# File lib/amber/render/filter/autolink.rb, line 33 def self.auto_link_urls(text) text.gsub(AUTO_LINK_RE) do scheme, href = $1, $& punctuation = [] if auto_linked?($`, $') # do not change string; URL is already linked href else # don't include trailing punctuation character as part of the URL while href.sub!(/[^#{WORD_PATTERN}\/-]$/, '') punctuation.push $& if opening = BRACKETS[punctuation.last] and href.scan(opening).size > href.scan(punctuation.last).size href << punctuation.pop break end end #link_text = block_given?? yield(href) : href link_text = href.sub(/^#{scheme}\/\//,'') href = 'http://' + href unless scheme %(<a href="#{href}">#{link_text}</a>) + punctuation.reverse.join('') end end end
auto_linked?(left, right)
click to toggle source
Detects already linked context or position in the middle of a tag
# File lib/amber/render/filter/autolink.rb, line 76 def self.auto_linked?(left, right) (left =~ AUTO_LINK_CRE[0] and right =~ AUTO_LINK_CRE[1]) or (left.rindex(AUTO_LINK_CRE[2]) and $' !~ AUTO_LINK_CRE[3]) end