I would like a library to create inlined CSS+HTML from .html and .css files or from an HTML file with tags in the head. I would prefer a PHP library if possible.
By inlined CSS I mean something like
<span style="font: bla bla bla">Hi There!</span>
versus
<style>
.greeting { font: bla bla bla; }
</style>
I often need to put HTML into emails and this would simplify the process greatly.
If anyone is interested, my current solution (for stuff that isn’t restyled often) is to use the Smarty templating engine to create the document, and to assign the style=”” part to a variable inside the template.
Then I can use that variable in each tag (like <td {$td_style}> (FYI – {$variable} is how smarty variables are inserted into a template) and have it generate the appropriate email-friendly HTML.
However I want something that is more general, and for which I can just feed it some HTML and CSS rather than have to convert all of it to a smarty template.
Does anyone know if a library like this exists?
Although I don’t know of a library that handles this, if you’re set on PHP, you could do this fairly easily I would think by using CSSTidy to parse the CSS, and then using an XML parser (ideal if your code is well-formed–maybe SimpleXML or the DOM extension) or one of the PHP-based HTML parsers I see out there to parse the HTML, so you can alter the HTML via DOM methods according to CSS rules.
If you don’t need it tied into a PHP process though, I recommend going with JavaScript for almost anything like this, since it can work in the client-side (and in different environments) and be a handy tool for anyone wishing to do this; you should be able to parse the CSS using http://glazman.org/JSCSSP/ and then using DOMParser() and the IE equivalent as needed (for XHTML) or use innerHTML, this htmlparser, or any other HTML Parsers.
Btw, I know
<style>works in emails since my add-on Color Source injects them to get syntax highlighting in emails, though I guess you were only saying it is more complicated for you to do it that way.