From 7beaf372a7d5341d9051fe12ac563fee97ba9256 Mon Sep 17 00:00:00 2001 From: Macario Date: Fri, 23 Oct 2009 19:07:09 -0500 Subject: [PATCH] trying to make hover work --- Manifest.txt | 2 - README.rdoc | 1 + Rakefile | 2 +- example.rb | 7 +- inline-style.gemspec | 12 +- lib/inline-style.rb | 32 ++-- rack-inline-styles.gemspec | 37 ----- spec/fixtures/boletin.html | 252 ++++++++++++++++++++++++++++++ spec/fixtures/inlne.html | 106 +++++++++++++ spec/fixtures/with-style-tag.html | 2 +- 10 files changed, 393 insertions(+), 60 deletions(-) delete mode 100644 rack-inline-styles.gemspec create mode 100644 spec/fixtures/boletin.html create mode 100644 spec/fixtures/inlne.html diff --git a/Manifest.txt b/Manifest.txt index 79c91da..0f45edb 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -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 diff --git a/README.rdoc b/README.rdoc index 6e13887..28b4701 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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: diff --git a/Rakefile b/Rakefile index 3c21043..4e7cb0a 100644 --- a/Rakefile +++ b/Rakefile @@ -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}"] diff --git a/example.rb b/example.rb index f336660..f5c4cd4 100644 --- a/example.rb +++ b/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) diff --git a/inline-style.gemspec b/inline-style.gemspec index 4733aa9..5bc004e 100644 --- a/inline-style.gemspec +++ b/inline-style.gemspec @@ -2,11 +2,11 @@ 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. @@ -14,7 +14,7 @@ correctly render stylesheets in some clients such as gmail. * 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"] @@ -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, [">= 1.3.3"]) - s.add_runtime_dependency(%q, [">= 2.0.0"]) + s.add_runtime_dependency(%q, [">= 2.0.1"]) s.add_development_dependency(%q, [">= 1.4.1"]) s.add_development_dependency(%q, [">= 1.8.0"]) else s.add_dependency(%q, [">= 1.3.3"]) - s.add_dependency(%q, [">= 2.0.0"]) + s.add_dependency(%q, [">= 2.0.1"]) s.add_dependency(%q, [">= 1.4.1"]) s.add_dependency(%q, [">= 1.8.0"]) end else s.add_dependency(%q, [">= 1.3.3"]) - s.add_dependency(%q, [">= 2.0.0"]) + s.add_dependency(%q, [">= 2.0.1"]) s.add_dependency(%q, [">= 1.4.1"]) s.add_dependency(%q, [">= 1.8.0"]) end diff --git a/lib/inline-style.rb b/lib/inline-style.rb index 5364e76..4026f1a 100644 --- a/lib/inline-style.rb +++ b/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 @@ -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 @@ -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 diff --git a/rack-inline-styles.gemspec b/rack-inline-styles.gemspec deleted file mode 100644 index ee48610..0000000 --- a/rack-inline-styles.gemspec +++ /dev/null @@ -1,37 +0,0 @@ -# -*- encoding: utf-8 -*- - -Gem::Specification.new do |s| - s.name = %q{rack-inline-styles} - s.version = "0.0.1" - - s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= - s.authors = ["Macario Ortega"] - s.date = %q{2009-10-16} - s.description = %q{FIX (describe your package)} - s.email = ["macarui@gmail.com"] - s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc"] - s.files = [".git/HEAD", ".git/config", ".git/description", ".git/hooks/applypatch-msg", ".git/hooks/commit-msg", ".git/hooks/post-commit", ".git/hooks/post-receive", ".git/hooks/post-update", ".git/hooks/pre-applypatch", ".git/hooks/pre-commit", ".git/hooks/pre-rebase", ".git/hooks/update", ".git/index", ".git/info/exclude", ".git/objects/14/fa7459b8ceed4fef2dae431412c51591335354", ".git/objects/2c/0095bcd01468cbafdbb01befcd08cd60ccfb78", ".git/objects/37/04bf0d18ced6f7c6a85467ca3ae017e87d9bf4", ".git/objects/55/8a5df09d6f6b75f07504dd86ead7a0078c9031", ".git/objects/a6/cfe2d2693a7d73be08898b26a1f44886878c21", ".git/objects/e4/f29ca4d0099d827535a935fed78a2aa70fb3e7", ".git/objects/f0/4fbfb5595b8187bf753bdedebb89ee835c28ef", "History.txt", "Manifest.txt", "README.rdoc", "Rakefile", "lib/rack/inline-styles.rb", "rack-inline-styles.gemspec", "spec/spec_helper.rb"] - s.has_rdoc = true - s.homepage = %q{http://github.com/#{github_username}/#{project_name}} - s.rdoc_options = ["--main", "README.rdoc"] - s.require_paths = ["lib"] - s.rubyforge_project = %q{rack-inline-styles} - s.rubygems_version = %q{1.3.1} - s.summary = %q{FIX (describe your package)} - - if s.respond_to? :specification_version then - current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION - s.specification_version = 2 - - if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then - s.add_development_dependency(%q, [">= 1.4.1"]) - s.add_development_dependency(%q, [">= 1.8.0"]) - else - s.add_dependency(%q, [">= 1.4.1"]) - s.add_dependency(%q, [">= 1.8.0"]) - end - else - s.add_dependency(%q, [">= 1.4.1"]) - s.add_dependency(%q, [">= 1.8.0"]) - end -end diff --git a/spec/fixtures/boletin.html b/spec/fixtures/boletin.html new file mode 100644 index 0000000..12bfa92 --- /dev/null +++ b/spec/fixtures/boletin.html @@ -0,0 +1,252 @@ + + + + + + Boletín CENART - November + + + + + + + + + + + + + + + + + + + + +
+
Infantiles
+
+ + +
+
Transitio_mx
+ +

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+
+
Fechas: Del 1 de noviembre del 2009 al 3 de enero del 2010 a las 10:00 horas.
+
Lugar: Centro Multimedia
+
Público: Infantil, cupo Limitado
+
Precio: Entrada libre
+
+ + +
+
+ + + + + + + + + +
+
Exposiciones virtuales
+
+ + +
+
Cumpleaños de Lau
+ +

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+
+
Fechas: 3 de Noviembre del 2009 a las 20:00
+
Lugar: CMM
+
Público: Selecto, cupo 100
+
Precio: Entrada libre
+
+ + +
+
+ + + + + +
+
Teatro
+
+ + +
+
Ya viene la navidad...
+ +

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+
+
Fechas: 1, 2 y 3 de noviembre a las 16:00
+
Lugar: CENART
+
Público: Todo público, cupo Limitado
+
Precio: Entrada libre
+
+ + +
+
+ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spec/fixtures/inlne.html b/spec/fixtures/inlne.html new file mode 100644 index 0000000..9eebda4 --- /dev/null +++ b/spec/fixtures/inlne.html @@ -0,0 +1,106 @@ + + + + +  Boletín CENART - November + + + +   + + +   + +   +  
+
Infantiles
+    
+       +         +        
+
Transitio_mx
+           +          
+

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+
+          
+Fechas: Del 1 de noviembre del 2009 al 3 de enero del 2010 a las 10:00 horas.
+          
+Lugar: Centro Multimedia
+          
+Público: Infantil, cupo Limitado
+          
+Precio: Entrada libre
+        
+
+  
+
+
Exposiciones virtuales
+    
+       +         +        
+
Cumpleaños de Lau
+           +          
+

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+
+          
+Fechas: 3 de Noviembre del 2009 a las 20:00
+          
+Lugar: CMM
+          
+Público: Selecto, cupo 100
+          
+Precio: Entrada libre
+        
+
+  
+
+
Teatro
+    
+       +         +        
+
Ya viene la navidad...
+           +          
+

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+
+          
+Fechas: 1, 2 y 3 de noviembre a las 16:00
+          
+Lugar: CENART
+          
+Público: Todo público, cupo Limitado
+          
+Precio: Entrada libre
+        
+
+  
+ + + diff --git a/spec/fixtures/with-style-tag.html b/spec/fixtures/with-style-tag.html index 9211844..d2cbd68 100644 --- a/spec/fixtures/with-style-tag.html +++ b/spec/fixtures/with-style-tag.html @@ -33,7 +33,7 @@ } - +