pretty_mailer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +53 -0
- data/Rakefile +1 -0
- data/lib/pretty_mailer.rb +39 -0
- data/lib/pretty_mailer/asset_parser.rb +17 -0
- data/lib/pretty_mailer/html.rb +61 -0
- data/lib/pretty_mailer/version.rb +3 -0
- data/pretty_mailer.gemspec +26 -0
- metadata +123 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Adam Ross Cohen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# PrettyMailer
|
2
|
+
|
3
|
+
Apply stylesheets to your rails mailers. PrettyMailer uses sprockets to find and parse your stylesheets, which it then applies inline to the emails HTML part. It takes css selector specificity into account, so your styles are applied as they would be with a modern web browser, taking the proper order of precedence.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'pretty_mailer'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install pretty_mailer
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class ExampleMailer < ActionMailer::Base
|
23
|
+
include PrettyMailer # include the module in your mailer
|
24
|
+
|
25
|
+
# you can specify a default style for the whole mailer
|
26
|
+
default css: 'email.css'
|
27
|
+
# or, you can optionally pass a whole array of stylesheets
|
28
|
+
default css: ['email.css', 'other_styles.css']
|
29
|
+
|
30
|
+
def foomail
|
31
|
+
# you can also specify stylesheets per email
|
32
|
+
mail to: 'some@one.com' from: 'another@guy.com', subject: 'Pretty mailer is awesome', css: 'awesome.css' # array syntax works here too
|
33
|
+
end
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create new Pull Request
|
44
|
+
|
45
|
+
## License
|
46
|
+
|
47
|
+
Copyright (C) 2013 Adam Ross Cohen
|
48
|
+
|
49
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
50
|
+
|
51
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
52
|
+
|
53
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "pretty_mailer/version"
|
2
|
+
require "pretty_mailer/html"
|
3
|
+
require "pretty_mailer/asset_parser"
|
4
|
+
|
5
|
+
module PrettyMailer
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def included base
|
10
|
+
base.alias_method_chain :collect_responses_and_parts_order, :inline_styles
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
def collect_responses_and_parts_order_with_inline_styles headers
|
16
|
+
responses, parts_order = collect_responses_and_parts_order_without_inline_styles headers
|
17
|
+
unless headers[:css].blank?
|
18
|
+
responses.map! do |response|
|
19
|
+
if response[:content_type] == 'text/html'
|
20
|
+
apply_inline_styles response, headers[:css]
|
21
|
+
else
|
22
|
+
response
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
[responses, parts_order]
|
27
|
+
end
|
28
|
+
|
29
|
+
def apply_inline_styles response, stylesheets
|
30
|
+
body = HTML.new response[:body]
|
31
|
+
AssetParser.new(stylesheets: [*stylesheets]).each_selector do |selector, declarations, specificity|
|
32
|
+
body.add_rules selector, declarations, specificity
|
33
|
+
end
|
34
|
+
body.reduce_specificities!
|
35
|
+
response[:body] = body.to_s
|
36
|
+
response
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'css_parser'
|
2
|
+
|
3
|
+
module PrettyMailer
|
4
|
+
|
5
|
+
class AssetParser < CssParser::Parser
|
6
|
+
|
7
|
+
def initialize options
|
8
|
+
super options
|
9
|
+
options[:stylesheets].each do |stylesheet|
|
10
|
+
load_string! Rails.application.assets.find_asset(stylesheet).to_s
|
11
|
+
end
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
|
3
|
+
module PrettyMailer
|
4
|
+
|
5
|
+
class HTML
|
6
|
+
|
7
|
+
def initialize body
|
8
|
+
@doc = Nokogiri::HTML body
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_rules selector, declarations, specificity
|
12
|
+
if selector =~ /^[\.\#a-z]/i
|
13
|
+
begin
|
14
|
+
@doc.css(selector).each do |tag|
|
15
|
+
tag['specificities'] = "#{tag['specificities']}#{specificity};"
|
16
|
+
tag[specificity.to_s] = declarations.gsub /\s+/, ''
|
17
|
+
end
|
18
|
+
rescue Exception => Nokogiri::CSS::SyntaxError
|
19
|
+
Rails.logger.debug "Ignoring invalid selector: #{selector}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def reduce_specificities!
|
25
|
+
tags_with_specificities.each do |tag|
|
26
|
+
specificities_on_tag(tag).each do |specificity|
|
27
|
+
if tag['style'].blank?
|
28
|
+
tag['style'] = "#{ tag[specificity] }"
|
29
|
+
else
|
30
|
+
merge_in_styles tag, specificity
|
31
|
+
end
|
32
|
+
tag.delete specificity
|
33
|
+
end
|
34
|
+
tag.delete 'specificities'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_s
|
39
|
+
@doc.to_s
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def tags_with_specificities
|
45
|
+
@doc.xpath '//*[@specificities]'
|
46
|
+
end
|
47
|
+
|
48
|
+
def specificities_on_tag tag
|
49
|
+
tag['specificities'].split(';').sort
|
50
|
+
end
|
51
|
+
|
52
|
+
def merge_in_styles tag, specificity
|
53
|
+
tag[specificity].split(';').each do |rule|
|
54
|
+
tag['style'] = tag['style'].sub /#{rule.split(':').first.strip}:\s*[a-z0-9]+\s*;/i, "#{rule};"
|
55
|
+
tag['style'] += "#{rule};" unless $~
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'pretty_mailer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "pretty_mailer"
|
8
|
+
spec.version = PrettyMailer::VERSION
|
9
|
+
spec.authors = ["Adam Ross Cohen"]
|
10
|
+
spec.email = ["a.ross.cohen@gmail.com"]
|
11
|
+
spec.description = %q{PrettyMailer lets you use stylesheets to apply inline styles to emails.}
|
12
|
+
spec.summary = %q{Apply stylesheets to your rails mailers. PrettyMailer uses sprockets to find and parse your stylesheets, which it then applies inline to the emails HTML part. It takes css selector specificity into account, so your styles are applied as they would be with a modern web browser, taking the proper order of precedence.}
|
13
|
+
spec.homepage = "https://github.com/a-ross-cohen/pretty_mailer"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
|
24
|
+
spec.add_dependency "css_parser"
|
25
|
+
spec.add_dependency "nokogiri"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pretty_mailer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Adam Ross Cohen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: css_parser
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: nokogiri
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: PrettyMailer lets you use stylesheets to apply inline styles to emails.
|
79
|
+
email:
|
80
|
+
- a.ross.cohen@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- lib/pretty_mailer.rb
|
91
|
+
- lib/pretty_mailer/asset_parser.rb
|
92
|
+
- lib/pretty_mailer/html.rb
|
93
|
+
- lib/pretty_mailer/version.rb
|
94
|
+
- pretty_mailer.gemspec
|
95
|
+
homepage: https://github.com/a-ross-cohen/pretty_mailer
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.8.25
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: Apply stylesheets to your rails mailers. PrettyMailer uses sprockets to find
|
120
|
+
and parse your stylesheets, which it then applies inline to the emails HTML part.
|
121
|
+
It takes css selector specificity into account, so your styles are applied as they
|
122
|
+
would be with a modern web browser, taking the proper order of precedence.
|
123
|
+
test_files: []
|