diff --git a/app/helpers/display_helper.rb b/app/helpers/display_helper.rb index 5443678..13b3742 100644 --- a/app/helpers/display_helper.rb +++ b/app/helpers/display_helper.rb @@ -27,14 +27,18 @@ module DisplayHelper end def txt2html(txt) - txt. - gsub( /\n\s*\n/, '

' ). - gsub( /\n/, '
' ). - gsub( /(^|\s)(www\.[^\s])/, '\1http://\2' ). - gsub( /(https?:\/\/([^\s]+))/ ) do - href, content = $1, $2.scan(/.{4}/).join('​') - content_tag(:a, content, :href => href, :target => '_blank', :rel => 'nofolow', - :onclick => "return confirm('%s')" % escape_javascript(_('Are you sure you want to visit this web site?'))) + txt.strip. + gsub( /\s*\n\s*\n\s*/, "\r

\r" ). + gsub( /\s*\n\s*/, "\n
\n" ). + gsub( /\r/, "\n" ). + gsub( /(^|\s)(www\.[^\s]+|https?:\/\/[^\s]+)/ ) do + pre_char, href = $1, $2 + href = 'http://'+href if ! href.match /^https?:/ + content = href.gsub(/^https?:\/\//, '').scan(/.{1,4}/).join('​') + pre_char + + content_tag(:a, content, :href => href, :target => '_blank', + :rel => 'nofolow', :onclick => "return confirm('%s')" % + _('Are you sure you want to visit this web site?')) end end end diff --git a/app/views/profile/_profile_scrap.rhtml b/app/views/profile/_profile_scrap.rhtml index 7e0afa6..390d3d5 100644 --- a/app/views/profile/_profile_scrap.rhtml +++ b/app/views/profile/_profile_scrap.rhtml @@ -8,7 +8,7 @@ <% comment_balloon :class => 'profile-wall-description' do %>

<%= link_to scrap.sender.name, scrap.sender.url %>

<%= time_ago_as_sentence(scrap.created_at) + ' ' + _('ago') %>

-

<%= auto_link_urls scrap.content %>

+

<%= txt2html scrap.content %>

<%= button_to_remote(:delete, content_tag(:span, _('Remove')), :url =>{:action => 'remove_scrap', :scrap_id => scrap.id}, :update => "profile-wall-item-#{scrap.id}") if logged_in? && user.can_control_scrap?(scrap) %> <% if logged_in? && current_person.follows?(scrap.sender) && scrap.root.nil? %>

<%= link_to_function _('Reply'), "hide_and_show(['#profile-wall-reply-response-#{scrap.id}'],['#profile-wall-reply-#{scrap.id}', '#profile-wall-reply-form-#{scrap.id}']);$('reply_content_#{scrap.id}').value='';$('scrap_id_#{scrap.id}').value='#{scrap.id}';return false", :class => "profile-send-reply icon-reply" %>

diff --git a/test/unit/display_helper_test.rb b/test/unit/display_helper_test.rb new file mode 100644 index 0000000..ae25d09 --- /dev/null +++ b/test/unit/display_helper_test.rb @@ -0,0 +1,47 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class DisplayHelperTest < Test::Unit::TestCase + + include DisplayHelper + include ActionView::Helpers::TagHelper + + #### product_path related tests #### + + # TODO: product_path has no tests! + + #### txt2html related tests #### + + should 'show txt line-breaks' do + html = txt2html "Bli\n123" + assert_equal "Bli\n
\n123", html + html = txt2html "Bli\n123 456\nabc" + assert_equal "Bli\n
\n123 456\n
\nabc", html + end + + should 'replace empty lines as paragraphs separators' do + html = txt2html "Bli\n\n123" + assert_equal "Bli\n

\n123", html + html = txt2html "Bli \n \n 123" + assert_equal "Bli\n

\n123", html + end + + should 'trim txt before convert to html' do + html = txt2html "\nBli\n123\n" + assert_equal "Bli\n
\n123", html + html = txt2html " Bli\n123 " + assert_equal "Bli\n
\n123", html + html = txt2html " \nBli\n123\n " + assert_equal "Bli\n
\n123", html + end + + should 'linkify "http://" prepended words on txt2html' do + html = txt2html "go to http://noosfero.org" + assert_equal 'go to noos​fero​.org', html + end + + should 'linkify "www." prepended words on txt2html' do + html = txt2html "go to www.noosfero.org yeah!" + assert_equal 'go to www.​noos​fero​.org yeah!', html + end + +end -- libgit2 0.21.2