middleman-inline-style 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +55 -0
- data/README.rdoc +127 -0
- data/Rakefile +9 -0
- data/example.rb +5 -0
- data/lib/middleman-inline-style.rb +105 -0
- data/lib/middleman-inline-style/css_parser_wrapper.rb +15 -0
- data/lib/middleman-inline-style/csspool_wrapper.rb +12 -0
- data/lib/middleman-inline-style/mail-interceptor.rb +37 -0
- data/lib/middleman-inline-style/middleman-extension.rb +11 -0
- data/lib/middleman-inline-style/rack-middleware.rb +36 -0
- data/lib/middleman-inline-style/rule.rb +18 -0
- data/lib/middleman-inline-style/version.rb +3 -0
- data/middleman-inline-style.gemspec +29 -0
- data/spec/css_inlining_spec.rb +90 -0
- data/spec/css_parsing_spec.rb +146 -0
- data/spec/fixtures/all.css +3 -0
- data/spec/fixtures/boletin.html +261 -0
- data/spec/fixtures/box-model.html +37 -0
- data/spec/fixtures/inline.html +107 -0
- data/spec/fixtures/none.css +3 -0
- data/spec/fixtures/print.css +3 -0
- data/spec/fixtures/selectors.html +27 -0
- data/spec/fixtures/style.css +3 -0
- data/spec/interceptor_spec.rb +64 -0
- data/spec/rack_middleware_spec.rb +60 -0
- data/spec/spec_helper.rb +62 -0
- metadata +186 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ee1ba4737bd9ab7d57803703ca5b57e646938874
|
4
|
+
data.tar.gz: 8e771299193d7dae034d80fc79597f61f317f0a9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ead817715c64b55db01dc560fbb6b80a757865bbe84be8ecb6d8cf01b0d833c14ef3ad34aabe9e226cf773f8a691b63212892d7ef9c37000aa57cce79216f5f6
|
7
|
+
data.tar.gz: 77dc5338f4cc4dcf50b48e33c797c2f8827e78f066e036ab845dc5e798179340a885737c5678389a23290475fafe3f05e90cf966f925c1c34021ada7bc4a7803
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
inline-style (0.4.10)
|
5
|
+
css_parser
|
6
|
+
facets
|
7
|
+
maca-fork-csspool
|
8
|
+
nokogiri
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: http://rubygems.org/
|
12
|
+
specs:
|
13
|
+
activesupport (3.0.4)
|
14
|
+
css_parser (1.1.5)
|
15
|
+
diff-lcs (1.1.2)
|
16
|
+
facets (2.9.1)
|
17
|
+
ffi (1.0.7)
|
18
|
+
rake (>= 0.8.7)
|
19
|
+
i18n (0.5.0)
|
20
|
+
maca-fork-csspool (2.0.2)
|
21
|
+
ffi
|
22
|
+
mail (2.2.15)
|
23
|
+
activesupport (>= 2.3.6)
|
24
|
+
i18n (>= 0.4.0)
|
25
|
+
mime-types (~> 1.16)
|
26
|
+
treetop (~> 1.4.8)
|
27
|
+
mime-types (1.16)
|
28
|
+
nokogiri (1.4.4)
|
29
|
+
polyglot (0.3.1)
|
30
|
+
rack (1.2.1)
|
31
|
+
rake (0.8.7)
|
32
|
+
rspec (2.4.0)
|
33
|
+
rspec-core (~> 2.4.0)
|
34
|
+
rspec-expectations (~> 2.4.0)
|
35
|
+
rspec-mocks (~> 2.4.0)
|
36
|
+
rspec-core (2.4.0)
|
37
|
+
rspec-expectations (2.4.0)
|
38
|
+
diff-lcs (~> 1.1.2)
|
39
|
+
rspec-mocks (2.4.0)
|
40
|
+
treetop (1.4.9)
|
41
|
+
polyglot (>= 0.3.1)
|
42
|
+
|
43
|
+
PLATFORMS
|
44
|
+
ruby
|
45
|
+
|
46
|
+
DEPENDENCIES
|
47
|
+
css_parser
|
48
|
+
facets
|
49
|
+
inline-style!
|
50
|
+
maca-fork-csspool
|
51
|
+
mail
|
52
|
+
nokogiri
|
53
|
+
rack
|
54
|
+
rspec
|
55
|
+
rspec-core
|
data/README.rdoc
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
= middleman-inline-style
|
2
|
+
|
3
|
+
https://github.com/trenton42/middleman-inline-style
|
4
|
+
|
5
|
+
Slightly modified version of http://github.com/maca/inline-style to work directly with middleman
|
6
|
+
|
7
|
+
|
8
|
+
== Description
|
9
|
+
|
10
|
+
Will take all css in a page (either from linked stylesheet or from style tag) and will embed it in the style attribute for
|
11
|
+
each refered element taking selector specificity and declarator order.
|
12
|
+
|
13
|
+
With this you can generate your html email templates with middleman without needing to inline all of the styles yourself
|
14
|
+
|
15
|
+
*Note*
|
16
|
+
|
17
|
+
Currently only works with styles specified in <style></style> tags.
|
18
|
+
|
19
|
+
|
20
|
+
== Usage (in your config.rb)
|
21
|
+
|
22
|
+
require 'middleman-inline-style'
|
23
|
+
|
24
|
+
activate :inline_style
|
25
|
+
|
26
|
+
index.html contains:
|
27
|
+
|
28
|
+
<style type="text/css" media="screen">
|
29
|
+
* {
|
30
|
+
font-family: "Lucida Grande", Lucida, Verdana, sans-serif;
|
31
|
+
margin: 4px 3px 2px 1px;
|
32
|
+
padding: 0;
|
33
|
+
}
|
34
|
+
|
35
|
+
#list {
|
36
|
+
margin: 10;
|
37
|
+
}
|
38
|
+
|
39
|
+
#list li {
|
40
|
+
font-family: Arial;
|
41
|
+
}
|
42
|
+
|
43
|
+
.element {
|
44
|
+
padding: 10;
|
45
|
+
}
|
46
|
+
|
47
|
+
.odd {
|
48
|
+
background-color: black;
|
49
|
+
}
|
50
|
+
|
51
|
+
.pair {
|
52
|
+
background-color: red;
|
53
|
+
}
|
54
|
+
|
55
|
+
</style>
|
56
|
+
|
57
|
+
|
58
|
+
<ul id='number' class='listing inlined' style='background-color: yellow'>
|
59
|
+
<li class='list-element odd'>
|
60
|
+
<span>1</span>
|
61
|
+
</li>
|
62
|
+
<li class='list-element pair'>
|
63
|
+
<span>2</span>
|
64
|
+
</li>
|
65
|
+
<li class='list-element odd'>
|
66
|
+
<span>3</span>
|
67
|
+
</li>
|
68
|
+
<li class='list-element pair'>
|
69
|
+
<span>4</span>
|
70
|
+
</li>
|
71
|
+
</ul>
|
72
|
+
|
73
|
+
Will become:
|
74
|
+
|
75
|
+
<ul id="number" class="listing inlined" style='font-family: "Lucida Grande", Lucida, Verdana, sans-serif;margin: 4.0px 3.0px 2.0px 1.0px;padding: 0.0;background-color: yellow;'>
|
76
|
+
<li class="list-element odd" style='font-family: "Lucida Grande", Lucida, Verdana, sans-serif;margin: 4.0px 3.0px 2.0px 1.0px;padding: 0.0;background-color: black;'>
|
77
|
+
<span style='font-family: "Lucida Grande", Lucida, Verdana, sans-serif;margin: 4.0px 3.0px 2.0px 1.0px;padding: 0.0;'>1</span>
|
78
|
+
</li>
|
79
|
+
<li class="list-element pair" style='font-family: "Lucida Grande", Lucida, Verdana, sans-serif;margin: 4.0px 3.0px 2.0px 1.0px;padding: 0.0;background-color: red;'>
|
80
|
+
<span style='font-family: "Lucida Grande", Lucida, Verdana, sans-serif;margin: 4.0px 3.0px 2.0px 1.0px;padding: 0.0;'>2</span>
|
81
|
+
</li>
|
82
|
+
<li class="list-element odd" style='font-family: "Lucida Grande", Lucida, Verdana, sans-serif;margin: 4.0px 3.0px 2.0px 1.0px;padding: 0.0;background-color: black;'>
|
83
|
+
<span style='font-family: "Lucida Grande", Lucida, Verdana, sans-serif;margin: 4.0px 3.0px 2.0px 1.0px;padding: 0.0;'>3</span>
|
84
|
+
</li>
|
85
|
+
<li class="list-element pair" style='font-family: "Lucida Grande", Lucida, Verdana, sans-serif;margin: 4.0px 3.0px 2.0px 1.0px;padding: 0.0;background-color: red;'>
|
86
|
+
<span style='font-family: "Lucida Grande", Lucida, Verdana, sans-serif;margin: 4.0px 3.0px 2.0px 1.0px;padding: 0.0;'>4</span>
|
87
|
+
</li>
|
88
|
+
</ul>
|
89
|
+
|
90
|
+
|
91
|
+
== Requirements:
|
92
|
+
|
93
|
+
nokogiri && (css_parser || maca-fork-csspool)
|
94
|
+
|
95
|
+
|
96
|
+
== Install:
|
97
|
+
(in your Gemfile)
|
98
|
+
gem 'maca-fork-csspool' # Optional requires librcroco to be installed
|
99
|
+
gem "middleman-inline-style"
|
100
|
+
|
101
|
+
bundle install
|
102
|
+
|
103
|
+
|
104
|
+
== License:
|
105
|
+
|
106
|
+
(The MIT License)
|
107
|
+
|
108
|
+
Copyright (c) 2009 Macario Ortega
|
109
|
+
|
110
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
111
|
+
a copy of this software and associated documentation files (the
|
112
|
+
'Software'), to deal in the Software without restriction, including
|
113
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
114
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
115
|
+
permit persons to whom the Software is furnished to do so, subject to
|
116
|
+
the following conditions:
|
117
|
+
|
118
|
+
The above copyright notice and this permission notice shall be
|
119
|
+
included in all copies or substantial portions of the Software.
|
120
|
+
|
121
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
122
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
123
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
124
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
125
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
126
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
127
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/example.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'open-uri'
|
3
|
+
|
4
|
+
$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
5
|
+
|
6
|
+
require "middleman-inline-style/rule"
|
7
|
+
require "middleman-inline-style/rack-middleware" # This two may be should be required by user if she needs it
|
8
|
+
require "middleman-inline-style/mail-interceptor"
|
9
|
+
|
10
|
+
require "middleman-core"
|
11
|
+
|
12
|
+
::Middleman::Extensions.register(:inline_style) do
|
13
|
+
require "middleman-inline-style/middleman-extension"
|
14
|
+
::Middleman::InlineStyleExtension
|
15
|
+
end
|
16
|
+
|
17
|
+
class InlineStyle
|
18
|
+
CSSParser =
|
19
|
+
if const_defined? :CSSPool
|
20
|
+
require 'middleman-inline-style/csspool_wrapper'
|
21
|
+
CSSPoolWrapper
|
22
|
+
else
|
23
|
+
require 'middleman-inline-style/css_parser_wrapper'
|
24
|
+
CssParserWrapper
|
25
|
+
end
|
26
|
+
|
27
|
+
# @param [String, Nokogiri::HTML::Document] html Html or Nokogiri html to be inlined
|
28
|
+
# @param [Hash] opts Processing options
|
29
|
+
#
|
30
|
+
# @option opts [String] :stylesheets_path (ENV['DOCUMENT_ROOT'])
|
31
|
+
# Stylesheets root path or app's public directory where the stylesheets are to be found
|
32
|
+
def self.process html, opts = {}
|
33
|
+
new(html, opts).process
|
34
|
+
end
|
35
|
+
|
36
|
+
def initialize html, opts = {}
|
37
|
+
@stylesheets_path = opts[:stylesheets_path] || ENV['DOCUMENT_ROOT'] || '.'
|
38
|
+
@html = html
|
39
|
+
@dom = String === html ? Nokogiri.HTML(html) : html
|
40
|
+
end
|
41
|
+
|
42
|
+
def process
|
43
|
+
nodes_with_rules.each_pair do |node, rules|
|
44
|
+
rules = rules.sort_by{ |sel| "#{sel.specificity}%04d" % rules.index(sel) }
|
45
|
+
|
46
|
+
styles = []
|
47
|
+
rules.each do |rule|
|
48
|
+
next if rule.dynamic_pseudo_class
|
49
|
+
rule.declarations.each do |declaration|
|
50
|
+
if defined = styles.assoc(declaration.first)
|
51
|
+
styles[styles.index(defined)] = declaration # overrides defined declaration
|
52
|
+
else
|
53
|
+
styles << declaration
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
style = styles.map{ |declaration| declaration.join(': ') }.join('; ')
|
59
|
+
node['style'] = "#{style};" unless style.empty?
|
60
|
+
end
|
61
|
+
pre_parsed? ? @dom : @dom.to_s
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
def nodes_with_rules
|
66
|
+
nodes, body = {}, @dom.css('body')
|
67
|
+
|
68
|
+
parse_css.rules.each do |rule|
|
69
|
+
body.css(rule.selector).each do |node|
|
70
|
+
nodes[node] ||= []
|
71
|
+
nodes[node].push rule
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
body.css('[style]').each do |node|
|
76
|
+
nodes[node] ||= []
|
77
|
+
nodes[node].push Rule.new ':inline', node['style'], '1000' # :inline is not really a pseudoclass
|
78
|
+
end
|
79
|
+
|
80
|
+
nodes
|
81
|
+
end
|
82
|
+
|
83
|
+
def pre_parsed?
|
84
|
+
@html == @dom
|
85
|
+
end
|
86
|
+
|
87
|
+
# Returns parsed CSS
|
88
|
+
def extract_css
|
89
|
+
@dom.css('style, link[rel=stylesheet]').collect do |node|
|
90
|
+
next unless /^$|screen|all/ === node['media'].to_s
|
91
|
+
node.remove
|
92
|
+
|
93
|
+
if node.name == 'style'
|
94
|
+
node.content
|
95
|
+
else
|
96
|
+
uri = %r{^https?://} === node['href'] ? node['href'] : File.join(@stylesheets_path, node['href'].sub(/\?.+$/,''))
|
97
|
+
open(uri).read
|
98
|
+
end
|
99
|
+
end.join("\n")
|
100
|
+
end
|
101
|
+
|
102
|
+
def parse_css
|
103
|
+
CSSParser.new extract_css
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'css_parser'
|
2
|
+
|
3
|
+
class InlineStyle
|
4
|
+
class CssParserWrapper
|
5
|
+
attr_accessor :rules
|
6
|
+
|
7
|
+
def initialize(css_code)
|
8
|
+
parser, @rules = CssParser::Parser.new, []
|
9
|
+
parser.add_block! css_code
|
10
|
+
parser.each_rule_set do |rule_set|
|
11
|
+
rule_set.each_selector { |sel, dec, spec| @rules << Rule.new(sel, dec, '%04d' % spec.to_i) }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class InlineStyle
|
2
|
+
class CSSPoolWrapper
|
3
|
+
attr_accessor :rules
|
4
|
+
|
5
|
+
def initialize css_code
|
6
|
+
parser = CSSPool.CSS css_code
|
7
|
+
@rules = parser.rule_sets.map do |rule_set|
|
8
|
+
rule_set.selectors.map { |sel| Rule.new(sel.to_s, sel.declarations.join, "0#{sel.specificity.join}") }
|
9
|
+
end.flatten
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# A interceptor for +mail+ (https://github.com/mikel/mail) to
|
2
|
+
# automatically inline the styles of outgoing e-mails. To use:
|
3
|
+
#
|
4
|
+
# Mail.register_interceptor \
|
5
|
+
# InlineStyle::Mail::Interceptor.new(:stylesheets_path => 'public')
|
6
|
+
#
|
7
|
+
# Rails 3's ActionMailer wraps around the +mail+ and also supports
|
8
|
+
# interceptors. Example usage:
|
9
|
+
#
|
10
|
+
# ActionMailer::Base.register_interceptor \
|
11
|
+
# InlineStyle::Mail::Interceptor.new(:stylesheets_path => 'public')
|
12
|
+
#
|
13
|
+
module InlineStyle::Mail
|
14
|
+
class Interceptor
|
15
|
+
# The mime types we should inline. Basically HTML and XHTML.
|
16
|
+
# If you have something else you can just push it onto the list
|
17
|
+
INLINE_MIME_TYPES = %w(text/html application/xhtml+xml)
|
18
|
+
|
19
|
+
# Save the options to later pass to InlineStyle.process
|
20
|
+
def initialize(options={})
|
21
|
+
@options = options
|
22
|
+
end
|
23
|
+
|
24
|
+
# Mail callback where we actually inline the styles
|
25
|
+
def delivering_email(part)
|
26
|
+
if part.multipart?
|
27
|
+
for part in part.parts
|
28
|
+
delivering_email part
|
29
|
+
end
|
30
|
+
elsif INLINE_MIME_TYPES.any? {|m| part.content_type && part.content_type.starts_with?(m)}
|
31
|
+
part.body = InlineStyle.process(part.body.to_s, @options)
|
32
|
+
part.content_transfer_encoding = nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module InlineStyle::Rack
|
2
|
+
class Middleware
|
3
|
+
# @param [Hash] opts Middlewar options
|
4
|
+
#
|
5
|
+
# @option opts [String] :stylesheets_path (env['DOCUMENT_ROOT'])
|
6
|
+
# Stylesheets root path or app's public directory where the stylesheets are to be found
|
7
|
+
# @option opts [Regexp, Array, String] :paths
|
8
|
+
# Limit processing to the passed absolute paths
|
9
|
+
# Can be an array of strings or regular expressions, a single string or regular expression
|
10
|
+
# If not passed will process output for every path.
|
11
|
+
# Regexps and strings must comence with '/'
|
12
|
+
# @option opts [Boolean] :pseudo (false)
|
13
|
+
# If set to true will inline style for pseudo classes according to the W3C specification:
|
14
|
+
# http://www.w3.org/TR/css-style-attr.
|
15
|
+
# Should probably be left as false because browsers don't seem to comply with the specification for pseudo class style in the style attribute.
|
16
|
+
def initialize app, opts = {}
|
17
|
+
@app = app
|
18
|
+
@opts = opts
|
19
|
+
@paths = /^(?:#{ [*opts[:paths]].join('|') })/
|
20
|
+
end
|
21
|
+
|
22
|
+
def call env
|
23
|
+
response = @app.call env
|
24
|
+
return response unless @paths === env['PATH_INFO']
|
25
|
+
|
26
|
+
status, headers, body = response
|
27
|
+
newbody = []
|
28
|
+
body.each { |str| newbody << str }
|
29
|
+
newbody = newbody.join('')
|
30
|
+
newbody = InlineStyle.process(newbody, {:stylesheets_path => env['DOCUMENT_ROOT']}.merge(@opts))
|
31
|
+
headers['Content-Length'] = Rack::Utils.bytesize(newbody).to_s
|
32
|
+
[status, headers, [newbody]]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|