aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view
diff options
context:
space:
mode:
authorAvi Tzurel <avi@kensodev.com>2012-05-11 21:46:39 +0300
committerAvi Tzurel <avi@kensodev.com>2012-05-14 16:55:41 +0300
commit09fcac95d7d263b054fa0c9a168da2789ce8ae06 (patch)
treea72014908eb35b866ec27a2d1cb52b7dac2cb212 /actionpack/lib/action_view
parentc13a3f0fc18eec15441e6d88073b92850e93d59a (diff)
downloadrails-09fcac95d7d263b054fa0c9a168da2789ce8ae06.tar.gz
rails-09fcac95d7d263b054fa0c9a168da2789ce8ae06.tar.bz2
rails-09fcac95d7d263b054fa0c9a168da2789ce8ae06.zip
simple_format refactoring
using split_paragraphs method and content_tag instead of string concatination
Diffstat (limited to 'actionpack/lib/action_view')
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb26
1 files changed, 17 insertions, 9 deletions
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index 698f4434ba..19ecb617a8 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -259,16 +259,16 @@ module ActionView
# simple_format("<span>I'm allowed!</span> It's true.", {}, :sanitize => false)
# # => "<p><span>I'm allowed!</span> It's true.</p>"
def simple_format(text, html_options={}, options={})
- text = '' if text.nil?
- text = text.dup
- start_tag = tag('p', html_options, true)
text = sanitize(text) unless options[:sanitize] == false
- text = text.to_str
- text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n
- text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline -> paragraph
- text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
- text.insert 0, start_tag
- text.html_safe.safe_concat("</p>")
+ paragraphs = split_paragraphs(text)
+
+ if paragraphs.empty?
+ content_tag('p', nil, html_options)
+ else
+ paragraphs.map { |paragraph|
+ content_tag('p', paragraph, html_options, options[:sanitize])
+ }.join("\n\n").html_safe
+ end
end
# Creates a Cycle object whose _to_s_ method cycles through elements of an
@@ -413,6 +413,14 @@ module ActionView
@_cycles = Hash.new unless defined?(@_cycles)
@_cycles[name] = cycle_object
end
+
+ def split_paragraphs(text)
+ return [] if text.blank?
+
+ text.to_str.gsub(/\r\n?/, "\n").split(/\n\n+/).map! do |t|
+ t.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') || t
+ end
+ end
end
end
end