Skip to content

Commit

Permalink
trying to make hover work
Browse files Browse the repository at this point in the history
  • Loading branch information
Macario committed Oct 24, 2009
1 parent bab45be commit 7beaf37
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 60 deletions.
2 changes: 0 additions & 2 deletions Manifest.txt
Expand Up @@ -3,10 +3,8 @@ Manifest.txt
README.rdoc
Rakefile
example.rb
inline-style.gemspec
lib/inline-style.rb
lib/inline-style/rack/middleware.rb
rack-inline-styles.gemspec
spec/as_middleware_spec.rb
spec/css_inlining_spec.rb
spec/fixtures/style.css
Expand Down
1 change: 1 addition & 0 deletions README.rdoc
Expand Up @@ -94,6 +94,7 @@ As rack middleware:
== ISSUES:

* Doesn't work with relative stylesheet links
* It strips any numeric character (Rails may add) at the end of the stylesheet file name, anyway stylesheets should end with .css extension

== REQUIREMENTS:

Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -8,7 +8,7 @@ $hoe = Hoe.new('inline-style', InlineStyle::VERSION) do |p|
p.rubyforge_name = p.name
p.extra_deps = [
['nokogiri','>= 1.3.3'],
['csspool', '>= 2.0.0']
['maca-csspool', '>= 2.0.1']
]
p.extra_dev_deps = [
['newgem', ">= #{::Newgem::VERSION}"]
Expand Down
7 changes: 3 additions & 4 deletions example.rb
@@ -1,6 +1,5 @@
require 'rubygems'
require "#{ dir = File.dirname(__FILE__) }/lib/inline-style"
require 'benchmark'
require "#{ File.dirname(__FILE__) }/lib/inline-style"

html = File.read("#{ dir }/spec/fixtures/with-style-tag.html")
puts InlineStyle.process(html, "#{ dir }/spec/fixtures")
html = File.read("#{ dir = File.dirname(__FILE__) + '/spec/fixtures' }/boletin.html")
puts InlineStyle.process(html, dir)
12 changes: 6 additions & 6 deletions inline-style.gemspec
Expand Up @@ -2,19 +2,19 @@

Gem::Specification.new do |s|
s.name = %q{inline-style}
s.version = "0.0.1"
s.version = "0.1.2"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Macario Ortega"]
s.date = %q{2009-10-22}
s.date = %q{2009-10-23}
s.description = %q{Simple utility for "inlining" all CSS in the style attribute for the html tags. Useful for html emails that won't
correctly render stylesheets in some clients such as gmail.
* Includes a Rack middleware for using with Rails, Sinatra, etc...
* It takes into account selector specificity.}
s.email = ["macarui@gmail.com"]
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc"]
s.files = ["History.txt", "Manifest.txt", "README.rdoc", "Rakefile", "example.rb", "inline-style.gemspec", "lib/inline-style.rb", "lib/inline-style/rack/middleware.rb", "rack-inline-styles.gemspec", "spec/as_middleware_spec.rb", "spec/css_inlining_spec.rb", "spec/fixtures/style.css", "spec/fixtures/with-style-tag.html", "spec/spec_helper.rb"]
s.files = ["History.txt", "Manifest.txt", "README.rdoc", "Rakefile", "example.rb", "lib/inline-style.rb", "lib/inline-style/rack/middleware.rb", "spec/as_middleware_spec.rb", "spec/css_inlining_spec.rb", "spec/fixtures/style.css", "spec/fixtures/with-style-tag.html", "spec/spec_helper.rb"]
s.homepage = %q{http://github.com/maca/inline-style}
s.rdoc_options = ["--main", "README.rdoc"]
s.require_paths = ["lib"]
Expand All @@ -28,18 +28,18 @@ correctly render stylesheets in some clients such as gmail.

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<nokogiri>, [">= 1.3.3"])
s.add_runtime_dependency(%q<csspool>, [">= 2.0.0"])
s.add_runtime_dependency(%q<maca-csspool>, [">= 2.0.1"])
s.add_development_dependency(%q<newgem>, [">= 1.4.1"])
s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
else
s.add_dependency(%q<nokogiri>, [">= 1.3.3"])
s.add_dependency(%q<csspool>, [">= 2.0.0"])
s.add_dependency(%q<maca-csspool>, [">= 2.0.1"])
s.add_dependency(%q<newgem>, [">= 1.4.1"])
s.add_dependency(%q<hoe>, [">= 1.8.0"])
end
else
s.add_dependency(%q<nokogiri>, [">= 1.3.3"])
s.add_dependency(%q<csspool>, [">= 2.0.0"])
s.add_dependency(%q<maca-csspool>, [">= 2.0.1"])
s.add_dependency(%q<newgem>, [">= 1.4.1"])
s.add_dependency(%q<hoe>, [">= 1.8.0"])
end
Expand Down
32 changes: 23 additions & 9 deletions lib/inline-style.rb
@@ -1,22 +1,25 @@
require 'nokogiri'
require 'open-uri'
require 'csspool'
require '/Users/sistemasinteractivos/Gems/Web/csspool/lib/csspool'

require "#{ File.dirname( __FILE__ ) }/inline-style/rack/middleware"

module InlineStyle
VERSION = '0.0.1'
VERSION = '0.1.2'

def self.process html, document_root
nokogiri_doc_given = Nokogiri::HTML::Document === html

html = nokogiri_doc_given ? html : Nokogiri.HTML(html)
css = extract_css html, document_root
nodes = {}

css.rule_sets.each do |rule_set|
rule_set.selectors.each do |selector|
html.css("body #{ selector }").each do |node|

css_selector = selector.to_s
css_selector = "#{ 'body ' unless /^body/ === css_selector }#{ css_selector.gsub /:.*/, '' }"

html.css(css_selector).each do |node|
nodes[node] ||= []
nodes[node].push selector

Expand All @@ -26,15 +29,26 @@ def self.process html, document_root
path << "##{ node['id'] }" if node['id']
path << ".#{ node['class'].scan(/\S+/).join('.') }" if node['class']

CSSPool.CSS("#{ path }{#{ node['style'] }}").rule_sets.each{ |rule| nodes[node].push *rule.selectors}
CSSPool.CSS("#{ path }{#{ node['style'] }}").rule_sets.each{ |rule| nodes[node].push *rule.selectors }
end
end
end

nodes.each_pair do |node, style|
style = style.sort_by{ |sel| "#{ sel.specificity }#{ style.index sel }" } #TO fix
style.map!{ |sel| sel.declarations.map{ |d| d.to_css.strip }.join } and style.uniq!
node['style'] = style.join
style = style.sort_by{ |sel| "#{ sel.specificity }%03d" % style.index(sel) }
sets = style.partition{ |sel| not /:\w+/ === sel.to_s }

node['style'] = sets.collect do |selectors|
next if selectors.empty?

index = sets.index selectors
set = selectors.map do |selector|
declarations = selector.declarations.map{ |d| d.to_css.strip }.join
index == 0 ? declarations : "#{ selector.to_s.gsub /\w(?=:)/, '' } {#{ declarations }}"
end

index == 0 && !sets.last.empty? ? "{#{ set }}" : set.join
end.compact.join(";")
end

nokogiri_doc_given ? html : html.to_s
Expand All @@ -47,7 +61,7 @@ def self.extract_css html, document_root
next unless e['rel'] == 'stylesheet'
e.remove
next open(e['href']).read if %r{^https?://} === e['href']
File.read File.join(document_root, e['href'])
File.read File.join(document_root, e['href'].sub(/\?\d+$/,'')) rescue ''
}.join("\n")
end

Expand Down
37 changes: 0 additions & 37 deletions rack-inline-styles.gemspec

This file was deleted.

0 comments on commit 7beaf37

Please sign in to comment.