FIX: Code and Emoticon formatting in HTML emails.

This commit is contained in:
Robin Ward 2013-11-28 17:20:56 -05:00
parent 4ec0543362
commit 8c8645f158
11 changed files with 100 additions and 43 deletions

View file

@ -59,6 +59,8 @@ gem 'redis', :require => ["redis", "redis/connection/hiredis"]
gem 'active_model_serializers'
gem 'truncate_html'
# we had issues with latest, stick to the rev till we figure this out
# PR that makes it all hang together welcome
gem 'ember-rails'

View file

@ -442,6 +442,7 @@ GEM
polyglot
polyglot (>= 0.3.1)
trollop (2.0)
truncate_html (0.9.2)
tzinfo (0.3.38)
uglifier (2.3.1)
execjs (>= 0.3.0)
@ -543,6 +544,7 @@ DEPENDENCIES
therubyracer
thin
timecop
truncate_html
uglifier
unf
unicorn

View file

@ -22,7 +22,7 @@ class Admin::EmailController < Admin::AdminController
def preview_digest
params.require(:last_seen_at)
renderer = Email::Renderer.new(UserNotifications.digest(current_user, since: params[:last_seen_at]), html_template: true)
renderer = Email::Renderer.new(UserNotifications.digest(current_user, since: params[:last_seen_at]))
render json: MultiJson.dump(html_content: renderer.html, text_content: renderer.text)
end

View file

@ -17,4 +17,20 @@ module UserNotificationsHelper
fragment.to_html.html_safe
end
def logo_url
logo_url = SiteSetting.logo_url
if logo_url !~ /http(s)?\:\/\//
logo_url = "#{Discourse.base_url}#{logo_url}"
end
logo_url
end
def html_site_link
"<a href='#{Discourse.base_url}'>#{@site_name}</a>"
end
def email_excerpt(html)
html_string = TruncateHtml::HtmlString.new(html)
raw Sanitize.clean(TruncateHtml::HtmlTruncator.new(html_string, length: 1000).truncate, Sanitize::Config::RELAXED)
end
end

View file

@ -0,0 +1,47 @@
<table style="border: 1px solid #ddd;" cellspacing="0" cellpadding="0">
<tr>
<td style="padding: 10px 10px; background-color: #eee; border: 1px solid #ddd;">
<a href="<%= Discourse.base_url %>">
<img src="<%= logo_url %>" style="max-height: 35px; min-height: 35px; height: 35px;" class='site-logo'></a>
</td>
</tr>
<tr>
<td style="background-color: #fff; padding: 10px 10px; font-family: Arial, Helvetica, sans-serif; font-size: 14px;">
<%= raw(t 'user_notifications.digest.why', site_link: html_site_link, last_seen_at: @last_seen_at) %>
<%- if @featured_topics.present? %>
<h3><%=t 'user_notifications.digest.top_topics' %></h3>
<%- @featured_topics.each_with_index do |t, i| %>
<%= link_to t.title, t.relative_url %>
<%- if t.best_post.present? %>
<div class='digest-post'>
<%= email_excerpt(t.best_post.cooked) %>
</div>
<%- end %>
<%- if i < @featured_topics.size - 1 %><hr><% end %>
<%- end %>
<%- end %>
<%- if @new_topics.present? %>
<h3><%=t 'user_notifications.digest.other_new_topics' %></h3>
<%- @new_topics.each do |t| %>
<ul>
<li><%= link_to t.title, t.relative_url %> <%- if t.category %>[<%= t.category.name %>]<%- end %></li>
</ul>
<%- end -%>
<%- end -%>
<span class='footer-notice'>
<%=raw(t :'user_notifications.digest.unsubscribe',
site_link: html_site_link,
unsubscribe_link: link_to(t('user_notifications.digest.click_here'), email_unsubscribe_path(key: @user.temporary_key))) %>
</span>
</td>
</tr>
</table>

View file

