root/lib/vintage/helpers.rb

Revision 1, 3.9 kB (checked in by jeremymcanal..@gmail.com, 9 months ago)

--

Line 
1 module Vintage
2   # A module of various helpers, many of them copied or based on Rails helpers.
3   module Helpers
4     # Borrowed from Rails, a regex to pick out things to link in text
5     AUTO_LINK_RE = %r{
6                     (                          # leading text
7                       <\w+.*?>|                # leading HTML tag, or
8                       [^=!:'"/]|               # leading punctuation, or
9                       ^                        # beginning of line
10                     )
11                     (
12                       (?:https?://)|           # protocol spec, or
13                       (?:www\.)                # www.*
14                     )
15                     (
16                       [-\w]+                   # subdomain or domain
17                       (?:\.[-\w]+)*            # remaining subdomains or domain
18                       (?::\d+)?                # port
19                       (?:/(?:(?:[~\w\+@%-]|(?:[,.;:][^\s$]))+)?)* # path
20                       (?:\?[\w\+@%&=.;-]+)?     # query string
21                       (?:\#[\w\-]*)?           # trailing anchor
22                     )
23                     ([[:punct:]]|\s|<|$)       # trailing text
24                    }x unless const_defined?(:AUTO_LINK_RE)
25    
26     # Redirect to a URL or other action using a 301 status code
27     # and +Location+ header.
28     def redirect_to(url)
29       response.code = 301
30       response.headers['Location'] = url
31     end
32    
33     # Link to a given +url+ with the given +text+.
34     def link_to(text, url)
35       "<a href='#{url}' title='#{text}'>#{text}</a>"
36     end
37    
38     # Auto link URLs and e-mail addresses in +text+.
39     def auto_link(text)
40       auto_link_email_addresses(auto_link_urls(text))
41     end
42    
43     # Auto link e-mail addresses in +text+.
44     def auto_link_email_addresses(text)
45       body = text.dup
46       text.gsub(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
47         text = $1
48          
49         if body.match(/<a\b[^>]*>(.*)(#{Regexp.escape(text)})(.*)<\/a>/)
50           text
51         else
52           %{<a href="mailto:#{text}">#{text}</a>}
53         end
54       end
55     end
56    
57     # Auto link URLs in +text.
58     def auto_link_urls(text)
59       text.gsub(AUTO_LINK_RE) do
60         all, a, b, c, d = $&, $1, $2, $3, $4
61    
62         if a =~ /<a\s/i # don't replace URL's that are already linked
63           all
64         else
65           text = b + c
66           %(#{a}<a href="#{b=="www."?"http://www.":b}#{c}">#{text}</a>#{d})
67         end
68       end
69     end
70  
71     # Grab an excerpt from +text+, starting at +start+ and stopping at +stop+.
72     # The +padding+ argument lets you define what should be on eithe side of the
73     # excerpt.
74     def excerpt(text, start = 0, stop = 20, padding = "...")
75       return "" if text.nil?
76      
77       (padding if start > 0).to_s + text[start..(start + stop)] + padding
78     end
79  
80     # Highlight an array of +phrases+ in +text+.  The +highlighter+ argument lets you set
81     # how to highlight the text.
82     def highlight(text, phrases, highlighter = '<strong class="highlight">\1</strong>')
83       if text.blank? || phrases.blank?
84         text
85       else
86         match = Array(phrases).map { |p| Regexp.escape(p) }.join('|')
87         text.gsub(/(#{match})/i, highlighter)
88       end
89     end
90  
91     # Truncate +text+ to the length specified in +length+ with +ending+
92     # as the text appearing on the end.
93     def truncate(text, length = 20, ending = "...")
94       return "" if text.nil?
95      
96       text[0..length] + ending
97     end
98  
99     # Create a button that submits to another URL.
100     def button_to(url, text = "Click Here")
101       "<form method='GET' action='#{url}'><div class='button-to'><input type='submit' value='#{text}'></div></form>"
102     end
103  
104     # Create a +mailto:+ link for +address+.  You can optionally provide
105     # +text+ for the link's text.
106     def mail_to(address, text = nil)
107       "<a href='mailto:#{address}'>#{text || address}</a>"
108     end
109   end
110 end
Note: See TracBrowser for help on using the browser.