inline-style

github.com/maca/inline-style

DESCRIPTION:

Will take all css in a page (either from linked stylesheet or from style tag) and will embed it in the style attribute for each refered element taking selector specificity and declarator order.

Useful for html email: some clients (gmail, et all) won’t render non inline styles.

  • Includes a Rack middleware for using with Rails, Sinatra, etc…

  • It takes into account selector specificity.

USAGE

require 'inline-style'

html  = File.read("#{ dir }/index.html")
puts InlineStyle.process(html, :stylesheets_paths => "#{ dir }/styles")

index.html contains:

<style type="text/css" media="screen">
* {
  font-family: "Lucida Grande", Lucida, Verdana, sans-serif;
  margin: 4px 3px 2px 1px;
  padding: 0;
}

#list {
  margin: 10;
}

#list li {
  font-family: Arial;
}

.element {
  padding: 10;
}

.odd {
  background-color: black;
}

.pair {
  background-color: red;
}

</style>

<ul id='number' class='listing inlined' style='background-color: yellow'>
  <li class='list-element odd'>
    <span>1</span>
  </li>
  <li class='list-element pair'>
    <span>2</span>
  </li>
  <li class='list-element odd'>
    <span>3</span>
  </li>
  <li class='list-element pair'>
    <span>4</span>
  </li>
</ul>

Will become:

<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;'>
  <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;'>
    <span style='font-family: "Lucida Grande", Lucida, Verdana, sans-serif;margin: 4.0px 3.0px 2.0px 1.0px;padding: 0.0;'>1</span>
  </li>
  <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;'>
    <span style='font-family: "Lucida Grande", Lucida, Verdana, sans-serif;margin: 4.0px 3.0px 2.0px 1.0px;padding: 0.0;'>2</span>
  </li>
  <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;'>
    <span style='font-family: "Lucida Grande", Lucida, Verdana, sans-serif;margin: 4.0px 3.0px 2.0px 1.0px;padding: 0.0;'>3</span>
  </li>
  <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;'>
    <span style='font-family: "Lucida Grande", Lucida, Verdana, sans-serif;margin: 4.0px 3.0px 2.0px 1.0px;padding: 0.0;'>4</span>
  </li>
</ul>

RACK MIDDLEWARE:

# Process all routes:
use InlineStyle::Rack::Middleware

# Restrict processing to single route:
use InlineStyle::Rack::Middleware, :paths => %r(/mails/.*)

# Restrict processing to some routes:
use InlineStyle::Rack::Middleware, :paths => [%r(/mails/.*), "/somepath"]

ISSUES:

doesn’t seems to.

  • It strips any numeric character (Rails may add) at the end of the stylesheet file name, anyway stylesheets should end with .css extension

REQUIREMENTS:

tenderlove’s nokogiri and csspool

INSTALL:

sudo gem install inline-style --source http://gemcutter.org

LICENSE:

(The MIT License)

Copyright © 2009 Macario Ortega

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.