Commit fc586dbf6ed64a9db0be9c0fec0f41fb30232c48
1 parent
3c633e98
Exists in
master
changing from hpricot to nokogiri
Showing
3 changed files
with
45 additions
and
8 deletions
Show diff stats
controllers/email_article_plugin_myprofile_controller.rb
1 | +require 'nokogiri' | |
2 | +require 'open-uri' | |
3 | + | |
1 | 4 | class EmailArticlePluginMyprofileController < MyProfileController |
2 | 5 | |
3 | 6 | needs_profile |
7 | + | |
8 | + class Webpage < Nokogiri::HTML::Document | |
9 | + attr_accessor :url | |
10 | + | |
11 | + class << self | |
12 | + | |
13 | + def new(url) | |
14 | + html = open(url) | |
15 | + self.parse(html).tap do |d| | |
16 | + d.url = url | |
17 | + end | |
18 | + end | |
19 | + end | |
20 | + end | |
4 | 21 | |
5 | 22 | def send_email |
6 | 23 | if user.is_admin?(profile) |
7 | - article = Article.find(params[:id]) | |
24 | + article = profile.articles.find(params[:id]) | |
25 | +# article = Article.find(params[:id]) | |
8 | 26 | EmailArticlePluginMyprofileController::Sender.content(article).deliver |
9 | 27 | render :text => "Email sent to queue" |
10 | 28 | else |
... | ... | @@ -14,6 +32,27 @@ class EmailArticlePluginMyprofileController < MyProfileController |
14 | 32 | |
15 | 33 | class Sender < ActionMailer::Base |
16 | 34 | def content(article) |
35 | + doc = Nokogiri::HTML(article.body) | |
36 | + doc.css("a").each do |link| | |
37 | + unless link.attribute("href").nil? | |
38 | + relative_path = link.attribute("href").value | |
39 | + link.attribute("href").value = "http://#{article.environment.default_hostname}/#{relative_path}" | |
40 | + end | |
41 | + end | |
42 | + | |
43 | + doc.css("img").each do |link| | |
44 | + unless link.attribute("src").nil? | |
45 | + relative_path = link.attribute("src").value | |
46 | + link.attribute("src").value = "http://#{article.environment.default_hostname}/#{relative_path}" | |
47 | + end | |
48 | + end | |
49 | + | |
50 | + | |
51 | +# link.attributes["href"].value = "http://myproxy.com/?url=#{CGI.escape link.attributes["href"].value}" | |
52 | +# http://#{environment.default_hostname}/#{relative_path[1]} | |
53 | + | |
54 | + | |
55 | + body = doc.to_s | |
17 | 56 | sender = Person.find(article.author_id) |
18 | 57 | members = article.profile.members |
19 | 58 | emails = [] |
... | ... | @@ -26,7 +65,8 @@ class EmailArticlePluginMyprofileController < MyProfileController |
26 | 65 | from: "#{article.author_name} <#{sender.contact_email}>", |
27 | 66 | reply_to: article.author.user.email, |
28 | 67 | subject: "[Artigo] " + article.title, |
29 | - body: set_absolute_path(article.body, article.environment) | |
68 | + body: body | |
69 | +# body: set_absolute_path(article.body, article.environment) | |
30 | 70 | ) |
31 | 71 | end |
32 | 72 | ... | ... |
lib/email_article_plugin.rb
... | ... | @@ -8,16 +8,12 @@ class EmailArticlePlugin < Noosfero::Plugin |
8 | 8 | _("A plugin that emails an article to the members of the community") |
9 | 9 | end |
10 | 10 | |
11 | - def page | |
12 | - @page | |
13 | - end | |
14 | - | |
15 | 11 | def article_toolbar_extra_buttons |
16 | 12 | label = _("Send article to members") |
17 | 13 | htmlclass = _("button with-text icon-menu-mail") |
18 | 14 | title = _("Email article to all community members") |
19 | 15 | proc do |
20 | - if !profile.blank? and !user.blank? and user.is_admin?(profile) and page.kind_of?(TextArticle) | |
16 | + if !profile.blank? and !user.blank? and user.is_admin?(profile) and @page.kind_of?(TextArticle) | |
21 | 17 | link_to_remote( |
22 | 18 | label, |
23 | 19 | { | ... | ... |
test/functional/email_article_plugin_myprofile_controller_test.rb
... | ... | @@ -46,9 +46,10 @@ class EmailArticlePluginMyprofileControllerTest < ActionController::TestCase |
46 | 46 | send_mail_button = @plugin.article_toolbar_extra_buttons |
47 | 47 | self.stubs(:profile).returns(@profile) |
48 | 48 | self.stubs(:user).returns(@user) |
49 | - self.stubs(:page).returns(@article) | |
49 | +# self.stubs(:page).returns(@article) | |
50 | 50 | @user.stubs(:is_admin?).returns(true) |
51 | 51 | self.stubs(:link_to_remote).returns("send mail button") |
52 | +# @plugin.class_eval{@page=2} | |
52 | 53 | html = self.instance_eval(&send_mail_button) |
53 | 54 | assert_equal html, "send mail button" |
54 | 55 | end | ... | ... |