module ActionView::Helpers::TextHelper
def auto_link(text, *args, &block)#link = :all, html = {}, &block)
Please e-mail me at me@email.com."
# => "Welcome to my new blog at http://www.myblog.com.
auto_link(post_body, :all, :target => "_blank") # => Once upon\na time
Please e-mail me at me@email.com."
# => "Welcome to my new blog at http://www.myblog.com.
auto_link(post_body, :urls) # => Once upon\na time
post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
as its optional third parameter:
+link+ as its optional second parameter and the +html_options+ hash
You can still use auto_link with the old API that accepts the
Please e-mail me at me@email.com."
# => "Welcome to my new blog at http://www.m....
end
truncate(text, 15)
auto_link(post_body, :html => { :target => '_blank' }) do |text|
post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
# => "Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com"
auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :link => :email_addresses)
# or e-mail david@loudthinking.com"
# => "Visit http://www.loudthinking.com/
auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :link => :urls)
# say hello to david@loudthinking.com"
# => "Go to http://www.rubyonrails.org and
auto_link("Go to http://www.rubyonrails.org and say hello to david@loudthinking.com")
==== Examples
e-mail address is yielded and the result is used as the link text.
:email_addresses, and :urls. If a block is given, each URL and
:html. Possible values for :link are :all (default),
will limit what should be linked. You can add HTML attributes to the links using
Turns all URLs and e-mail addresses into clickable links. The :link option
def auto_link(text, *args, &block)#link = :all, html = {}, &block) return '' if text.blank? options = args.size == 2 ? {} : args.extract_options! # this is necessary because the old auto_link API has a Hash as its last parameter unless args.empty? options[:link] = args[0] || :all options[:html] = args[1] || {} end options.reverse_merge!(:link => :all, :html => {}) case options[:link].to_sym when :all then auto_link_email_addresses(auto_link_urls(text, options[:html], options, &block), options[:html], &block) when :email_addresses then auto_link_email_addresses(text, options[:html], &block) when :urls then auto_link_urls(text, options[:html], options, &block) end end
def auto_link_email_addresses(text, html_options = {}, options = {})
Turns all email addresses into clickable links. If a block is given,
def auto_link_email_addresses(text, html_options = {}, options = {}) text.to_str.gsub(AUTO_EMAIL_RE) do text = $& if auto_linked?($`, $') text.html_safe else display_text = block_given? ? yield(text) : text display_text = sanitize(display_text) unless options[:sanitize] == false mail_to text, display_text, html_options end end end
def auto_link_urls(text, html_options = {}, options = {})
Turns all urls into clickable links. If a block is given, each url
def auto_link_urls(text, html_options = {}, options = {}) link_attributes = html_options.stringify_keys text.to_str.gsub(AUTO_LINK_RE) do scheme, href = $1, $& punctuation = [] if auto_linked?($`, $') # do not change string; URL is already linked href else # don't include trailing punctuation character as part of the URL while href.sub!(/[^\w\/-]$/, '') punctuation.push $& if opening = BRACKETS[punctuation.last] and href.scan(opening).size > href.scan(punctuation.last).size href << punctuation.pop break end end link_text = block_given? ? yield(href) : href href = 'http://' + href unless scheme sanitize = options[:sanitize] != false content_tag(:a, link_text, link_attributes.merge('href' => href), sanitize) + punctuation.reverse.join('') end end end
def auto_linked?(left, right)
def auto_linked?(left, right) (left =~ AUTO_LINK_CRE[0] and right =~ AUTO_LINK_CRE[1]) or (left.rindex(AUTO_LINK_CRE[2]) and $' !~ AUTO_LINK_CRE[3]) end
def concat(string)
# will either display "Logged in!" or a login link
end
concat link_to('login', :action => login)
else
concat "Logged in!"
if logged_in
# is the equivalent of <%= "hello" %>
concat "hello"
<%
==== Examples
output text within a non-output code block (i.e., <% %>), you can use the concat method.
do not operate as expected in an eRuby code block. If you absolutely must
<%= "text" %> eRuby syntax. The regular _puts_ and _print_ methods
The preferred method of outputting text in your views is to use the
def concat(string) output_buffer << string end
def current_cycle(name = "default")
<%= item %>
">
<% @items.each do |item| %>
@items = [1,2,3,4]
# Alternate background colors
==== Example
the current cycle string in more than one place.
for complex table highlighting or any other design need which requires
Returns the current cycle string after a cycle has been started. Useful
<% @items.each do |item| %>
@items = [1,2,3,4]
# Alternate background colors
==== Example
the current cycle string in more than one place.
for complex table highlighting or any other design need which requires
Returns the current cycle string after a cycle has been started. Useful
def current_cycle(name = "default") cycle = get_cycle(name) cycle.current_value unless cycle.nil? end
def cycle(first_value, *values)
<% reset_cycle("colors") %>
<% end %>
<%= value %>
"colors") -%>">
<%# Create a named cycle "colors" %>
<% item.values.each do |value| %>
<% @items.each do |item| %>
{:first => 'June', :middle => 'Dae', :last => 'Jones'}]
{:first => 'Emily', :middle => 'Shannon', :maiden => 'Pike', :last => 'Hicks'},
@items = x = [{:first => 'Robert', :middle => 'Daniel', :last => 'James'},
# Cycle CSS classes for rows, and text colors for values within each row
<% end %>
<% @items.each do |item| %>
<% end %>
<% reset_cycle("colors") %>
<% end %>
<%= value %>
"colors") -%>">
<% item.each do |value| %>
<% @items.each do |item| %>