Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Or:
Finding a style name that is in the style sheet but not in any html file (widow) or a style used in html files that is not in the style sheet (orphan).

Parsing a local copy of a web site on winXP with Activestate.

Method:
Initialise hash with style names from the style sheet,
Extract styles used by html and add file names to each style key and
Output report.

Orphans can be identified while parsing the html file (the class key won't exist) and widows during output (usage count will be zero). It is often useful to identify rarely used styles, say less than 10 occurances. The output loop can trap these if required. As is, it generates 18k records which is a tad excessive so I either add more filters or use other scripts to filter areas of concern.

The style sheet only uses simple styles in the form of:
.red
h1.green
.redlink:hover etc.
(i.e. anything between a dot and an optional hyphon)

Web site overview:
approx 2k html pages (30MB). More details on my scratchpad.

This is part of an attempt to satisfy CSS2. I'm just hoping I can get there before CSS3 comes out! I don't have access to peer review so would appreciate any comments. I realise that it will be difficult to test without a handy web site on your hard disk but any general observations will be welcome.

Code...

#!/bin/perl5 use strict; use warnings; use HTML::TokeParser::Simple; use File::Find; use CSS::Tiny; # use diagnostics; # use Data::Dumper; my $dir = q|c:/web/root/|; # initialise hash with class names from the style sheet my $css = CSS::Tiny->new(); $css = CSS::Tiny->read( "${dir}cwi.css" ) or die "can't open style sheet $!"; my %class_hash; for my $style ( keys %{$css} ){ # a class name lies beween a dot and an optional hyphen $class_hash{$_}{'zz total'} = 0 if ($_) = $style =~ /\.([^:]*)/ ; } find(\&action, $dir); # generate report open my $o, '>', 'classes.txt' or die "can't open file for output $!"; for my $class ( sort keys %class_hash ){ my $total = $class_hash{$class}{'zz total'}; # next if $total > 10; my $widow = $total ? '' : 'widow'; print $o $class, "\t", $widow, "\n"; for my $path ( sort keys %{$class_hash{$class}} ){ print $o "\t$path\t$class_hash{$class}{$path}\n"; } } close $o; print "done\n"; sub action { # extract class names from each html file return if -d and ( /^_/ or /^pics/ ) and do { $File::Find::prune = 1 }; return unless -f and /\.html$/; my $tp = HTML::TokeParser::Simple->new( $_ ) or die "error opening html file $!"; while ( my $t = $tp->get_token ) { my $classes = $t->return_attr( 'class' ); if ( $classes ){ (my $path = $File::Find::name) =~ s/$dir//; # a class attribute may contain more than one class my @class_array = split ' ', $classes; for my $class (@class_array){ unless ( exists $class_hash{$class} ){ $class_hash{$class}{'zz orphan'}++; } $class_hash{$class}{$path}++; $class_hash{$class}{'zz total'}++; } } } }
Example output:
(indent2 is a widow and indent3 is an orphan)
indent2 widow contents.html 15 publications/tt/contents.html 15 zz total 30 indent3 contents.html 35 zz orphan 35 zz total 35 intro dutch/home.html 1 eng/2003/02/24history.html 2 french/home.html 1 german/home.html 1 home.html 1 italian/home.html 1 portugese/home.html 1 redirect.html 1 spanish/home.html 1 swe/home.html 1 turkish/home.html 1 zz total 12 langpage dutch/2003/1410.html 1 dutch/2003/20030330resist.html 1 dutch/2003/20030424.html 1 dutch/2003/20030730.html 1 dutch/2003/20030911.html 1 dutch/2003/20031014.html 1 dutch/2003/template.html 1

Update:
This all assumes, of course, that all styles are in one external sheet (which they are in this case).

Update 2:
Added clarification.


In reply to Identifying CSS widows and orphans by wfsp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-03-29 10:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found