Skip to content

Instantly share code, notes, and snippets.

@tokuhirom
Created February 5, 2009 09:57
Show Gist options
  • Save tokuhirom/58630 to your computer and use it in GitHub Desktop.
Save tokuhirom/58630 to your computer and use it in GitHub Desktop.
package HTML::CSSInliner;
use Moose;
use HTML::TreeBuilder;
use CSS::Tiny;
has css => (
is => 'rw',
isa => 'CSS::Tiny',
);
sub inline {
my ($self, $src) = @_;
my $css = $self->css;
# hr タグの変換
if ($css->{hr}) {
my $color = $css->{hr}->{color} || $css->{hr}->{'border-color'};
if ($color) {
$src =~ s{<hr([^>]*?)/?>}{
"<hr$1 style='color: $color; border-color: $color;' />";
}ge;
}
}
# class の適用
$src =~ s{<([^<>]+class=['"]?([^<>'"]+)['"]?[^<>]*)>}{
my $e;
while (my ($key , $val) = each %{ $css->{".$2"} }) {
$e .= "$key: $val;";
}
"<$1 style='$e'>";
}ge;
$src;
}
no Moose;
__PACKAGE__->meta->make_immutable;
__END__
=head1 DESCRIPTION
DoCoMo 端末は CSS を外部ファイルに分離することができないため、サーバサイドで適用する。
HTML::DoCoMoCSS という同等の機能をもつモジュールが CPAN にあがっているが、これは実行効率が悪いため車輪を再発明する。
HTML::DoCoMoCSS は、肝要なポリシーにしたがって実装されており、フルスペックの CSS をサポートしているのが遅い原因なので、本モジュールでは機能をしぼり、速度を向上させる。
- hr タグおよび .Foo の2種類のタグのみをサポートする
- 正規表現で実現する(はずれても泣かない)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment