From 09fcac95d7d263b054fa0c9a168da2789ce8ae06 Mon Sep 17 00:00:00 2001 From: Avi Tzurel Date: Fri, 11 May 2012 21:46:39 +0300 Subject: simple_format refactoring using split_paragraphs method and content_tag instead of string concatination --- actionpack/lib/action_view/helpers/text_helper.rb | 26 +++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'actionpack/lib/action_view') 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("I'm allowed! It's true.", {}, :sanitize => false) # # => "

I'm allowed! It's true.

" 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+/, "

\n\n#{start_tag}") # 2+ newline -> paragraph - text.gsub!(/([^\n]\n)(?=[^\n])/, '\1
') # 1 newline -> br - text.insert 0, start_tag - text.html_safe.safe_concat("

") + 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
') || t + end + end end end end -- cgit v1.2.3 From 4bb14b0daafb9b1c3d0d5c041703c9242b27adc6 Mon Sep 17 00:00:00 2001 From: Avi Tzurel Date: Mon, 14 May 2012 01:15:46 +0300 Subject: Added the wrapper tag option to simple_format --- actionpack/lib/action_view/helpers/text_helper.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 19ecb617a8..a62864bf81 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -241,6 +241,7 @@ module ActionView # # ==== Options # * :sanitize - If +false+, does not sanitize +text+. + # * :wrapper_tag - String representing the tag wrapper, defaults to "p" # # ==== Examples # my_text = "Here is some basic text...\n...with a line break." @@ -260,13 +261,14 @@ module ActionView # # => "

I'm allowed! It's true.

" def simple_format(text, html_options={}, options={}) text = sanitize(text) unless options[:sanitize] == false + wrapper_tag = options.fetch(:wrapper_tag, :p) paragraphs = split_paragraphs(text) if paragraphs.empty? - content_tag('p', nil, html_options) + content_tag(wrapper_tag, nil, html_options) else paragraphs.map { |paragraph| - content_tag('p', paragraph, html_options, options[:sanitize]) + content_tag(wrapper_tag, paragraph, html_options, options[:sanitize]) }.join("\n\n").html_safe end end -- cgit v1.2.3