Module: Vintage::Helpers
- Defined in:
- lib/vintage/helpers.rb
Overview
A module of various helpers, many of them copied or based on Rails helpers.
Constant Summary collapse
- AUTO_LINK_RE =
Borrowed from Rails, a regex to pick out things to link in text
%r{ ( # leading text <\w+.*?>| # leading HTML tag, or [^=!:'"/]| # leading punctuation, or ^ # beginning of line ) ( (?:https?://)| # protocol spec, or (?:www\.) # www.* ) ( [-\w]+ # subdomain or domain (?:\.[-\w]+)* # remaining subdomains or domain (?::\d+)? # port (?:/(?:(?:[~\w\+@%-]|(?:[,.;:][^\s$]))+)?)* # path (?:\?[\w\+@%&=.;-]+)? # query string (?:\#[\w\-]*)? # trailing anchor ) ([[:punct:]]|\s|<|$) # trailing text }x
Instance Method Summary collapse
-
#auto_link(text) ⇒ Object
Auto link URLs and e-mail addresses in
text. -
#auto_link_email_addresses(text) ⇒ Object
Auto link e-mail addresses in
text. -
#auto_link_urls(text) ⇒ Object
Auto link URLs in +text.
-
#button_to(url, text = "Click Here") ⇒ Object
Create a button that submits to another URL.
-
#excerpt(text, start = 0, stop = 20, padding = "...") ⇒ Object
Grab an excerpt from
text, starting atstartand stopping atstop. -
#highlight(text, phrases, highlighter = '<strong class="highlight">\1</strong>') ⇒ Object
Highlight an array of
phrasesintext. -
#link_to(text, url) ⇒ Object
Link to a given
urlwith the giventext. -
#mail_to(address, text = nil) ⇒ Object
Create a
mailto:link foraddress. -
#redirect_to(url) ⇒ Object
Redirect to a URL or other action using a 301 status code and
Locationheader. -
#truncate(text, length = 20, ending = "...") ⇒ Object
Truncate
textto the length specified inlengthwithendingas the text appearing on the end.
Instance Method Details
#auto_link(text) ⇒ Object
Auto link URLs and e-mail addresses in text.
39 40 41 |
# File 'lib/vintage/helpers.rb', line 39 def auto_link(text) auto_link_email_addresses(auto_link_urls(text)) end |
#auto_link_email_addresses(text) ⇒ Object
Auto link e-mail addresses in text.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/vintage/helpers.rb', line 44 def auto_link_email_addresses(text) body = text.dup text.gsub(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do text = $1 if body.match(/<a\b[^>]*>(.*)(#{Regexp.escape(text)})(.*)<\/a>/) text else %{<a href="mailto:#{text}">#{text}</a>} end end end |
#auto_link_urls(text) ⇒ Object
Auto link URLs in +text.
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/vintage/helpers.rb', line 58 def auto_link_urls(text) text.gsub(AUTO_LINK_RE) do all, a, b, c, d = $&, $1, $2, $3, $4 if a =~ /<a\s/i # don't replace URL's that are already linked all else text = b + c %(#{a}<a href="#{b=="www."?"http://www.":b}#{c}">#{text}</a>#{d}) end end end |
#button_to(url, text = "Click Here") ⇒ Object
Create a button that submits to another URL.
100 101 102 |
# File 'lib/vintage/helpers.rb', line 100 def (url, text = "Click Here") "<form method='GET' action='#{url}'><div class='button-to'><input type='submit' value='#{text}'></div></form>" end |
#excerpt(text, start = 0, stop = 20, padding = "...") ⇒ Object
Grab an excerpt from text, starting at start and stopping at stop. The padding argument lets you define what should be on eithe side of the excerpt.
74 75 76 77 78 |
# File 'lib/vintage/helpers.rb', line 74 def excerpt(text, start = 0, stop = 20, padding = "...") return "" if text.nil? (padding if start > 0).to_s + text[start..(start + stop)] + padding end |
#highlight(text, phrases, highlighter = '<strong class="highlight">\1</strong>') ⇒ Object
Highlight an array of phrases in text. The highlighter argument lets you set how to highlight the text.
82 83 84 85 86 87 88 89 |
# File 'lib/vintage/helpers.rb', line 82 def highlight(text, phrases, highlighter = '<strong class="highlight">\1</strong>') if text.blank? || phrases.blank? text else match = Array(phrases).map { |p| Regexp.escape(p) }.join('|') text.gsub(/(#{match})/i, highlighter) end end |
#link_to(text, url) ⇒ Object
Link to a given url with the given text.
34 35 36 |
# File 'lib/vintage/helpers.rb', line 34 def link_to(text, url) "<a href='#{url}' title='#{text}'>#{text}</a>" end |
#mail_to(address, text = nil) ⇒ Object
Create a mailto: link for address. You can optionally provide text for the link’s text.
106 107 108 |
# File 'lib/vintage/helpers.rb', line 106 def mail_to(address, text = nil) "<a href='mailto:#{address}'>#{text || address}</a>" end |
#redirect_to(url) ⇒ Object
Redirect to a URL or other action using a 301 status code and Location header.
28 29 30 31 |
# File 'lib/vintage/helpers.rb', line 28 def redirect_to(url) response.code = 301 response.headers['Location'] = url end |
#truncate(text, length = 20, ending = "...") ⇒ Object
Truncate text to the length specified in length with ending as the text appearing on the end.
93 94 95 96 97 |
# File 'lib/vintage/helpers.rb', line 93 def truncate(text, length = 20, ending = "...") return "" if text.nil? text[0..length] + ending end |