views:

524

answers:

3

Many email clients don't like linked CSS stylesheets, or even the embedded <style> tag, but rather want the CSS to appear inline as style attributes on all your markup.

  • BAD: <link rel=stylesheet type="text/css" href="/style.css">
  • BAD: <style type="text/css">...</style>
  • WORKS: <h1 style="margin: 0">...</h1>

However this inline style attribute approach is a right pain to manage.

I've found tools for Ruby and PHP that will take a CSS file and some separate markup as input and return you the merged result - a single file of markup with all the CSS converted to style attributes.

I'm looking for a Perl solution to this problem, but I've not found one on CPAN or by searching Google. Any pointers? Alternatively, are there CPAN modules one could combine to achieve the same result?

+10  A: 

I do not know of a complete, pre-packaged solution.

CSS::DOM's compute_style is subject to pretty much the same caveats as emogrifier above. That module, in conjunction with HTML::TokeParser ought to be usable to cook up something.

Update: Here is a buggy mish-mash of things:

#!/usr/bin/perl

use strict;
use warnings;

use CSS::DOM;
use File::Slurp;
use HTML::DOM;
use HTML::TokeParser;

die "convert html_file css_file" unless @ARGV == 2;
my ($html_file, $css_file) = @ARGV;

my $html_parser = HTML::TokeParser->new($html_file)
    or die "Cannot open '$html_file': $!";

my $sheet = CSS::DOM::parse( scalar read_file $css_file );

while ( my $token = $html_parser->get_token ) {
    my $type = $token->[0];
    my $text = $type eq 'T' ? $token->[1] : $token->[-1];
    if ( $type eq 'S' ) {
        unless ( skip( $token->[1] ) ) {
            $text = insert_computed_style($sheet, $token);
        }
    }
    print $text;
}

sub insert_computed_style {
    my ($sheet, $token) = @_;
    my ($tag, $attr, $attrseq) = @$token[1 .. 3];
    my $doc = HTML::DOM->new;

    my $element = $doc->createElement($tag);

    for my $attr_name ( @$attrseq ) {
        $element->setAttribute($attr_name, $attr->{$attr_name});
    }

    my $style = CSS::DOM::compute_style(
        element => $element, user_sheet => $sheet
    );

    my @attrseq = (style => grep { lc $_ ne 'style' } @$attrseq );
    $attr->{style} = $style->cssText;

    my $text .= join(" ",
        "<$tag",
        map{ qq/$_='$attr->{$_}'/ } @attrseq );
    $text .= '>';

    return $text;
}

sub skip {
    my ($tag) = @_;
    $tag = lc $tag;
    return 1 if $tag =~ /^(?:h(?:ead|tml)|link|meta|script|title)$/;
}
Sinan Ünür
Thank you - that is pretty amazing, although I think there is a bug/todo with CSS::Dom (which looks a little fragile still!) that skuppers things a little #foo { border-width: 2px } div { border: 1px dashed green }with the html <div id=foo> ... </div>ought to get style='border: 1px dashed green; border-width: 2px;'but currently gets style='border-width: 2px; border: 1px dashed green'
@mintywalker That is a problem. I have looked at the source for `compute_style` and I am not sure I can fix that right now. A kludgy fix is to change the order in the CSS file: div { border: 1px dashed green } #foo { border-width: 8px } but that is not practical in the general case.
Sinan Ünür
I'm going to have a play with CSS::DOM - for my purposes, spotting and throwing an exception would be better than emitting something that was wrong, so I'm wondering if I might be able to tweak compute_style in that direction at least. I'll report back here if I get anywhere (which is not guaranteed at all!)Again - many thanks, it's a very helpful start.
A: 

Still interested? Am looking for something similar and found something at http://sedition.com/a/156, have not tested it yet...

lmeurs
I think this has the same problem as the earlier answer from Sinan Ünür. See my comment on that. Often it will work fine but (I think) you cannot guarantee it will always work because it isn't resolving the precedence of the css rules correctly. A css selector for "#foo" should win over css selector for "div" but depending on the order they appear in, your code will get the order confused and thus return the wrong thing. Do please correct me if I'm missing something tho.
+1  A: 

You can use CSS::Inliner http://search.cpan.org/dist/CSS-Inliner/

Victor Sanchez