0
module Helpers #:nodoc:
0
- # The TextHelper module provides a set of methods for filtering, formatting
0
- # and transforming strings, which can reduce the amount of inline Ruby code in
0
- # your views. These helper methods extend ActionView making them callable
0
+ # The TextHelper module provides a set of methods for filtering, formatting
0
+ # and transforming strings, which can reduce the amount of inline Ruby code in
0
+ # your views. These helper methods extend ActionView making them callable
0
# within your template files.
0
- # The preferred method of outputting text in your views is to use the
0
- # <%= "text" %> eRuby syntax. The regular _puts_ and _print_ methods
0
- # do not operate as expected in an eRuby code block. If you absolutely must
0
+ # The preferred method of outputting text in your views is to use the
0
+ # <%= "text" %> eRuby syntax. The regular _puts_ and _print_ methods
0
+ # do not operate as expected in an eRuby code block. If you absolutely must
0
# output text within a non-output code block (i.e., <% %>), you can use the concat method.
0
- # concat "hello", binding
0
+ # concat "hello", binding
0
# # is the equivalent of <%= "hello" %>
0
# if (logged_in == true):
0
if RUBY_VERSION < '1.9'
0
- # If +text+ is longer than +length+, +text+ will be truncated to the length of
0
+ # If +text+ is longer than +length+, +text+ will be truncated to the length of
0
# +length+ (defaults to 30) and the last characters will be replaced with the +truncate_string+
0
- # truncate("Once upon a time in a world far far away", 14)
0
+ # truncate("Once upon a time in a world far far away", 14)
0
- # truncate("Once upon a time in a world far far away")
0
+ # truncate("Once upon a time in a world far far away")
0
# # => Once upon a time in a world f...
0
# truncate("And they found that many people were sleeping better.", 25, "(clipped)")
0
# Highlights one or more +phrases+ everywhere in +text+ by inserting it into
0
- # a +highlighter+ string. The highlighter can be specialized by passing +highlighter+
0
+ # a +highlighter+ string. The highlighter can be specialized by passing +highlighter+
0
# as a single-quoted string with \1 where the phrase is to be inserted (defaults to
0
# '<strong class="highlight">\1</strong>')
0
- # highlight('You searched for: rails', 'rails')
0
+ # highlight('You searched for: rails', 'rails')
0
# # => You searched for: <strong class="highlight">rails</strong>
0
# highlight('You searched for: ruby, rails, dhh', 'actionpack')
0
- # # => You searched for: ruby, rails, dhh
0
+ # # => You searched for: ruby, rails, dhh
0
- # highlight('You searched for: rails', ['for', 'rails'], '<em>\1</em>')
0
+ # highlight('You searched for: rails', ['for', 'rails'], '<em>\1</em>')
0
# # => You searched <em>for</em>: <em>rails</em>
0
# highlight('You searched for: rails', 'rails', "<a href='search?q=\1'>\1</a>")
0
# # => You searched for: <a href='search?q=rails>rails</a>
0
def highlight(text, phrases, highlighter = '<strong class="highlight">\1</strong>')
0
if RUBY_VERSION < '1.9'
0
- # Extracts an excerpt from +text+ that matches the first instance of +phrase+.
0
+ # Extracts an excerpt from +text+ that matches the first instance of +phrase+.
0
# The +radius+ expands the excerpt on each side of the first occurrence of +phrase+ by the number of characters
0
# defined in +radius+ (which defaults to 100). If the excerpt radius overflows the beginning or end of the +text+,
0
# then the +excerpt_string+ will be prepended/appended accordingly. The resulting string will be stripped in any case.
0
# If the +phrase+ isn't found, nil is returned.
0
- # excerpt('This is an example', 'an', 5)
0
+ # excerpt('This is an example', 'an', 5)
0
# # => "...s is an exam..."
0
- # excerpt('This is an example', 'is', 5)
0
+ # excerpt('This is an example', 'is', 5)
0
- # excerpt('This is an example', 'is')
0
+ # excerpt('This is an example', 'is')
0
# # => "This is an example"
0
- # excerpt('This next thing is an example', 'ex', 2)
0
+ # excerpt('This next thing is an example', 'ex', 2)
0
# excerpt('This is also an example', 'an', 8, '<chop> ')
0
- # Attempts to pluralize the +singular+ word unless +count+ is 1. If +plural+
0
- # is supplied, it will use that when count is > 1, if the ActiveSupport Inflector
0
- # is loaded, it will use the Inflector to determine the plural form, otherwise
0
- # it will just add an 's' to the +singular+ word.
0
+ # Attempts to pluralize the +singular+ word unless +count+ is 1. If
0
+ # +plural+ is supplied, it will use that when count is > 1, otherwise
0
+ # it will use the Inflector to determine the plural form
0
- # pluralize(1, 'person')
0
+ # pluralize(1, 'person')
0
- # pluralize(2, 'person')
0
+ # pluralize(2, 'person')
0
- # pluralize(3, 'person', 'users')
0
+ # pluralize(3, 'person', 'users')
0
# pluralize(0, 'person')
0
def pluralize(count, singular, plural = nil)
0
- "#{count || 0} " + if count == 1 || count == '1'
0
- elsif Object.const_defined?("Inflector")
0
- Inflector.pluralize(singular)
0
+ "#{count || 0} " + ((count == 1 || count == '1') ? singular : (plural || singular.pluralize))
0
# Wraps the +text+ into lines no longer than +line_width+ width. This method
0
- # Returns the text with all the Textile codes turned into HTML tags,
0
+ # Returns the text with all the Textile codes turned into HTML tags,
0
# but without the bounding <p> tag that RedCloth adds.
0
# You can learn more about Textile's syntax at its website[http://www.textism.com/tools/textile].
0
# # => "<p>We like to <em>write</em> <code>code</code>, not just <em>read</em> it!</p>"
0
# markdown("The [Markdown website](http://daringfireball.net/projects/markdown/) has more information.")
0
- # # => "<p>The <a href="http://daringfireball.net/projects/markdown/">Markdown website</a>
0
+ # # => "<p>The <a href="http://daringfireball.net/projects/markdown/">Markdown website</a>
0
# # has more information.</p>"
0
# markdown('')
0
- # # => '<p><img src="http://rubyonrails.com/images/rails.png" alt="The ROR logo" title="Ruby on Rails" /></p>'
0
+ # # => '<p><img src="http://rubyonrails.com/images/rails.png" alt="The ROR logo" title="Ruby on Rails" /></p>'
0
text.blank? ? "" : BlueCloth.new(text).to_html
0
# We can't really help what's not there
0
# Returns +text+ transformed into HTML using simple formatting rules.
0
- # Two or more consecutive newlines(<tt>\n\n</tt>) are considered as a
0
+ # Two or more consecutive newlines(<tt>\n\n</tt>) are considered as a
0
# paragraph and wrapped in <tt><p></tt> tags. One newline (<tt>\n</tt>) is
0
# considered as a linebreak and a <tt><br /></tt> tag is appended. This
0
- # method does not remove the newlines from the +text+.
0
+ # method does not remove the newlines from the +text+.
0
- # You can pass any HTML attributes into <tt>html_options</tt>. These
0
+ # You can pass any HTML attributes into <tt>html_options</tt>. These
0
# will be added to all created paragraphs.
0
# my_text = "Here is some basic text...\n...with a line break."
0
- # Turns all URLs and e-mail addresses into clickable links. The +link+ parameter
0
+ # Turns all URLs and e-mail addresses into clickable links. The +link+ parameter
0
# will limit what should be linked. You can add HTML attributes to the links using
0
- # +href_options+. Options for +link+ are <tt>:all</tt> (default),
0
- # <tt>:email_addresses</tt>, and <tt>:urls</tt>. If a block is given, each URL and
0
+ # +href_options+. Options for +link+ are <tt>:all</tt> (default),
0
+ # <tt>:email_addresses</tt>, and <tt>:urls</tt>. If a block is given, each URL and
0
# e-mail address is yielded and the result is used as the link text.
0
- # auto_link("Go to http://www.rubyonrails.org and say hello to david@loudthinking.com")
0
+ # auto_link("Go to http://www.rubyonrails.org and say hello to david@loudthinking.com")
0
# # => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and
0
# # say hello to <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
0
# auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :urls)
0
- # # => "Visit <a href=\"http://www.loudthinking.com/\">http://www.loudthinking.com/</a>
0
+ # # => "Visit <a href=\"http://www.loudthinking.com/\">http://www.loudthinking.com/</a>
0
# # or e-mail david@loudthinking.com"
0
# auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :email_addresses)
0
# auto_link(post_body, :all, :target => '_blank') do |text|
0
- # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>.
0
+ # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>.
0
# Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
0
def auto_link(text, link = :all, href_options = {}, &block)
0
return '' if text.blank?
0
when :urls then auto_link_urls(text, href_options, &block)
0
# Creates a Cycle object whose _to_s_ method cycles through elements of an
0
- # array every time it is called. This can be used for example, to alternate
0
- # classes for table rows. You can use named cycles to allow nesting in loops.
0
- # Passing a Hash as the last parameter with a <tt>:name</tt> key will create a
0
- # named cycle. You can manually reset a cycle by calling reset_cycle and passing the
0
+ # array every time it is called. This can be used for example, to alternate
0
+ # classes for table rows. You can use named cycles to allow nesting in loops.
0
+ # Passing a Hash as the last parameter with a <tt>:name</tt> key will create a
0
+ # named cycle. You can manually reset a cycle by calling reset_cycle and passing the
0
# # Alternate CSS classes for even and odd numbers...
0
# # Cycle CSS classes for rows, and text colors for values within each row
0
- # @items = x = [{:first => 'Robert', :middle => 'Daniel', :last => 'James'},
0
- # {:first => 'Emily', :middle => 'Shannon', :maiden => 'Pike', :last => 'Hicks'},
0
+ # @items = x = [{:first => 'Robert', :middle => 'Daniel', :last => 'James'},
0
+ # {:first => 'Emily', :middle => 'Shannon', :maiden => 'Pike', :last => 'Hicks'},
0
# {:first => 'June', :middle => 'Dae', :last => 'Jones'}]
0
# <% @items.each do |item| %>
0
# <tr class="<%= cycle("even", "odd", :name => "row_class") -%>">
0
- # Resets a cycle so that it starts from the first element the next time
0
+ # Resets a cycle so that it starts from the first element the next time
0
# it is called. Pass in +name+ to reset a named cycle.
0
def initialize(first_value, *values)
0
@values = values.unshift(first_value)
0
@_cycles = Hash.new unless defined?(@_cycles)
0
def set_cycle(name, cycle_object)
0
@_cycles = Hash.new unless defined?(@_cycles)
0
@_cycles[name] = cycle_object
0
<\w+.*?>| # leading HTML tag, or
0
- [^=!:'"/]| # leading punctuation, or
0
+ [^=!:'"/]| # leading punctuation, or
0
(?:https?://)| # protocol spec, or
0
[-\w]+ # subdomain or domain
0
(?:\.[-\w]+)* # remaining subdomains or domain
0
text.gsub(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
0
if body.match(/<a\b[^>]*>(.*)(#{Regexp.escape(text)})(.*)<\/a>/)
Comments
No one has commented yet.