Module: Snippr::Links
- Defined in:
- lib/snippr/links.rb
Constant Summary collapse
- HREF_REGEX =
/(href *= *['"])([^'"]*)(['"])/i
Class Method Summary collapse
-
.adjust_link(link) ⇒ Object
Adjusts a complete anchor tag, allowing the custom protocol popup: which will be converted to a javascript call to popup(this).
-
.adjust_url(url) ⇒ Object
Adjusts an url, prepending / and relative url root (context path) as needed.
-
.adjust_urls_except ⇒ Object
Returns the regular expressions used to determine which urls to exclude from adjustment.
-
.adjust_urls_except=(adjust_urls_except) ⇒ Object
Sets the regular expressions used to determine which urls to exclude from adjustment.
-
.relative_url_root ⇒ Object
Returns the relative url root (context path) the application is deployed to.
Class Method Details
.adjust_link(link) ⇒ Object
Adjusts a complete anchor tag, allowing the custom protocol popup: which will be converted to a javascript call to popup(this).
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# 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) ⇒ Object
Adjusts an url, prepending / and relative url root (context path) as needed.
38 39 40 41 42 43 44 |
# 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 ⇒ Object
Returns the regular expressions used to determine which urls to exclude from adjustment.
10 11 12 |
# 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) ⇒ Object
Sets the regular expressions used to determine which urls to exclude from adjustment.
15 16 17 |
# 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 ⇒ Object
Returns the relative url root (context path) the application is deployed to.
47 48 49 50 51 52 53 54 55 56 |
# 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 |