Commit 063a2c54eefac8fd0f08c0eb5424af1d3215f47a
Committed by
Daniela Feitosa
1 parent
b605f7cd
Exists in
master
and in
29 other branches
Now wall messages are respecting line-brakes
I also refactored the needed txt2html helper and created some tests for this. (ActionItem1956)
Showing
3 changed files
with
60 additions
and
9 deletions
Show diff stats
app/helpers/display_helper.rb
... | ... | @@ -27,14 +27,18 @@ module DisplayHelper |
27 | 27 | end |
28 | 28 | |
29 | 29 | def txt2html(txt) |
30 | - txt. | |
31 | - gsub( /\n\s*\n/, ' <p/> ' ). | |
32 | - gsub( /\n/, ' <br/> ' ). | |
33 | - gsub( /(^|\s)(www\.[^\s])/, '\1http://\2' ). | |
34 | - gsub( /(https?:\/\/([^\s]+))/ ) do | |
35 | - href, content = $1, $2.scan(/.{4}/).join('​') | |
36 | - content_tag(:a, content, :href => href, :target => '_blank', :rel => 'nofolow', | |
37 | - :onclick => "return confirm('%s')" % escape_javascript(_('Are you sure you want to visit this web site?'))) | |
30 | + txt.strip. | |
31 | + gsub( /\s*\n\s*\n\s*/, "\r<p/>\r" ). | |
32 | + gsub( /\s*\n\s*/, "\n<br/>\n" ). | |
33 | + gsub( /\r/, "\n" ). | |
34 | + gsub( /(^|\s)(www\.[^\s]+|https?:\/\/[^\s]+)/ ) do | |
35 | + pre_char, href = $1, $2 | |
36 | + href = 'http://'+href if ! href.match /^https?:/ | |
37 | + content = href.gsub(/^https?:\/\//, '').scan(/.{1,4}/).join('​') | |
38 | + pre_char + | |
39 | + content_tag(:a, content, :href => href, :target => '_blank', | |
40 | + :rel => 'nofolow', :onclick => "return confirm('%s')" % | |
41 | + _('Are you sure you want to visit this web site?')) | |
38 | 42 | end |
39 | 43 | end |
40 | 44 | end | ... | ... |
app/views/profile/_profile_scrap.rhtml
... | ... | @@ -8,7 +8,7 @@ |
8 | 8 | <% comment_balloon :class => 'profile-wall-description' do %> |
9 | 9 | <p class='profile-wall-sender'><%= link_to scrap.sender.name, scrap.sender.url %></p> |
10 | 10 | <p class='profile-wall-time'><%= time_ago_as_sentence(scrap.created_at) + ' ' + _('ago') %></p> |
11 | - <p class='profile-wall-text'><%= auto_link_urls scrap.content %></p> | |
11 | + <p class='profile-wall-text'><%= txt2html scrap.content %></p> | |
12 | 12 | <%= 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) %> |
13 | 13 | <% if logged_in? && current_person.follows?(scrap.sender) && scrap.root.nil? %> |
14 | 14 | <p class='profile-wall-send-reply'><%= 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" %></p> | ... | ... |
... | ... | @@ -0,0 +1,47 @@ |
1 | +require File.dirname(__FILE__) + '/../test_helper' | |
2 | + | |
3 | +class DisplayHelperTest < Test::Unit::TestCase | |
4 | + | |
5 | + include DisplayHelper | |
6 | + include ActionView::Helpers::TagHelper | |
7 | + | |
8 | + #### product_path related tests #### | |
9 | + | |
10 | + # TODO: product_path has no tests! | |
11 | + | |
12 | + #### txt2html related tests #### | |
13 | + | |
14 | + should 'show txt line-breaks' do | |
15 | + html = txt2html "Bli\n123" | |
16 | + assert_equal "Bli\n<br/>\n123", html | |
17 | + html = txt2html "Bli\n123 456\nabc" | |
18 | + assert_equal "Bli\n<br/>\n123 456\n<br/>\nabc", html | |
19 | + end | |
20 | + | |
21 | + should 'replace empty lines as paragraphs separators' do | |
22 | + html = txt2html "Bli\n\n123" | |
23 | + assert_equal "Bli\n<p/>\n123", html | |
24 | + html = txt2html "Bli \n \n 123" | |
25 | + assert_equal "Bli\n<p/>\n123", html | |
26 | + end | |
27 | + | |
28 | + should 'trim txt before convert to html' do | |
29 | + html = txt2html "\nBli\n123\n" | |
30 | + assert_equal "Bli\n<br/>\n123", html | |
31 | + html = txt2html " Bli\n123 " | |
32 | + assert_equal "Bli\n<br/>\n123", html | |
33 | + html = txt2html " \nBli\n123\n " | |
34 | + assert_equal "Bli\n<br/>\n123", html | |
35 | + end | |
36 | + | |
37 | + should 'linkify "http://" prepended words on txt2html' do | |
38 | + html = txt2html "go to http://noosfero.org" | |
39 | + assert_equal 'go to <a href="http://noosfero.org" onclick="return confirm(\'Are you sure you want to visit this web site?\')" rel="nofolow" target="_blank">noos​fero​.org</a>', html | |
40 | + end | |
41 | + | |
42 | + should 'linkify "www." prepended words on txt2html' do | |
43 | + html = txt2html "go to www.noosfero.org yeah!" | |
44 | + assert_equal 'go to <a href="http://www.noosfero.org" onclick="return confirm(\'Are you sure you want to visit this web site?\')" rel="nofolow" target="_blank">www.​noos​fero​.org</a> yeah!', html | |
45 | + end | |
46 | + | |
47 | +end | ... | ... |