Skip to content
Newer
Older
100644 137 lines (119 sloc) 5.9 KB
db045db @dhh Initial
dhh authored Nov 24, 2004
1 module ActionView
2 module Helpers #:nodoc:
3 # Provides a set of methods for working with text strings that can help unburden the level of inline Ruby code in the
dfac1ce @dhh Fixed that form helpers would treat string and symbol keys differentl…
dhh authored Mar 6, 2005
4 # templates. In the example below we iterate over a collection of posts provided to the template and prints each title
db045db @dhh Initial
dhh authored Nov 24, 2004
5 # after making sure it doesn't run longer than 20 characters:
6 # <% for post in @posts %>
7 # Title: <%= truncate(post.title, 20) %>
8 # <% end %>
9 module TextHelper
10 # The regular puts and print are outlawed in eRuby. It's recommended to use the <%= "hello" %> form instead of print "hello".
11 # If you absolutely must use a method-based output, you can use concat. It's use like this <% concat "hello", binding %>. Notice that
12 # it doesn't have an equal sign in front. Using <%= concat "hello" %> would result in a double hello.
13 def concat(string, binding)
14 eval("_erbout", binding).concat(string)
15 end
16
17 # Truncates +text+ to the length of +length+ and replaces the last three characters with the +truncate_string+
18 # if the +text+ is longer than +length+.
19 def truncate(text, length = 30, truncate_string = "...")
20 if text.nil? then return end
21 if text.length > length then text[0..(length - 3)] + truncate_string else text end
22 end
23
24 # Highlights the +phrase+ where it is found in the +text+ by surrounding it like
25 # <strong class="highlight">I'm a highlight phrase</strong>. The highlighter can be specialized by
26 # passing +highlighter+ as single-quoted string with \1 where the phrase is supposed to be inserted.
27 # N.B.: The +phrase+ is sanitized to include only letters, digits, and spaces before use.
28 def highlight(text, phrase, highlighter = '<strong class="highlight">\1</strong>')
29 if text.nil? || phrase.nil? then return end
30 text.gsub(/(#{escape_regexp(phrase)})/i, highlighter) unless text.nil?
31 end
dfac1ce @dhh Fixed that form helpers would treat string and symbol keys differentl…
dhh authored Mar 6, 2005
32
db045db @dhh Initial
dhh authored Nov 24, 2004
33 # Extracts an excerpt from the +text+ surrounding the +phrase+ with a number of characters on each side determined
dfac1ce @dhh Fixed that form helpers would treat string and symbol keys differentl…
dhh authored Mar 6, 2005
34 # by +radius+. If the phrase isn't found, nil is returned. Ex:
db045db @dhh Initial
dhh authored Nov 24, 2004
35 # excerpt("hello my world", "my", 3) => "...lo my wo..."
36 def excerpt(text, phrase, radius = 100, excerpt_string = "...")
37 if text.nil? || phrase.nil? then return end
38 phrase = escape_regexp(phrase)
dfac1ce @dhh Fixed that form helpers would treat string and symbol keys differentl…
dhh authored Mar 6, 2005
39
db045db @dhh Initial
dhh authored Nov 24, 2004
40 if found_pos = text =~ /(#{phrase})/i
41 start_pos = [ found_pos - radius, 0 ].max
42 end_pos = [ found_pos + phrase.length + radius, text.length ].min
43
44 prefix = start_pos > 0 ? excerpt_string : ""
45 postfix = end_pos < text.length ? excerpt_string : ""
46
47 prefix + text[start_pos..end_pos].strip + postfix
48 else
49 nil
50 end
51 end
52
53 # Attempts to pluralize the +singular+ word unless +count+ is 1. See source for pluralization rules.
54 def pluralize(count, singular, plural = nil)
55 "#{count} " + if count == 1
56 singular
57 elsif plural
58 plural
59 elsif Object.const_defined?("Inflector")
60 Inflector.pluralize(singular)
dfac1ce @dhh Fixed that form helpers would treat string and symbol keys differentl…
dhh authored Mar 6, 2005
61 else
db045db @dhh Initial
dhh authored Nov 24, 2004
62 singular + "s"
63 end
64 end
65
66 begin
67 require "redcloth"
68
dfac1ce @dhh Fixed that form helpers would treat string and symbol keys differentl…
dhh authored Mar 6, 2005
69 # Returns the text with all the Textile codes turned into HTML-tags.
db045db @dhh Initial
dhh authored Nov 24, 2004
70 # <i>This method is only available if RedCloth can be required</i>.
71 def textilize(text)
6526adc @dhh Fixed textilize to be resilient to getting nil parsed (by using Objec…
dhh authored Mar 9, 2005
72 text.blank? ? "" : RedCloth.new(text, [ :hard_breaks ]).to_html
db045db @dhh Initial
dhh authored Nov 24, 2004
73 end
74
dfac1ce @dhh Fixed that form helpers would treat string and symbol keys differentl…
dhh authored Mar 6, 2005
75 # Returns the text with all the Textile codes turned into HTML-tags, but without the regular bounding <p> tag.
db045db @dhh Initial
dhh authored Nov 24, 2004
76 # <i>This method is only available if RedCloth can be required</i>.
77 def textilize_without_paragraph(text)
78 textiled = textilize(text)
79 if textiled[0..2] == "<p>" then textiled = textiled[3..-1] end
80 if textiled[-4..-1] == "</p>" then textiled = textiled[0..-5] end
81 return textiled
82 end
83 rescue LoadError
84 # We can't really help what's not there
85 end
86
87 begin
88 require "bluecloth"
89
dfac1ce @dhh Fixed that form helpers would treat string and symbol keys differentl…
dhh authored Mar 6, 2005
90 # Returns the text with all the Markdown codes turned into HTML-tags.
db045db @dhh Initial
dhh authored Nov 24, 2004
91 # <i>This method is only available if BlueCloth can be required</i>.
92 def markdown(text)
c8b2491 @dhh Fixed that textilize and markdown would instantiate their engines eve…
dhh authored Dec 19, 2004
93 text.empty? ? "" : BlueCloth.new(text).to_html
db045db @dhh Initial
dhh authored Nov 24, 2004
94 end
95 rescue LoadError
96 # We can't really help what's not there
97 end
98
0369ec7 @dhh Made auto_link the only public method of its clan and added an option…
dhh authored Feb 19, 2005
99 # Turns all urls and email addresses into clickable links. The +link+ parameter can limit what should be linked.
100 # Options are :all (default), :email_addresses, and :urls.
101 #
102 # Example:
103 # auto_link("Go to http://www.rubyonrails.com and say hello to david@loudthinking.com") =>
dfac1ce @dhh Fixed that form helpers would treat string and symbol keys differentl…
dhh authored Mar 6, 2005
104 # Go to <a href="http://www.rubyonrails.com">http://www.rubyonrails.com</a> and
0369ec7 @dhh Made auto_link the only public method of its clan and added an option…
dhh authored Feb 19, 2005
105 # say hello to <a href="mailto:david@loudthinking.com">david@loudthinking.com</a>
106 def auto_link(text, link = :all)
107 case link
108 when :all then auto_link_urls(auto_link_email_addresses(text))
109 when :email_addresses then auto_link_email_addresses(text)
110 when :urls then auto_link_urls(text)
111 end
b305756 @dhh Added TextHelper#auto_link, TextHelper#auto_link_urls, and TextHelper…
dhh authored Feb 18, 2005
112 end
113
db045db @dhh Initial
dhh authored Nov 24, 2004
114 # Turns all links into words, like "<a href="something">else</a>" to "else".
115 def strip_links(text)
116 text.gsub(/<a.*>(.*)<\/a>/m, '\1')
117 end
dfac1ce @dhh Fixed that form helpers would treat string and symbol keys differentl…
dhh authored Mar 6, 2005
118
db045db @dhh Initial
dhh authored Nov 24, 2004
119 private
120 # Returns a version of the text that's safe to use in a regular expression without triggering engine features.
121 def escape_regexp(text)
122 text.gsub(/([\\|?+*\/\)\(])/) { |m| "\\#{$1}" }
123 end
0369ec7 @dhh Made auto_link the only public method of its clan and added an option…
dhh authored Feb 19, 2005
124
125 # Turns all urls into clickable links.
126 def auto_link_urls(text)
127 text.gsub(/([^=><!:'"\/]|^)((http[s]?:\/\/)|(www\.))(\S+\b\/?)([[:punct:]]*)(\s|$)/, '\1<a href="\3\4\5">\3\4\5</a>\6\7')
128 end
129
130 # Turns all email addresses into clickable links.
131 def auto_link_email_addresses(text)
132 text.gsub(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/, '<a href="mailto:\1">\1</a>')
133 end
db045db @dhh Initial
dhh authored Nov 24, 2004
134 end
135 end
dfac1ce @dhh Fixed that form helpers would treat string and symbol keys differentl…
dhh authored Mar 6, 2005
136 end
Something went wrong with that request. Please try again.