CSS-Inliner
view release on metacpan or search on metacpan
lib/CSS/Inliner.pm view on Meta::CPAN
unless ($params && $$params{url}) {
croak 'You must pass in hash params that contain a url argument';
}
# fetch and retrieve the remote content
my ($content,$baseref,$ctcharset) = $self->_fetch_url({ url => $$params{url} });
my $charset = $self->detect_charset({ content => $content, charset => $$params{charset}, ctcharset => $ctcharset });
my $decoded_html;
if ($charset) {
$decoded_html = $self->decode_characters({ content => $content, charset => $charset });
}
else {
# no good hints found, do the best we can
if ($self->_fixlatin()) {
Encoding::FixLatin->import('fix_latin');
$decoded_html = fix_latin($content);
}
else {
$decoded_html = $self->decode_characters({ content => $content, charset => 'ascii' });
}
}
my $html = $self->_absolutize_references({ content => $decoded_html, baseref => $baseref });
$self->read({ html => $html, charset => $charset });
return();
}
=head2 read_file
Opens and reads an HTML file that supposedly contains both HTML and a
style declaration, properly tags the data with the proper charset
lib/CSS/Inliner.pm view on Meta::CPAN
unless ($params && $$params{filename}) {
croak "You must pass in hash params that contain a filename argument";
}
open FILE, "<", $$params{filename} or die $!;
my $content = do { local( $/ ) ; <FILE> };
my $charset = $self->detect_charset({ content => $content, charset => $$params{charset} });
my $decoded_html;
if ($charset) {
$decoded_html = $self->decode_characters({ content => $content, charset => $charset });
}
else {
# no good hints found, do the best we can
if ($self->_fixlatin()) {
Encoding::FixLatin->import('fix_latin');
$decoded_html = fix_latin($content);
}
else {
$decoded_html = $self->decode_characters({ content => $content, charset => 'ascii' });
}
}
$self->read({ html => $decoded_html, charset => $charset });
return();
}
=head2 read
Reads passed html data and parses it. The intermediate data is stored in
class variables.
The <style> block is ripped out of the html here, and stored
lib/CSS/Inliner.pm view on Meta::CPAN
croak "You must pass content for content character decoding";
}
unless ($params && $$params{charset}) {
croak "You must pass the charset type of the content to decode";
}
my $content = $$params{content};
my $charset = $$params{charset};
my $decoded_html;
eval {
$decoded_html = decode($charset,$content);
};
if (!$decoded_html) {
croak('Error decoding content with character set "'.$$params{charset}.'"');
}
return $decoded_html;
}
=head2 inlinify
Processes the html data that was entered through either 'read' or
'read_file', returns a scalar that contains a composite chunk of html
that has inline styles instead of a top level <style> declaration.
=cut
lib/CSS/Inliner.pm view on Meta::CPAN
}
}
}
sub _extract_meta_charset {
my ($self,$params) = @_;
$self->_check_object();
# we are going to parse an html document as ascii that is not necessarily ascii - silence the warning
local $SIG{__WARN__} = sub { my $warning = shift; warn $warning unless $warning =~ /^Parsing of undecoded UTF-8/ };
# parse document and pull out key header elements
my $extract_tree = new CSS::Inliner::TreeBuilder();
$self->_configure_tree({ tree => $extract_tree });
$extract_tree->parse_content($$params{content});
my $head = $extract_tree->look_down("_tag", "head"); # there should only be one
my $meta_charset;
( run in 0.261 second using v1.01-cache-2.11-cpan-26ccb49234f )