Skip to content

Simple utility and Rack middleware 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.

Notifications You must be signed in to change notification settings

extension/inline-style

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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…

  • Includes a interceptor for the mail gem which allows automatic inline processing for both mail as well as ActionMailer.

  • It takes into account selector specificity.

Usage

gem 'maca-fork-csspool' # this is optional if csspool is required inline-style will resort to it for css parsing, otherwise it will use the pure ruby css_parser
require 'csspool'       # csspool should be faster because it uses librcroco written in C wich can also be an issue, use css_parser if you deploy to heroku

require 'inline-style'

html  = File.read("./index.html")
puts InlineStyle.process(html, :stylesheets_paths => "./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"]

Mail Interceptor

If using the mail library the following code will work:

Mail.register_interceptor \
  InlineStyle::Mail::Interceptor.new(:stylesheets_path => 'public')

If using ActionMailer (which wraps mail):

ActionMailer::Base.register_interceptor \
  InlineStyle::Mail::Interceptor.new(:stylesheets_path => 'public')

Requirements:

nokogiri && (css_parser || maca-fork-csspool)

Install:

sudo gem install inline-style

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.

About

Simple utility and Rack middleware 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.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%