diff --git a/app/helpers/content_viewer_helper.rb b/app/helpers/content_viewer_helper.rb index 4077617..3b31b3f 100644 --- a/app/helpers/content_viewer_helper.rb +++ b/app/helpers/content_viewer_helper.rb @@ -46,4 +46,13 @@ module ContentViewerHelper end end + def addthis_facebook_url(article) + "http://www.facebook.com/sharer.php?s=100&p[title]=%{title}&p[summary]=%{summary}&p[url]=%{url}&p[images][0]=%{image}" % { + :title => CGI.escape(article.title), + :url => CGI.escape(url_for(article.url)), + :summary => CGI.escape(truncate(strip_tags(article.body.to_s), 300)), + :image => CGI.escape(article.body_images_paths.first.to_s) + } + end + end diff --git a/app/models/article.rb b/app/models/article.rb index 5e3e324..56e39cc 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -509,6 +509,13 @@ class Article < ActiveRecord::Base self.parent && self.parent.accept_uploads? end + def body_images_paths + require 'uri' + Hpricot(self.body.to_s).search('img[@src]').collect do |i| + (self.profile && self.profile.environment) ? URI.join(self.profile.environment.top_url, i.attributes['src']).to_s : i.attributes['src'] + end + end + private def sanitize_tag_list diff --git a/app/views/content_viewer/view_page.rhtml b/app/views/content_viewer/view_page.rhtml index 64bd4b1..7fb307b 100644 --- a/app/views/content_viewer/view_page.rhtml +++ b/app/views/content_viewer/view_page.rhtml @@ -38,6 +38,13 @@ diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 50fab33..bb31deb 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -1526,4 +1526,33 @@ class ArticleTest < Test::Unit::TestCase assert_includes Article.find_by_contents('thing'), art2 end + should 'get images paths in article body' do + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') + a = TinyMceArticle.new :profile => @profile + a.body = 'Noosfero test ' + assert_includes a.body_images_paths, 'http://noosfero.com/test.png' + assert_includes a.body_images_paths, 'http://test.com/noosfero.png' + end + + should 'get absolute images paths in article body' do + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') + a = TinyMceArticle.new :profile => @profile + a.body = 'Noosfero Absolute test ' + assert_includes a.body_images_paths, 'http://noosfero.org/test.png' + assert_includes a.body_images_paths, 'http://noosfero.org/relative/path.png' + end + + should 'return empty if there are no images in article body' do + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') + a = Event.new :profile => @profile + a.body = 'Noosfero test' + assert_equal [], a.body_images_paths + end + + should 'return empty if body is nil' do + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') + a = Article.new :profile => @profile + assert_equal [], a.body_images_paths + end + end diff --git a/test/unit/content_viewer_helper_test.rb b/test/unit/content_viewer_helper_test.rb index e3e36ed..f4e89f9 100644 --- a/test/unit/content_viewer_helper_test.rb +++ b/test/unit/content_viewer_helper_test.rb @@ -83,6 +83,42 @@ class ContentViewerHelperTest < Test::Unit::TestCase assert_no_match /feed/, result end + should 'generate facebook addthis url for article' do + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') + [TextileArticle, Blog, Folder, Gallery, UploadedFile, Forum, Event, TextArticle, TinyMceArticle].each do |model| + a = model.new(:name => 'Some title', :body => 'Some text here.', :profile => profile) + assert_equal "http://www.facebook.com/sharer.php?s=100&p[title]=Some+title&p[summary]=Some+text+here.&p[url]=http%3A%2F%2Fnoosfero.org%2Fblog_helper_test%2Fsome-title&p[images][0]=", addthis_facebook_url(a) + end + end + + should 'generate facebook addthis url without body' do + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') + a = TinyMceArticle.new(:name => 'Test', :body => nil, :profile => profile) + assert_equal "http://www.facebook.com/sharer.php?s=100&p[title]=Test&p[summary]=&p[url]=http%3A%2F%2Fnoosfero.org%2Fblog_helper_test%2Ftest&p[images][0]=", addthis_facebook_url(a) + end + + should 'generate facebook addthis url without tags in body' do + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') + a = TinyMceArticle.new(:name => 'Some title', :body => '

This is a test

', :profile => profile) + assert_equal "http://www.facebook.com/sharer.php?s=100&p[title]=Some+title&p[summary]=This+is+a+test&p[url]=http%3A%2F%2Fnoosfero.org%2Fblog_helper_test%2Fsome-title&p[images][0]=", addthis_facebook_url(a) + end + + should 'generate facebook addthis url with truncated body' do + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') + a = TinyMceArticle.new(:name => 'Some title', :body => 'test' * 76, :profile => profile) + assert_equal "http://www.facebook.com/sharer.php?s=100&p[title]=Some+title&p[summary]=#{'test' * 74}t...&p[url]=http%3A%2F%2Fnoosfero.org%2Fblog_helper_test%2Fsome-title&p[images][0]=", addthis_facebook_url(a) + end + + should 'generate facebook addthis url for tinymce article with images' do + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') + a = TinyMceArticle.new(:name => 'Some title', :body => '

This is a test

', :profile => profile) + assert_equal "http://www.facebook.com/sharer.php?s=100&p[title]=Some+title&p[summary]=This+is+a+test&p[url]=http%3A%2F%2Fnoosfero.org%2Fblog_helper_test%2Fsome-title&p[images][0]=http%3A%2F%2Fnoosfero.org%2Fimages%2Fx.png", addthis_facebook_url(a) + end + + protected + + include ActionView::Helpers::TextHelper + end def show_date(date) @@ -96,3 +132,9 @@ def _(text) end def will_paginate(arg1, arg2) end +def strip_tags(html) + html.gsub(/<[^>]+>/, '') +end +def url_for(args = {}) + ['http:/', args[:host], args[:profile], args[:page]].join('/') +end -- libgit2 0.21.2