By blogging about it I can pretty much guarantee that someone will comment and say "Hey, why didn't you use the pypi/alreadyexists package which does the same thing but better". I couldn't find one after a quick search and I felt the hacker mood creeping up on my begging me to (re)invent it.
premailer.py takes a HTML page, finds all CSS blocks and transforms these into style
attributes. For example, from this:
<html>
<head>
<title>Test</title>
<style>
h1, h2 { color:red; }
strong {
text-decoration:none
}
</style>
</head>
<body>
<h1>Hi!</h1>
<p><strong>Yes!</strong></p>
</body>
</html>
You get this:
<html>
<head>
<title>Test</title>
</head>
<body>
<h1 style="color:red">Hi!</h1>
<p><strong style="text-decoration:none">Yes!</strong></p>
</body>
</html>
Why is this useful? When you're writing HTML emails. Like this newsletter app that I'm working on.
I just wrote it late yesterday and it needs lots of work to impress but for the moment it works for me. If I take the time to tidy it up properly I'll turn it into a package. Assuming there isn't one already :)
UPDATE
No available on github.com and as a PyPi package
UPDATE #2
Two new copy-cats have been released:
- python-premailer which seems to do the same thing but without lxml (which is sort of the whole point)
- inline-styler which also uses lxml but I don't know what it does differently or better
Might be worth poking around at these if my premailer isn't good enough.