@ -10,11 +10,10 @@
<%= raw(@markdown_linker.create(t.title, t.relative_url)) %>
<%- if t.best_post.present? %>
<div class='digest-post'><%= raw(t.best_post.excerpt(1000,
<%= raw(t.best_post.excerpt(1000,
strip_links: true,
text_entities: true,
markdown_images: true)) %></div>
markdown_images: true)) %>
--------------------------------------------------------------------------------
<%- end %>
@ -35,9 +34,9 @@
<%= raw(@markdown_linker.references) %>
<span class='footer-notice'><%=raw(t :'user_notifications.digest.unsubscribe',
<%=raw(t :'user_notifications.digest.unsubscribe',
site_link: site_link,
unsubscribe_link: raw(@markdown_linker.create(t('user_notifications.digest.click_here'), email_unsubscribe_path(key: @user.temporary_key)))) %></span>
unsubscribe_link: raw(@markdown_linker.create(t('user_notifications.digest.click_here'), email_unsubscribe_path(key: @user.temporary_key)))) %>
<%= raw(@markdown_linker.references) %>

View file

@ -9,32 +9,22 @@ module Email
end
def text
@text ||= @message.body.to_s.force_encoding('UTF-8')
end
def logo_url
logo_url = SiteSetting.logo_url
if logo_url !~ /http(s)?\:\/\//
logo_url = "#{Discourse.base_url}#{logo_url}"
end
logo_url
return @text if @text
@text = (@message.text_part ? @message.text_part : @message).body.to_s.force_encoding('UTF-8')
end
def html
style = Email::Styles.new(PrettyText.cook(text))
style.format_basic
if @opts[:html_template]
if @message.html_part
style = Email::Styles.new(@message.html_part.body.to_s)
style.format_basic
style.format_html
ActionView::Base.new(Rails.configuration.paths["app/views"]).render(
template: 'email/template',
format: :html,
locals: { html_body: style.to_html, logo_url: logo_url }
)
else
style.to_html
style = Email::Styles.new(PrettyText.cook(text))
style.format_basic
end
style.to_html
end
end

View file

@ -21,15 +21,17 @@ module Email
def send
return if @message.blank?
return if @message.to.blank?
return if @message.body.blank?
if @message.text_part
return if @message.text_part.body.to_s.blank?
else
return if @message.body.to_s.blank?
end
@message.charset = 'UTF-8'
opts = {}
# Only use the html template on digest emails
opts[:html_template] = true if (@email_type == 'digest')
renderer = Email::Renderer.new(@message, opts)
unless @message.html_part

View file

@ -13,7 +13,9 @@ module Email
def format_basic
@fragment.css('img').each do |img|
if img['src'] =~ /\/assets\/emoji\//
next if img['class'] == 'site-logo'
if img['class'] == "emoji" || img['src'] =~ /plugins\/emoji/
img['width'] = 20
img['height'] = 20
else
@ -57,11 +59,6 @@ module Email
style('div.digest-post', 'margin-left: 15px; margin-top: 20px; max-width: 694px;')
style('div.digest-post h1', 'font-size: 20px;')
style('span.footer-notice', 'color:#666; font-size:80%')
@fragment.css('pre').each do |pre|
pre.replace(pre.text)
end
end
def to_html

View file

@ -30,7 +30,7 @@ describe Email::Styles do
end
it "adds a width and height to images with an emoji path" do
frag = basic_fragment("<img src='/assets/emoji/fish.png'>")
frag = basic_fragment("<img src='/plugins/emoji/fish.png' class='emoji'>")
expect(frag.at("img")["width"]).to eq("20")
expect(frag.at("img")["height"]).to eq("20")
end
@ -85,12 +85,6 @@ describe Email::Styles do
expect(frag.at('li')['style']).to be_present
end
it "removes pre tags but keeps their contents" do
style = Email::Styles.new("<pre>hello</pre>")
style.format_basic
style.format_html
expect(style.to_html).to eq("hello")
end
end

View file

@ -37,7 +37,15 @@ describe UserNotifications do
its(:to) { should == [user.email] }
its(:subject) { should be_present }
its(:from) { should == [SiteSetting.notification_email] }
its(:body) { should be_present }
it 'should have a html body' do
subject.html_part.body.to_s.should be_present
end
it 'should have a text body' do
subject.html_part.body.to_s.should be_present
end
end
end