Provides a set of methods for working with text strings that can help unburden the level of inline Ruby code in the templates. In the example below we iterate over a collection of posts provided to the template and prints each title after making sure it doesn’t run longer than 20 characters:
<% for post in @posts %>
Title: <%= truncate(post.title, 20) %>
<% end %>
Extracts an excerpt from the text surrounding the phrase with a number of characters on each side determined by radius. If the phrase isn’t found, nil is returned. Ex:
excerpt("hello my world", "my", 3) => "...lo my wo..."
[ show source ]
# File lib/action_view/helpers/text_helper.rb, line 59
59: def excerpt(text, phrase, radius = 100, excerpt_string = "...")
60: if text.nil? || phrase.nil? then return end
61: phrase = escape_regexp(phrase)
62:
63: if found_pos = text =~ /(#{phrase})/i
64: start_pos = [ found_pos - radius, 0 ].max
65: end_pos = [ found_pos + phrase.length + radius, text.length ].min
66:
67: prefix = start_pos > 0 ? excerpt_string : ""
68: postfix = end_pos < text.length ? excerpt_string : ""
69:
70: prefix + text[start_pos..end_pos].strip + postfix
71: else
72: nil
73: end
74: end
Highlights the phrase where it is found in the text by surrounding it like <strong class="highlight">I’m a highlight phrase</strong>. The highlighter can be specialized by passing highlighter as single-quoted string with \1 where the phrase is supposed to be inserted. N.B.: The phrase is sanitized to include only letters, digits, and spaces before use.
[ show source ]
# File lib/action_view/helpers/text_helper.rb, line 51
51: def highlight(text, phrase, highlighter = '<strong class="highlight">\1</strong>')
52: if text.nil? || phrase.nil? then return end
53: text.gsub(/(#{escape_regexp(phrase)})/i, highlighter) unless text.nil?
54: end
Turns all links into words, like "<a href="something">else</a>" to "else".
[ show source ]
# File lib/action_view/helpers/text_helper.rb, line 98
98: def strip_links(text)
99: text.gsub(/<a.*>(.*)<\/a>/, '\1')
100: end
Returns the text with all the Textile codes turned into HTML-tags. This method is only available if RedCloth can be required.
[ show source ]
# File lib/action_view/helpers/text_helper.rb, line 81
81: def textilize(text)
82: RedCloth.new(text).to_html
83: end
Returns the text with all the Textile codes turned into HTML-tags, but without the regular bounding <p> tag. This method is only available if RedCloth can be required.
[ show source ]
# File lib/action_view/helpers/text_helper.rb, line 87
87: def textilize_without_paragraph(text)
88: textiled = textilize(text)
89: if textiled[0..2] == "<p>" then textiled = textiled[3..-1] end
90: if textiled[-4..-1] == "</p>" then textiled = textiled[0..-5] end
91: return textiled
92: end
Truncates text to the length of length and replaces the last three characters with the truncate_string if the text is longer than length.
[ show source ]
# File lib/action_view/helpers/text_helper.rb, line 42
42: def truncate(text, length = 30, truncate_string = "...")
43: if text.nil? then return end
44: if text.length > length then text[0..(length - 3)] + truncate_string else text end
45: end
Returns a version of the text that’s safe to use in a regular expression without triggering engine features.
[ show source ]
# File lib/action_view/helpers/text_helper.rb, line 104
104: def escape_regexp(text)
105: text.gsub(/([\\|?+*\/\)\(])/) { |m| "\\#{$1}" }
106: end