From fc586dbf6ed64a9db0be9c0fec0f41fb30232c48 Mon Sep 17 00:00:00 2001 From: Evandro Junior Date: Thu, 26 Feb 2015 18:44:57 -0300 Subject: [PATCH] changing from hpricot to nokogiri --- controllers/email_article_plugin_myprofile_controller.rb | 44 ++++++++++++++++++++++++++++++++++++++++++-- lib/email_article_plugin.rb | 6 +----- test/functional/email_article_plugin_myprofile_controller_test.rb | 3 ++- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/controllers/email_article_plugin_myprofile_controller.rb b/controllers/email_article_plugin_myprofile_controller.rb index b5bed2d..8b90b76 100644 --- a/controllers/email_article_plugin_myprofile_controller.rb +++ b/controllers/email_article_plugin_myprofile_controller.rb @@ -1,10 +1,28 @@ +require 'nokogiri' +require 'open-uri' + class EmailArticlePluginMyprofileController < MyProfileController needs_profile + + class Webpage < Nokogiri::HTML::Document + attr_accessor :url + + class << self + + def new(url) + html = open(url) + self.parse(html).tap do |d| + d.url = url + end + end + end + end def send_email if user.is_admin?(profile) - article = Article.find(params[:id]) + article = profile.articles.find(params[:id]) +# article = Article.find(params[:id]) EmailArticlePluginMyprofileController::Sender.content(article).deliver render :text => "Email sent to queue" else @@ -14,6 +32,27 @@ class EmailArticlePluginMyprofileController < MyProfileController class Sender < ActionMailer::Base def content(article) + doc = Nokogiri::HTML(article.body) + doc.css("a").each do |link| + unless link.attribute("href").nil? + relative_path = link.attribute("href").value + link.attribute("href").value = "http://#{article.environment.default_hostname}/#{relative_path}" + end + end + + doc.css("img").each do |link| + unless link.attribute("src").nil? + relative_path = link.attribute("src").value + link.attribute("src").value = "http://#{article.environment.default_hostname}/#{relative_path}" + end + end + + +# link.attributes["href"].value = "http://myproxy.com/?url=#{CGI.escape link.attributes["href"].value}" +# http://#{environment.default_hostname}/#{relative_path[1]} + + + body = doc.to_s sender = Person.find(article.author_id) members = article.profile.members emails = [] @@ -26,7 +65,8 @@ class EmailArticlePluginMyprofileController < MyProfileController from: "#{article.author_name} <#{sender.contact_email}>", reply_to: article.author.user.email, subject: "[Artigo] " + article.title, - body: set_absolute_path(article.body, article.environment) + body: body +# body: set_absolute_path(article.body, article.environment) ) end diff --git a/lib/email_article_plugin.rb b/lib/email_article_plugin.rb index ae614fd..f49fb66 100644 --- a/lib/email_article_plugin.rb +++ b/lib/email_article_plugin.rb @@ -8,16 +8,12 @@ class EmailArticlePlugin < Noosfero::Plugin _("A plugin that emails an article to the members of the community") end - def page - @page - end - def article_toolbar_extra_buttons label = _("Send article to members") htmlclass = _("button with-text icon-menu-mail") title = _("Email article to all community members") proc do - if !profile.blank? and !user.blank? and user.is_admin?(profile) and page.kind_of?(TextArticle) + if !profile.blank? and !user.blank? and user.is_admin?(profile) and @page.kind_of?(TextArticle) link_to_remote( label, { diff --git a/test/functional/email_article_plugin_myprofile_controller_test.rb b/test/functional/email_article_plugin_myprofile_controller_test.rb index da6f178..6ebb4d7 100644 --- a/test/functional/email_article_plugin_myprofile_controller_test.rb +++ b/test/functional/email_article_plugin_myprofile_controller_test.rb @@ -46,9 +46,10 @@ class EmailArticlePluginMyprofileControllerTest < ActionController::TestCase send_mail_button = @plugin.article_toolbar_extra_buttons self.stubs(:profile).returns(@profile) self.stubs(:user).returns(@user) - self.stubs(:page).returns(@article) +# self.stubs(:page).returns(@article) @user.stubs(:is_admin?).returns(true) self.stubs(:link_to_remote).returns("send mail button") +# @plugin.class_eval{@page=2} html = self.instance_eval(&send_mail_button) assert_equal html, "send mail button" end -- libgit2 0.21.2