Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Extract inline styles to an external style sheet ( was Re: Re: Using a config file in my regexp script.)

by clscott (Friar)
on Sep 17, 2003 at 19:13 UTC ( #292225=note: print w/replies, xml ) Need Help??


in reply to Re: Using a config file in my regexp script.
in thread Using a config file in my regexp script.

In regards to jeffas final suggestion here is a utility that will strip out your inline style attributes, put them in an external stylesheet, output a new html file with the style attributes removed and the new css file linked in.

All with HTML::TokeParser::Simple

Some work to beautify the output or make it operate over mulitple files is left as an excercise for the reader

#!/opt/perl/bin/perl -w use strict; use warnings; use HTML::TokeParser::Simple; use IO::File; my ( $htmlInfile, $htmlOutfile, $cssOutfile ) = @ARGV; $htmlOutfile ||= 'out.html'; $cssOutfile ||= 'out.css' ; my $htmlFile = new IO::File "> $htmlOutfile" or die "Can't open $ht +mlOutfile for writing: $!\n"; my $cssFile = new IO::File "> $cssOutfile" or die "Can't open $css +Outfile for writing: $!\n"; my $parser = HTML::TokeParser::Simple->new($htmlInfile); my %styles; while (my $token = $parser->get_token) { # link in our new css file if ($token->is_end_tag('/head') ){ $htmlFile->print( "<link rel='stylesheet' type='text/css' href +='$cssOutfile'>\n" ); } # find and remove inline style definitions if ($token->is_start_tag) { my $tag = $token->return_tag; my $attr = $token->return_attr; # If we have an inline style attribute get the value # then delete it from the tag if ( defined $attr->{style} ){ $styles{ $attr->{style} }{$tag} += 1; $token->delete_attr('style'); } } $htmlFile->print( $token->as_is ); } # print our collected styles into the css file while (my ( $style, $tags ) = each %styles ){ #li, p { font-family: Times; font-size: 10pt } $cssFile->print( join( ', ', sort keys( %$tags ) ) . " { $style }\ +n"); }
--
Clayton
  • Comment on Extract inline styles to an external style sheet ( was Re: Re: Using a config file in my regexp script.)
  • Download Code

Replies are listed 'Best First'.
Re: Extract inline styles to an external style sheet ( was Re: Re: Using a config file in my regexp script.)
by Anonymous Monk on Jul 08, 2015 at 14:36 UTC
    This does not handle fonts?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://292225]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2019-03-26 14:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (125 votes). Check out past polls.

    Notices?