Giter Club home page Giter Club logo

inline-style's Introduction

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.

inline-style's People

Contributors

eric1234 avatar gaiottino avatar ragga avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.