module Snippr::Links
Constants
- HREF_REGEX
Public Class Methods
adjust_link(link)
click to toggle source
Adjusts a complete anchor tag, allowing the custom protocol popup: which will be converted to a javascript call to popup(this).
# File lib/snippr/links.rb, line 20 def self.adjust_link(link) return link if link !~ HREF_REGEX url = $2 if url =~ /popup:\/*(.*)$/i url = adjust_url $1 onclick = "onclick=\"if (typeof popup == 'undefined') { return true; } else { popup(this); return false; }\"" # replace an existing onclick (if present) link_with_onclick = link.gsub /onclick *= *['"][^'"]*['"]/i, onclick # add a new onclick (when there was no existing onclick) link_with_onclick = link.gsub /(^[^>]+)>/, "\\1 #{onclick}>" if link_with_onclick == link link = link_with_onclick else url = adjust_url url end link.gsub HREF_REGEX, "\\1#{url}\\3" end
adjust_url(url)
click to toggle source
Adjusts an url, prepending / and relative url root (context path) as needed.
# File lib/snippr/links.rb, line 38 def self.adjust_url(url) adjust_urls_except.each do |regex| return url if url =~ regex end root = relative_url_root url.gsub(/^(#{root}|\/)?/, root) end
adjust_urls_except()
click to toggle source
Returns the regular expressions used to determine which urls to exclude from adjustment.
# File lib/snippr/links.rb, line 10 def self.adjust_urls_except @@adjust_urls_except ||= [/^#/, /^[a-z]+:/i] end
adjust_urls_except=(adjust_urls_except)
click to toggle source
Sets the regular expressions used to determine which urls to exclude from adjustment.
# File lib/snippr/links.rb, line 15 def self.adjust_urls_except=(adjust_urls_except) @@adjust_urls_except = adjust_urls_except end
relative_url_root()
click to toggle source
Returns the relative url root (context path) the application is deployed to.
# File lib/snippr/links.rb, line 47 def self.relative_url_root if defined? ActionController::Base root = ActionController::Base.config.relative_url_root || '/' root = "/#{root}" unless root.start_with?('/') root << '/' unless root.end_with?('/') root else '/' end end