diff options
Diffstat (limited to 'actionpack/lib/action_view/helpers/text_helper.rb')
-rw-r--r-- | actionpack/lib/action_view/helpers/text_helper.rb | 220 |
1 files changed, 98 insertions, 122 deletions
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index d80e7c6e57..1d9e4fe9b8 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -1,15 +1,5 @@ require 'action_view/helpers/tag_helper' -begin - require 'html/document' -rescue LoadError - html_scanner_path = "#{File.dirname(__FILE__)}/../../action_controller/vendor/html-scanner" - if File.directory?(html_scanner_path) - $:.unshift html_scanner_path - require 'html/document' - end -end - module ActionView module Helpers #:nodoc: # The TextHelper module provides a set of methods for filtering, formatting @@ -226,91 +216,79 @@ module ActionView end * "\n" end - begin - require_library_or_gem "redcloth" unless Object.const_defined?(:RedCloth) - - # Returns the text with all the Textile[http://www.textism.com/tools/textile] codes turned into HTML tags. - # - # You can learn more about Textile's syntax at its website[http://www.textism.com/tools/textile]. - # <i>This method is only available if RedCloth[http://whytheluckystiff.net/ruby/redcloth/] - # is available</i>. - # - # ==== Examples - # textilize("*This is Textile!* Rejoice!") - # # => "<p><strong>This is Textile!</strong> Rejoice!</p>" - # - # textilize("I _love_ ROR(Ruby on Rails)!") - # # => "<p>I <em>love</em> <acronym title="Ruby on Rails">ROR</acronym>!</p>" - # - # textilize("h2. Textile makes markup -easy- simple!") - # # => "<h2>Textile makes markup <del>easy</del> simple!</h2>" - # - # textilize("Visit the Rails website "here":http://www.rubyonrails.org/.) - # # => "<p>Visit the Rails website <a href="http://www.rubyonrails.org/">here</a>.</p>" - def textilize(text) - if text.blank? - "" - else - textilized = RedCloth.new(text, [ :hard_breaks ]) - textilized.hard_breaks = true if textilized.respond_to?(:hard_breaks=) - textilized.to_html - end + # Returns the text with all the Textile[http://www.textism.com/tools/textile] codes turned into HTML tags. + # + # You can learn more about Textile's syntax at its website[http://www.textism.com/tools/textile]. + # <i>This method is only available if RedCloth[http://whytheluckystiff.net/ruby/redcloth/] + # is available</i>. + # + # ==== Examples + # textilize("*This is Textile!* Rejoice!") + # # => "<p><strong>This is Textile!</strong> Rejoice!</p>" + # + # textilize("I _love_ ROR(Ruby on Rails)!") + # # => "<p>I <em>love</em> <acronym title="Ruby on Rails">ROR</acronym>!</p>" + # + # textilize("h2. Textile makes markup -easy- simple!") + # # => "<h2>Textile makes markup <del>easy</del> simple!</h2>" + # + # textilize("Visit the Rails website "here":http://www.rubyonrails.org/.) + # # => "<p>Visit the Rails website <a href="http://www.rubyonrails.org/">here</a>.</p>" + def textilize(text) + if text.blank? + "" + else + textilized = RedCloth.new(text, [ :hard_breaks ]) + textilized.hard_breaks = true if textilized.respond_to?(:hard_breaks=) + textilized.to_html end + end - # Returns the text with all the Textile codes turned into HTML tags, - # but without the bounding <p> tag that RedCloth adds. - # - # You can learn more about Textile's syntax at its website[http://www.textism.com/tools/textile]. - # <i>This method is only available if RedCloth[http://whytheluckystiff.net/ruby/redcloth/] - # is available</i>. - # - # ==== Examples - # textilize_without_paragraph("*This is Textile!* Rejoice!") - # # => "<strong>This is Textile!</strong> Rejoice!" - # - # textilize_without_paragraph("I _love_ ROR(Ruby on Rails)!") - # # => "I <em>love</em> <acronym title="Ruby on Rails">ROR</acronym>!" - # - # textilize_without_paragraph("h2. Textile makes markup -easy- simple!") - # # => "<h2>Textile makes markup <del>easy</del> simple!</h2>" - # - # textilize_without_paragraph("Visit the Rails website "here":http://www.rubyonrails.org/.) - # # => "Visit the Rails website <a href="http://www.rubyonrails.org/">here</a>." - def textilize_without_paragraph(text) - textiled = textilize(text) - if textiled[0..2] == "<p>" then textiled = textiled[3..-1] end - if textiled[-4..-1] == "</p>" then textiled = textiled[0..-5] end - return textiled - end - rescue LoadError - # We can't really help what's not there + # Returns the text with all the Textile codes turned into HTML tags, + # but without the bounding <p> tag that RedCloth adds. + # + # You can learn more about Textile's syntax at its website[http://www.textism.com/tools/textile]. + # <i>This method is requires RedCloth[http://whytheluckystiff.net/ruby/redcloth/] + # to be available</i>. + # + # ==== Examples + # textilize_without_paragraph("*This is Textile!* Rejoice!") + # # => "<strong>This is Textile!</strong> Rejoice!" + # + # textilize_without_paragraph("I _love_ ROR(Ruby on Rails)!") + # # => "I <em>love</em> <acronym title="Ruby on Rails">ROR</acronym>!" + # + # textilize_without_paragraph("h2. Textile makes markup -easy- simple!") + # # => "<h2>Textile makes markup <del>easy</del> simple!</h2>" + # + # textilize_without_paragraph("Visit the Rails website "here":http://www.rubyonrails.org/.) + # # => "Visit the Rails website <a href="http://www.rubyonrails.org/">here</a>." + def textilize_without_paragraph(text) + textiled = textilize(text) + if textiled[0..2] == "<p>" then textiled = textiled[3..-1] end + if textiled[-4..-1] == "</p>" then textiled = textiled[0..-5] end + return textiled end - begin - require_library_or_gem "bluecloth" unless Object.const_defined?(:BlueCloth) - - # Returns the text with all the Markdown codes turned into HTML tags. - # <i>This method is only available if BlueCloth[http://www.deveiate.org/projects/BlueCloth] - # is available</i>. - # - # ==== Examples - # markdown("We are using __Markdown__ now!") - # # => "<p>We are using <strong>Markdown</strong> now!</p>" - # - # markdown("We like to _write_ `code`, not just _read_ it!") - # # => "<p>We like to <em>write</em> <code>code</code>, not just <em>read</em> it!</p>" - # - # markdown("The [Markdown website](http://daringfireball.net/projects/markdown/) has more information.") - # # => "<p>The <a href="http://daringfireball.net/projects/markdown/">Markdown website</a> - # # has more information.</p>" - # - # markdown('![The ROR logo](http://rubyonrails.com/images/rails.png "Ruby on Rails")') - # # => '<p><img src="http://rubyonrails.com/images/rails.png" alt="The ROR logo" title="Ruby on Rails" /></p>' - def markdown(text) - text.blank? ? "" : BlueCloth.new(text).to_html - end - rescue LoadError - # We can't really help what's not there + # Returns the text with all the Markdown codes turned into HTML tags. + # <i>This method requires BlueCloth[http://www.deveiate.org/projects/BlueCloth] + # to be available</i>. + # + # ==== Examples + # markdown("We are using __Markdown__ now!") + # # => "<p>We are using <strong>Markdown</strong> now!</p>" + # + # markdown("We like to _write_ `code`, not just _read_ it!") + # # => "<p>We like to <em>write</em> <code>code</code>, not just <em>read</em> it!</p>" + # + # markdown("The [Markdown website](http://daringfireball.net/projects/markdown/) has more information.") + # # => "<p>The <a href="http://daringfireball.net/projects/markdown/">Markdown website</a> + # # has more information.</p>" + # + # markdown('![The ROR logo](http://rubyonrails.com/images/rails.png "Ruby on Rails")') + # # => '<p><img src="http://rubyonrails.com/images/rails.png" alt="The ROR logo" title="Ruby on Rails" /></p>' + def markdown(text) + text.blank? ? "" : BlueCloth.new(text).to_html end # Returns +text+ transformed into HTML using simple formatting rules. @@ -392,8 +370,8 @@ module ActionView options.reverse_merge!(:link => :all, :html => {}) case options[:link].to_sym - when :all then auto_link_email_addresses(auto_link_urls(text, options[:html], &block), &block) - when :email_addresses then auto_link_email_addresses(text, &block) + when :all then auto_link_email_addresses(auto_link_urls(text, options[:html], &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], &block) end end @@ -545,45 +523,43 @@ module ActionView end AUTO_LINK_RE = %r{ - ( # leading text - <\w+.*?>| # leading HTML tag, or - [^=!:'"/]| # leading punctuation, or - ^ # beginning of line - ) - ( - (?:https?://)| # protocol spec, or - (?:www\.) # www.* - ) - ( - [-\w]+ # subdomain or domain - (?:\.[-\w]+)* # remaining subdomains or domain - (?::\d+)? # port - (?:/(?:[~\w\+@%=\(\)-]|(?:[,.;:'][^\s$]))*)* # path - (?:\?[\w\+@%&=.;-]+)? # query string - (?:\#[\w\-]*)? # trailing anchor - ) - ([[:punct:]]|<|$|) # trailing text - }x unless const_defined?(:AUTO_LINK_RE) + ( https?:// | www\. ) + [^\s<]+ + }x unless const_defined?(:AUTO_LINK_RE) + + BRACKETS = { ']' => '[', ')' => '(', '}' => '{' } # Turns all urls into clickable links. If a block is given, each url # is yielded and the result is used as the link text. def auto_link_urls(text, html_options = {}) - extra_options = tag_options(html_options.stringify_keys) || "" + link_attributes = html_options.stringify_keys text.gsub(AUTO_LINK_RE) do - all, a, b, c, d = $&, $1, $2, $3, $4 - if a =~ /<a\s/i # don't replace URL's that are already linked - all + href = $& + punctuation = '' + # detect already linked URLs + if $` =~ /<a\s[^>]*href="$/ + # do not change string; URL is alreay linked + href else - text = b + c - text = yield(text) if block_given? - %(#{a}<a href="#{b=="www."?"http://www.":b}#{c}"#{extra_options}>#{text}</a>#{d}) + # don't include trailing punctuation character as part of the URL + if href.sub!(/[^\w\/-]$/, '') and punctuation = $& and opening = BRACKETS[punctuation] + if href.scan(opening).size > href.scan(punctuation).size + href << punctuation + punctuation = '' + end + end + + link_text = block_given?? yield(href) : href + href = 'http://' + href unless href.index('http') == 0 + + content_tag(:a, h(link_text), link_attributes.merge('href' => href)) + punctuation end end end # Turns all email addresses into clickable links. If a block is given, # each email is yielded and the result is used as the link text. - def auto_link_email_addresses(text) + def auto_link_email_addresses(text, html_options = {}) body = text.dup text.gsub(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do text = $1 @@ -592,10 +568,10 @@ module ActionView text else display_text = (block_given?) ? yield(text) : text - %{<a href="mailto:#{text}">#{display_text}</a>} + mail_to text, display_text, html_options end end end end end -end
\ No newline at end of file +end |