#!/usr/bin/env perl package HTML::RewriteAttributes::Resources; use strict; use warnings; use base 'HTML::RewriteAttributes'; use URI; our $VERSION = '0.02'; my %rewritable_attrs = ( bgsound => { src => 1 }, body => { background => 1 }, img => { src => 1 }, input => { src => 1 }, table => { background => 1 }, td => { background => 1 }, th => { background => 1 }, tr => { background => 1 }, ); sub _rewrite { my $self = shift; my $html = shift; my $cb = shift; my %args = @_; $self->{rewrite_inline_css_cb} = $args{inline_css}; $self->{rewrite_inline_imports} = $args{inline_imports}; $self->{rewrite_inline_imports_seen} = {}; $self->SUPER::_rewrite($html, $cb); } sub _should_rewrite { my ($self, $tag, $attr) = @_; return ( $rewritable_attrs{$tag} || {} )->{$attr}; } sub _invoke_callback { my $self = shift; my ($tag, $attr, $value) = @_; return $self->{rewrite_callback}->($value, tag => $tag, attr => $attr, rewriter => $self); } sub _start_tag { my $self = shift; my ($tag, $attr, $attrseq, $text) = @_; if ($self->{rewrite_inline_css_cb}) { if ($tag eq 'link' && $attr->{type} eq 'text/css') { my $content = $self->_import($attr->{href}); if (defined $content) { $content = $self->_handle_imports($content, $attr->{href}); $self->{rewrite_html} .= "\n\n"; return; } } if ($tag eq 'style' && $attr->{type} eq 'text/css') { $self->{rewrite_look_for_style} = 1; } } $self->SUPER::_start_tag(@_); } sub _default { my ($self, $tag, $attrs, $text) = @_; if (delete $self->{rewrite_look_for_style}) { $text = $self->_handle_imports($text, '.'); } $self->SUPER::_default($tag, $attrs, $text); } sub _handle_imports { my $self = shift; my $content = shift; my $base = shift; return $content if !$self->{rewrite_inline_imports}; $content =~ s{ \@import \s* " ([^"]+) " \s* ; }{ $self->_import($self->_absolutify($1, $base)) }xeg; return $content; } sub _absolutify { my $self = shift; my $path = shift; my $base = shift; my $uri = URI->new($path); unless (defined $uri->scheme) { $uri = $uri->abs($base); } return $uri->as_string; } sub _import { my $self = shift; my $path = shift; return '' if $self->{rewrite_inline_imports_seen}{$path}++; my $content = $self->{rewrite_inline_css_cb}->($path); return $self->_handle_imports($content, $path); } 1; __END__ =head1 NAME HTML::RewriteAttributes::Resources - concise resource-link rewriting =head1 SYNOPSIS # writing some HTML email I see.. $html = HTML::RewriteAttributes::Resources->rewrite($html, sub { my $uri = shift; my $content = render_template($uri); my $cid = generate_cid_from($content); $mime->attach($cid => content); return "cid:$cid"; }); # need to inline CSS too? $html = HTML::RewriteAttributes::Resources->rewrite($html, sub { # see above }, inline_css => sub { my $uri = shift; return render_template($uri); }); # need to inline CSS and follow @imports? $html = HTML::RewriteAttributes::Resources->rewrite($html, sub { # see above }, inline_css => sub { # see above }, inline_imports => 1); =head1 DESCRIPTION C is a special case of L for rewriting links to resources. This is to facilitate generating, for example, HTML email in an extensible way. We don't care about how to fetch resources and attach them to the MIME object; that's your job. But you don't have to care about how to rewrite the HTML. =head1 METHODS =head2 C You don't need to call C explicitly - it's done in L. It takes no arguments. =head2 C HTML, callback[, args] -> HTML See the documentation of L. The callback receives as arguments the resource URI (the attribute value), then, in a hash, C and C. =head3 Inlining CSS C can automatically inline CSS for you. Passing C will invoke that callback to inline C