Coder Social home page Coder Social logo

andsome / premailer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from peterbe/premailer

0.0 7.0 0.0 138 KB

Turns CSS blocks into style attributes

Home Page: http://www.peterbe.com/plog/premailer.py

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%

premailer's Introduction

premailer

this is a fork that allows you to use media queries

Turns CSS blocks into style attributes

When you send HTML emails you can't used style tags but instead you have to put inline style attributes on every element. So from this:

    <html>
    <style type="text/css">
    h1 { border:1px solid black }
    p { color:red;}
    p::first-letter { float:left; }
    </style>
    <h1 style="font-weight:bolder">Peter</h1>
    <p>Hej</p>
    </html>

You want this:

    <html>
    <h1 style="font-weight:bolder; border:1px solid black">Peter</h1>
    <p style="{color:red} ::first-letter{float:left}">Hej</p>
    </html>

premailer does this. It parses an HTML page, looks up style blocks and parses the CSS. It then uses the lxml.html parser to modify the DOM tree of the page accordingly.

Getting started

If you havena't already done so, install premailer first:

    $ pip install premailer

Next, the most basic use is to use the shortcut function, like this:

    >>> from premailer import transform
    >>> print transform("""
    ...         <html>
    ...         <style type="text/css">
    ...         h1 { border:1px solid black }
    ...         p { color:red;}
    ...         p::first-letter { float:left; }
    ...         </style>
    ...         <h1 style="font-weight:bolder">Peter</h1>
    ...         <p>Hej</p>
    ...         </html>
    ... """)
    <html>
    <head></head>
    <body>
    <h1 style="{font-weight:bolder; border:1px solid black}::first-lette\
    r{font-weight:bolder}">Peter</h1>
            <p style="{color:red} ::first-letter{float:left}">Hej</p>
            </body>
    </html>

For more advanced options, check out the code of the Premailer class and all its options in its constructor.

Turning relative URLs into absolute URLs

Another thing premailer can do for you is to turn relative URLs (e.g. "/some/page.html" into "http://www.peterbe.com/some/page.html"). It does this to all href and src attributes that don't have a :// part in it. For example, turning this:

    <html>
    <body>
    <a href="/">Home</a>
    <a href="page.html">Page</a>
    <a href="http://crosstips.org">External</a>
    <img src="/folder/">Folder</a>
    </body>
    </html>

Into this:

    <html>
    <body>
    <a href="http://www.peterbe.com/">Home</a>
    <a href="http://www.peterbe.com/page.html">Page</a>
    <a href="http://crosstips.org">External</a>
    <img src="http://www.peterbe.com/folder/">Folder</a>
    </body>
    </html>

HTML attributes created additionally

Certain HTML attributes are also created on the HTML if the CSS contains any ones that are easily translated into HTML attributes. For example, if you have this CSS: td { background-color:#eee; } then this is transformed into style="background-color:#eee" AND as an HTML attribute bgcolor="#eee".

Having these extra attributes basically as a "back up" for really shit email clients that can't even take the style attributes. A lot of professional HTML newsletters such as Amazon's use this.

Using with Media Queries

For advanced designs this technique can be combined with CSS media queries. But style blocks with such queries must not be put inline. You can specify that using the premailer="ignore" attribute like this:

<html>
<head>
    <style type="text/css">
    /* This is put inline */
    h1 { border:1px solid black }
    p { color:red;}
    p::first-letter { float:left; }
    </style>
    <style type="text/css" premailer="ignore">
    /* This block is left intact and can be used to target modern mail
     * clients that don't need inlining */
    @media only screen and (max-device-width: 480px) {
        /* ... */
    }
    </style>
</head>
<body>
    <!-- ...... -->
</body>
</html>

See the HTML Email Boilerplate project for more information about this technique.

premailer's People

Contributors

peterbe avatar beniwohli avatar revolunet avatar russelldavis avatar bensentropy avatar bradleyayers avatar techniq avatar nicoechaniz avatar

Watchers

James Cloos avatar Mark Sievers avatar Medhat Gayed avatar  avatar  avatar Adi avatar Zephyr Brown 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.