premailer-rails3 1.3.1 → 1.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +3 -1
- data/Gemfile +4 -0
- data/README.md +13 -13
- data/VERSION +1 -0
- data/lib/premailer/rails.rb +23 -0
- data/lib/premailer/rails/css_helper.rb +58 -0
- data/lib/premailer/rails/css_loaders.rb +66 -0
- data/lib/premailer/rails/customized_premailer.rb +20 -0
- data/lib/premailer/rails/hook.rb +45 -0
- data/lib/premailer/rails/nokogiri_fix.rb +13 -0
- data/lib/premailer/rails/premailer.rb +20 -0
- data/lib/premailer/rails/version.rb +7 -0
- data/lib/premailer/rails3.rb +23 -0
- data/premailer-rails3.gemspec +15 -12
- data/spec/{premailer-rails3 → lib}/css_helper_spec.rb +10 -75
- data/spec/{premailer-rails3/premailer_spec.rb → lib/customized_premailer_spec.rb} +16 -12
- data/spec/{premailer-rails3 → lib}/hook_registration_spec.rb +3 -3
- data/spec/{premailer-rails3 → lib}/hook_spec.rb +22 -14
- data/spec/{premailer-rails3 → lib}/premailer_rails_3_spec.rb +3 -3
- data/spec/spec_helper.rb +2 -3
- data/spec/stubs/action_mailer.rb +1 -1
- data/spec/stubs/rails.rb +2 -4
- metadata +52 -66
- data/lib/premailer-rails3.rb +0 -18
- data/lib/premailer-rails3/css_helper.rb +0 -61
- data/lib/premailer-rails3/css_loaders.rb +0 -91
- data/lib/premailer-rails3/hook.rb +0 -43
- data/lib/premailer-rails3/premailer.rb +0 -18
- data/lib/premailer-rails3/version.rb +0 -3
- data/spec/stubs/hassle.rb +0 -1
data/lib/premailer-rails3.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'premailer'
|
2
|
-
require 'premailer-rails3/css_loaders'
|
3
|
-
require 'premailer-rails3/css_helper'
|
4
|
-
require 'premailer-rails3/premailer'
|
5
|
-
require 'premailer-rails3/hook'
|
6
|
-
|
7
|
-
module PremailerRails
|
8
|
-
@config = {
|
9
|
-
:input_encoding => 'UTF-8',
|
10
|
-
:inputencoding => 'UTF-8',
|
11
|
-
:generate_text_part => true
|
12
|
-
}
|
13
|
-
class << self
|
14
|
-
attr_accessor :config
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
ActionMailer::Base.register_interceptor(PremailerRails::Hook)
|
@@ -1,61 +0,0 @@
|
|
1
|
-
require 'open-uri'
|
2
|
-
require 'zlib'
|
3
|
-
|
4
|
-
module PremailerRails
|
5
|
-
module CSSHelper
|
6
|
-
extend self
|
7
|
-
|
8
|
-
@cache = {}
|
9
|
-
attr :cache
|
10
|
-
|
11
|
-
STRATEGIES = [
|
12
|
-
CSSLoaders::CacheLoader,
|
13
|
-
CSSLoaders::HassleLoader,
|
14
|
-
CSSLoaders::AssetPipelineLoader,
|
15
|
-
CSSLoaders::FileSystemLoader
|
16
|
-
]
|
17
|
-
|
18
|
-
# Returns all linked CSS files concatenated as string.
|
19
|
-
def css_for_doc(doc)
|
20
|
-
urls = css_urls_in_doc(doc)
|
21
|
-
if urls.empty?
|
22
|
-
load_css(:default) unless has_inline_css? doc
|
23
|
-
else
|
24
|
-
urls.map { |url| load_css(url) }.join("\n")
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
private
|
29
|
-
|
30
|
-
def css_urls_in_doc(doc)
|
31
|
-
doc.search('link[@type="text/css"]').map do |link|
|
32
|
-
link.attributes['href'].to_s
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def has_inline_css?(doc)
|
37
|
-
not doc.search('style[@type="text/css"]').empty?
|
38
|
-
end
|
39
|
-
|
40
|
-
def load_css(url)
|
41
|
-
path = extract_path(url)
|
42
|
-
|
43
|
-
@cache[path] = STRATEGIES.each do |strategy|
|
44
|
-
css = strategy.load(path)
|
45
|
-
break css if css
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
# Extracts the path of a url.
|
50
|
-
def extract_path(url)
|
51
|
-
if url.is_a? String
|
52
|
-
# Remove everything after ? including ?
|
53
|
-
url = url[0..(url.index('?') - 1)] if url.include? '?'
|
54
|
-
# Remove the host
|
55
|
-
url = url.sub(/^https?\:\/\/[^\/]*/, '') if url.index('http') == 0
|
56
|
-
end
|
57
|
-
|
58
|
-
url
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
@@ -1,91 +0,0 @@
|
|
1
|
-
module PremailerRails
|
2
|
-
module CSSLoaders
|
3
|
-
# Loads the CSS from cache when not in development env.
|
4
|
-
module CacheLoader
|
5
|
-
extend self
|
6
|
-
|
7
|
-
def load(path)
|
8
|
-
unless Rails.env.development?
|
9
|
-
CSSHelper.cache[path]
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
# Loads the CSS from Hassle middleware if present.
|
15
|
-
module HassleLoader
|
16
|
-
extend self
|
17
|
-
|
18
|
-
def load(path)
|
19
|
-
if hassle_enabled?
|
20
|
-
File.read("#{Rails.root}/tmp/hassle#{normalized_path(path)}")
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def hassle_enabled?
|
25
|
-
Rails.configuration.middleware.include? Hassle rescue false
|
26
|
-
end
|
27
|
-
|
28
|
-
def normalized_path(path)
|
29
|
-
path == :default ? '/stylesheets/email.css' : path
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
# Loads the CSS from the asset pipeline.
|
34
|
-
module AssetPipelineLoader
|
35
|
-
extend self
|
36
|
-
|
37
|
-
def load(path)
|
38
|
-
if assets_enabled?
|
39
|
-
file = file_name(path)
|
40
|
-
if asset = Rails.application.assets.find_asset(file)
|
41
|
-
asset.to_s
|
42
|
-
else
|
43
|
-
request_and_unzip(file)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def assets_enabled?
|
49
|
-
Rails.configuration.assets.enabled rescue false
|
50
|
-
end
|
51
|
-
|
52
|
-
def file_name(path)
|
53
|
-
if path == :default
|
54
|
-
'email.css'
|
55
|
-
else
|
56
|
-
path.sub("#{Rails.configuration.assets.prefix}/", '') \
|
57
|
-
.sub(/-.*\.css$/, '.css')
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def request_and_unzip(file)
|
62
|
-
url = [
|
63
|
-
Rails.configuration.action_controller.asset_host,
|
64
|
-
Rails.configuration.assets.prefix.sub(/^\//, ''),
|
65
|
-
Rails.configuration.assets.digests[file]
|
66
|
-
].join('/')
|
67
|
-
response = Kernel.open(url)
|
68
|
-
|
69
|
-
begin
|
70
|
-
Zlib::GzipReader.new(response).read
|
71
|
-
rescue Zlib::GzipFile::Error, Zlib::Error
|
72
|
-
response.rewind
|
73
|
-
response.read
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
# Loads the CSS from the file system.
|
79
|
-
module FileSystemLoader
|
80
|
-
extend self
|
81
|
-
|
82
|
-
def load(path)
|
83
|
-
File.read("#{Rails.root}/public#{normalized_path(path)}")
|
84
|
-
end
|
85
|
-
|
86
|
-
def normalized_path(path)
|
87
|
-
path == :default ? '/stylesheets/email.css' : path
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
module PremailerRails
|
2
|
-
class Hook
|
3
|
-
def self.delivering_email(message)
|
4
|
-
# If the mail only has one part, it may be stored in message.body. In that
|
5
|
-
# case, if the mail content type is text/html, the body part will be the
|
6
|
-
# html body.
|
7
|
-
if message.html_part
|
8
|
-
html_body = message.html_part.body.to_s
|
9
|
-
needs_multipart = true
|
10
|
-
message.parts.delete(message.html_part)
|
11
|
-
elsif message.content_type =~ /text\/html/
|
12
|
-
html_body = message.body.to_s
|
13
|
-
message.body = nil
|
14
|
-
needs_multipart = PremailerRails.config[:generate_text_part]
|
15
|
-
end
|
16
|
-
|
17
|
-
if html_body
|
18
|
-
premailer = Premailer.new(html_body)
|
19
|
-
charset = message.charset
|
20
|
-
|
21
|
-
if needs_multipart
|
22
|
-
# IMPORTANT: Plain text part must be generated before CSS is inlined.
|
23
|
-
# Not doing so results in CSS declarations visible in the plain text
|
24
|
-
# part.
|
25
|
-
if PremailerRails.config[:generate_text_part] \
|
26
|
-
and not message.text_part
|
27
|
-
message.text_part do
|
28
|
-
content_type "text/plain; charset=#{charset}"
|
29
|
-
body premailer.to_plain_text
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
message.html_part do
|
34
|
-
content_type "text/html; charset=#{charset}"
|
35
|
-
body premailer.to_inline_css
|
36
|
-
end
|
37
|
-
else
|
38
|
-
message.body = premailer.to_inline_css
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module PremailerRails
|
2
|
-
class Premailer < ::Premailer
|
3
|
-
def initialize(html)
|
4
|
-
# In order to pass the CSS as string to super it is necessary to access
|
5
|
-
# the parsed HTML beforehand. To do so, the adapter needs to be
|
6
|
-
# initialized. The ::Premailer::Adaptor handles the discovery of a
|
7
|
-
# suitable adaptor (Nokogiri or Hpricot). To make load_html work, an
|
8
|
-
# adaptor needs to be included and @options[:with_html_string] needs to be
|
9
|
-
# set. For further information, refer to ::Premailer#initialize.
|
10
|
-
@options = PremailerRails.config.merge(:with_html_string => true)
|
11
|
-
::Premailer.send(:include, Adapter.find(Adapter.use))
|
12
|
-
doc = load_html(html)
|
13
|
-
|
14
|
-
options = @options.merge(:css_string => CSSHelper.css_for_doc(doc))
|
15
|
-
super(html, options)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
data/spec/stubs/hassle.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
module Hassle; end
|