HTML-MobileJpCSS

 view release on metacpan or  search on metacpan

lib/HTML/MobileJpCSS.pm  view on Meta::CPAN

    if ($res->is_success) {
        my $css = CSS::Tiny->read_string($res->content)
            or croak "Can't parse '$url' by @{[ __PACKAGE__ ]}";
        return $css;
    }
    croak "Can't fetch $url by @{[ __PACKAGE__ ]}";
}

sub _replace_style {
    my ($self, $content, $node, $props) = @_;
    my ($tag) = $node =~ /^<([^\s]+).*?>/is;
    my $replace = $node;
    my $style;
    for (keys %$props) {
        $style .= $self->_filter($tag, $_, $props->{$_});
    }
    if ($node =~ /style=".+?"/) {
        $replace =~ s/(style=".+?)"/$1$style"/is;
    }
    else {
        $replace =~ s/<$tag/<$tag style="$style"/is;
    }
    $replace =~ s/[\n\s]+/ /g;
    $content =~ s/$node/$replace/is;
    return $content;
}

sub _filter {
    my ($self, $tagname, $property, $value) = @_;
    return "$property:$value;" unless $StyleMap->{$tagname};
    return "$property:$value;" unless $StyleMap->{$tagname}->{$property};
    my $style = $StyleMap->{$tagname}->{$property};
    my $carrier = $self->{agent}->carrier;
    $carrier =~ s/^V$/S/;
    $carrier =~ s/^H$/W/;    
    if (ref $style eq 'ARRAY') {
        my $prop = $style->[0]->{$value}->{$carrier};
        return "$prop;" if $prop;
    }
    elsif ($style) {
        my $prop = $style->{$carrier};
        return "$prop:$value;" if $prop;
    }
    return "$property:$value;";
}

1;

__END__

=head1 NAME

HTML::MobileJpCSS - css inliner and converter

=head1 SYNOPSIS

  use HTML::MobileJpCSS;
  my $inliner = HTML::MobileJpCSS->new(base_dir => '/path/to/documentroot/');
  $inliner->apply(<<'...');
  <?xml version="1.0" encoding="Shift_JIS"?>
  <!DOCTYPE html PUBLIC "-//i-mode group (ja)//DTD XHTML i-XHTML(Locale/Ver.=ja/2.1) 1.0//EN"
      "i-xhtml_4ja_10.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
  <head>
    <link rel="stylesheet" href="/css/foo.css" />
  </head>
  <body>
    <div class="title">bar</div>
  </body>
  </html>
  ...

  # foo.css
  .title {
    color: red;
  }

  # result
  <?xml version="1.0" encoding="Shift_JIS"?>
  <!DOCTYPE html PUBLIC "-//i-mode group (ja)//DTD XHTML i-XHTML(Locale/Ver.=ja/2.1) 1.0//EN"
      "i-xhtml_4ja_10.dtd">
  <html>
  <head>
  </head>
  <body>
    <div class="title" style="color:red;">bar</div>
  </body>
  </html>

=head1 DESCRIPTION

HTML::MobileJpCSS is css inliner.

this module is possible the specification of a style based EZweb in each career(DoCoMo,EZweb,Softbank[,Willcom])

=head1 METHODS

=over 4

=item new(%option)
    
constructor of HTML::MobileJpCSS->new();

=over 5

=item agent

HTTP_USER_AGENT or instance of HTTP::MobileAgent (default: instance of HTTP::MobileAgent)

=item inliner_ezweb

inline css when user_agent is EZweb (default: I<none>) 

=item base_dir

concatenate local css file specified with '<link rel="stylesheet" href="/css/foo.css">' (default: I<none>)

  my $inliner = HTML::DoCoMoCSS->new(base_dir => '/path/to');

=item css_file

read css file (default: I<none>)
force inline css when user_agent is EZweb

  my $inliner = HTML::DoCoMoCSS->new(css_file => '/path/to/css/foo.css');

=item css

read css (default: I<none>)
force inline css when user_agent is EZweb

  my $inliner = HTML::DoCoMoCSS->new(css => {'.color-red' => { color => 'blue' });

=back

=item apply($content)

=back

=head1 AUTHOR



( run in 1.554 second using v1.01-cache-2.11-cpan-119454b85a5 )