blib/lib/HTML/Clean.pm

filter: Statementstmt 91.4% 204 / 223 Branchbran 57.8% 59 / 102 Conditioncond 66.6% 8 / 12 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 90.9% 20 / 22 Podpod 100% 7 / 7 totaltotal 80.7% 301 / 373 CC 58 SCAR 26.1
CC
58
Cov
80%
strip
27.4
_level_defaults
18.3
_commentcheck
7.3
CRAP 13.6
↑ Summary
1 package HTML::Clean;
2  
3 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 require 5.004;
8  
9 2 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 require Exporter;
12 2 require AutoLoader;
13  
14 # Items to export to callers namespace
15 2 @EXPORT = qw();
16  
17 2 $VERSION = '1.1';
18  
19 =pod
20  
21 =head1 NAME
22  
23 HTML::Clean - Cleans up HTML code for web browsers, not humans
24  
25 =head1 SYNOPSIS
26  
27 use HTML::Clean;
28 $h = new HTML::Clean($filename); # or..
29 $h = new HTML::Clean($htmlcode);
30  
31 $h->compat();
32 $h->strip();
33 $data = $h->data();
34 print $$data;
35  
36 =head1 DESCRIPTION
37  
38 The HTML::Clean module encapsulates a number of common techniques for
39 minimizing the size of HTML files. You can typically save between
40 10% and 50% of the size of a HTML file using these methods.
41 It provides the following features:
42  
43 =over 8
44  
45 =item Remove unneeded whitespace (begining of line, etc)
46  
47 =item Remove unneeded META elements.
48  
49 =item Remove HTML comments (except for styles, javascript and SSI)
50  
51 =item Replace tags with equivilant shorter tags (<strong> --> <b>)
52  
53 =item etc.
54  
55 =back
56  
57 The entire proces is configurable, so you can pick and choose what you want
58 to clean.
59  
60 =cut
61 =head1 THE HTML::Clean CLASS
62  
63 =over 4
64  
65 =cut
66  
67  
68 ######################################################################
69  
70 =head2 $h = HTML::Clean->new($dataorfile, [$level]);
71  
72 This creates a new HTML::Clean object. A Prerequisite for all other
73 functions in this module.
74  
75 The $dataorfile parameter supplies the input HTML, either a filename,
76 or a reference to a scalar value holding the HTML, for example:
77  
78 $h = HTML::Clean->new("/htdocs/index.html");
79 $html = "<strong>Hello!</strong>";
80 $h = HTML::Clean->new(\$html);
81  
82 An optional 'level' parameter controls the level of optimization
83 performed. Levels range from 1 to 9. Level 1 includes only simple
84 fast optimizations. Level 9 includes all optimizations.
85  
86 =cut
87  
88 sub new {
89 10 ▶ my $this = shift;
Subroutine new: called CC=4 SCAR=15.0
Pod documented
90 10 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0100
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
91 10 my $self = {};
92 10 bless $self, $class;
93  
94 10 my $data = shift;
95 10 my $level = shift;
96  
97 10 ▶ if ($self->initialize($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->initialize($data)) { } 10 0 10
98 # set the default level
99 10 ▶ $level = 9 if (!$level);
Branch: 1 branch
BranchTrueFalseTotal
unless $level 10 0 10
100 10 $self->level($level);
101 10 return $self;
102 } else {
103 0 undef $self;
104 0 return undef;
105 }
106 }
107  
108  
109 #
110 # Set up the data in the self hash..
111 #
112  
113 =head2 $h->initialize($dataorfile)
114  
115 This function allows you to reinitialize the HTML data used by the
116 current object. This is useful if you are processing many files.
117  
118 $dataorfile has the same usage as the new method.
119  
120 Return 0 for an error, 1 for success.
121  
122 =cut
123  
124 sub initialize {
125 13 ▶ my($self, $data) = @_;
Subroutine initialize: called CC=7 SCAR=19.8
Pod documented
126 13 $self->{'DATA'} = undef;
127  
128 # Not defined? Just return true.
129 13 ▶ return(1) if (!$data);
Branch: 1 branch
BranchTrueFalseTotal
unless $data 1 12 13
130  
131 # Check if it's a ref
132 12 ▶ if (ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data) 4 8 12
133 4 $self->{DATA} = $data;
134 4 return(1);
135 }
136  
137 # Newline char, really an error, but just go with it..
138 8 ▶ if ($data =~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /\n/) 0 8 8
139 0 $self->{'DATA'} = \$data;
140 }
141  
142 # No newline? Must be a filename
143 8 ▶ if (-f $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $data) 8 0 8
144 8 my $storage;
145  
146 8 ▶ sysopen(IN, "$data", O_RDONLY) || return(0);
Branch: 1 branch
BranchTrueFalseTotal
sysopen IN, "$data", 0 or return 0 0 8 8
147 8 ▶ while (<IN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline IN)) 2759 8 2767
148 2759 $storage .= $_;
149 }
150 8 close(IN);
151 8 $self->{'DATA'} = \$storage;
152 8 return(1);
153 }
154  
155 0 return(0); # file not found?
156 }
157  
158  
159 =head2 $h->level([$level])
160  
161 Get/set the optimization level. $level is a number from 1 to 9.
162  
163 =cut
164  
165 sub level {
166 13 ▶ my($self, $level) = @_;
Subroutine level: called CC=4 SCAR=14.0
Pod documented
167  
168 13 ▶ if (defined($level) && ($level > 0) && ($level < 10)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $level and $level > 0 and $level < 10) 12 1 13
Condition66%4/6
defined $level and $level > 0 and $level < 10
!ll&&!rl&&r
1012
defined $level and $level > 0
!ll&&!rl&&r
1012
MC/DC33%1/3
defined $level and $level > 0 and $level < 10
defined $level $level > 0 $level < 10
Truth table50%2/4
A and B and C
A= defined $levelB= $level > 0C= $level < 10
ABCresult
0XX0
10X0
1100
1111
169 12 $self->{'LEVEL'} = $level
170 }
171 13 return($self->{'LEVEL'});
172 }
173  
174 =head2 $myref = $h->data()
175  
176 Returns the current HTML data as a scalar reference.
177  
178 =cut
179  
180 sub data {
181 8 ▶ my($self) = @_;
Subroutine data: called CC=1 SCAR=0.0
Pod documented
182  
183 8 return $self->{'DATA'};
184 }
185  
186  
187 # Junk HTML comments (INTERNAL)
188  
189 sub _commentcheck($) {
190 174 ▶ my($comment) = @_;
Subroutine _commentcheck: called CC=7 SCAR=19.9
191  
192 174 $_ = $comment;
193  
194 # Server side include
195 174 ▶ return($comment) if (m,^<!--\#,si);
Branch: 1 branch
BranchTrueFalseTotal
if /^<!--\#/is 0 174 174
196  
197 # ITU Hack.. preserve some frontpage components
198 174 ▶ return($comment) if (m,^<!-- %,si);
Branch: 1 branch
BranchTrueFalseTotal
if /^<!-- %/is 0 174 174
199 174 ▶ return($comment) if (m,bot="(SaveResults|Search|ConfirmationField)",si);
Branch: 1 branch
BranchTrueFalseTotal
if /bot="(SaveResults|Search|ConfirmationField)"/is 0 174 174
200  
201 # Javascript
202 174 ▶ return($comment) if (m,//.*-->$,si);
Branch: 1 branch
BranchTrueFalseTotal
if m[//.*-->$]is 12 162 174
203 162 ▶ return($comment) if (m,navigator\.app(name|version),si);
Branch: 1 branch
BranchTrueFalseTotal
if /navigator\.app(name|version)/is 0 162 162
204  
205 # Stylesheet
206 162 ▶ return($comment) if (m,[A-z0-9]+\:[A-z0-9]+\s*\{.*\},si);
Branch: 1 branch
BranchTrueFalseTotal
if /[A-z0-9]+\:[A-z0-9]+\s*\{.*\}/is 1 161 162
207 161 return('');
208 }
209  
210  
211 # Remove javascript comments (INTERNAL)
212  
213 sub _jscomments {
214 12 ▶ my($js) = @_;
Subroutine _jscomments: called CC=2 SCAR=6.9
215  
216 12 $js =~ s,\n\s*//.*?\n,\n,sig;
217 12 $js =~ s,\s+//.*?\n,\n,sig;
218  
219 # insure javascript is hidden
220  
221 12 ▶ if ($js =~ m,<!--,) {
Branch: 1 branch
BranchTrueFalseTotal
if ($js =~ /<!--/) 7 5 12
222 7 $js =~ s,</script>,// -->\n</script>,si;
223 }
224 12 return($js);
225 }
226  
227 # Clean up other javascript stuff..
228  
229 sub _javascript {
230 12 ▶ my($js) = @_;
Subroutine _javascript: called CC=1 SCAR=0.0
231  
232 # remove excess whitespace at the beginning and end of lines
233 12 $js =~ s,\s*\n+\s*,\n,sig;
234  
235 # braces/semicolon at end of line, join next line
236 12 $js =~ s,([;{}])\n,$1,sig;
237  
238 # What else is safe to do?
239  
240 12 return($js);
241 }
242  
243 # replace #000000 -> black, etc..
244 # Does the browser render faster with RGB? You would think so..
245  
246 sub _defcolorcheck ($) {
247 238 ▶ my($c) = @_;
Subroutine _defcolorcheck: called CC=1 SCAR=0.0
248  
249 238 $c =~ s/\#000000/black/;
250 238 $c =~ s/\#c0c0c0/silver/i;
251 238 $c =~ s/\#808080/gray/;
252 238 $c =~ s/\#ffffff/white/i;
253 238 $c =~ s/\#800000/maroon/;
254 238 $c =~ s/\#ff0000/red/i;
255 238 $c =~ s/\#800080/purple/;
256 238 $c =~ s/\#ff00ff/fuchsia/i;
257 238 $c =~ s/\#ff00ff/fuchsia/i;
258 238 $c =~ s/\#008000/green/;
259 238 $c =~ s/\#00ff00/lime/i;
260 238 $c =~ s/\#808000/olive/;
261 238 $c =~ s/\#ffff00/yellow/i;
262 238 $c =~ s/\#000080/navy/;
263 238 $c =~ s/\#0000ff/blue/i;
264 238 $c =~ s/\#008080/teal/i;
265 238 $c =~ s/\#00ffff/aqua/i;
266 238 return($c);
267 }
268  
269 # For replacing entities with numerics
270 2 ▶use vars qw/ %_ENTITIES/;
Subroutine BEGIN: called CC=1 SCAR=0.0
271 2 %_ENTITIES = (
272 'Agrave' => 192,
273 'Aacute' => 193,
274 'Acirc' => 194,
275 'Atilde' => 195,
276 'Auml' => 196,
277 'Aring' => 197,
278 'AElig' => 198,
279 'Ccedil' => 199,
280 'Egrave' => 200,
281 'Eacute' => 201,
282 'Ecirc' => 202,
283 'Euml' => 203,
284 'Igrave' => 204,
285 'Iacute' => 205,
286 'Icirc' => 206,
287 'Iuml' => 207,
288 'ETH' => 208,
289 'Ntilde' => 209,
290 'Ograve' => 210,
291 'Oacute' => 211,
292 'Ocirc' => 212,
293 'Otilde' => 213,
294 'Ouml' => 214,
295 'Oslash' => 216,
296 'Ugrave' => 217,
297 'Uacute' => 218,
298 'Ucirc' => 219,
299 'Uuml' => 220,
300 'Yacute' => 221,
301 'THORN' => 222,
302 'szlig' => 223,
303 'agrave' => 224,
304 'aacute' => 225,
305 'acirc' => 226,
306 'atilde' => 227,
307 'auml' => 228,
308 'aring' => 229,
309 'aelig' => 230,
310 'ccedil' => 231,
311 'egrave' => 232,
312 'eacute' => 233,
313 'ecirc' => 234,
314 'euml' => 235,
315 'igrave' => 236,
316 'iacute' => 237,
317 'icirc' => 238,
318 'iuml' => 239,
319 'eth' => 240,
320 'ntilde' => 241,
321 'ograve' => 242,
322 'oacute' => 243,
323 'ocirc' => 244,
324 'otilde' => 245,
325 'ouml' => 246,
326 'oslash' => 248,
327 'ugrave' => 249,
328 'uacute' => 250,
329 'ucirc' => 251,
330 'uuml' => 252,
331 'yacute' => 253,
332 'thorn' => 254,
333 'yuml' => 255
334 );
335  
336 =head2 strip(\%options);
337  
338 Removes excess space from HTML
339  
340 You can control the optimizations used by specifying them in the
341 %options hash reference.
342  
343 The following options are recognized:
344  
345 =over 8
346  
347 =item boolean values (0 or 1 values)
348  
349 whitespace Remove excess whitespace
350 shortertags <strong> -> <b>, etc..
351 blink No blink tags.
352 contenttype Remove default contenttype.
353 comments Remove excess comments.
354 entities &quot; -> ", etc.
355 dequote remove quotes from tag parameters where possible.
356 defcolor recode colors in shorter form. (#ffffff -> white, etc.)
357 javascript remove excess spaces and newlines in javascript code.
358 htmldefaults remove default values for some html tags
359 lowercasetags translate all HTML tags to lowercase
360  
361 =item parameterized values
362  
363 meta Takes a space separated list of meta tags to remove,
364 default "GENERATOR FORMATTER"
365  
366 emptytags Takes a space separated list of tags to remove when there is no
367 content between the start and end tag, like this: <b></b>.
368 The default is 'b i font center'
369  
370 =back
371  
372 =cut
373  
374 2 use vars qw/
375 $do_whitespace
376 $do_shortertags
377 $do_meta
378 $do_blink
379 $do_contenttype
380 $do_comments
381 $do_entities
382 $do_dequote
383 $do_defcolor
384 $do_emptytags
385 $do_javascript
386 $do_htmldefaults
387 $do_lowercasetags
388 $do_defbaseurl
389 2 ▶ /;
Subroutine BEGIN: called CC=1 SCAR=0.0
390  
391 2 $do_whitespace = 1;
392 2 $do_shortertags = 1;
393 2 $do_meta = "generator formatter";
394 2 $do_blink = 1;
395 2 $do_contenttype = 1;
396 2 $do_comments = 1;
397 2 $do_entities = 1;
398 2 $do_dequote = 1;
399 2 $do_defcolor = 1;
400 2 $do_emptytags = 'b i font center';
401 2 $do_javascript = 1;
402 2 $do_htmldefaults = 1;
403 2 $do_lowercasetags = 1;
404 2 $do_defbaseurl = '';
405  
406 sub strip {
407 11 ▶ my($self, $options) = @_;
Subroutine strip: called CC=24 SCAR=33.1
Pod documented
408  
409 11 my $h = $self->{'DATA'};
410 11 my $level = $self->{'LEVEL'};
411  
412 # Select a set of options based on $level, and then modify based on
413 # user supplied options.
414  
415 11 _level_defaults($level);
416  
417 11 ▶ if(defined($options)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options) 0 11 11
418 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
419 0 for (keys(%$options)) {
420 0 ▶ ${"do_" . lc($_)} = $options->{$_} if defined ${"do_" . lc($_)};
Branch: 1 branch
BranchTrueFalseTotal
if defined ${'do_' . lc($_);} 0 0 0
421 }
422 }
423  
424 11 ▶ if ($do_shortertags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Clean::do_shortertags) 11 0 11
425 11 $$h =~ s,<strong>,<b>,sgi;
426 11 $$h =~ s,</strong>,</b>,sgi;
427 11 $$h =~ s,<em>,<i>,sgi;
428 11 $$h =~ s,</em>,</i>,sgi;
429 }
430  
431 11 ▶ if ($do_whitespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Clean::do_whitespace) 11 0 11
432 11 $$h =~ s,[\r\n]+,\n,sg; # Carriage/LF -> LF
433 11 $$h =~ s,\s+\n,\n,sg; # empty line
434 11 $$h =~ s,\n\s+<,\n<,sg; # space before tag
435 11 $$h =~ s,\n\s+,\n ,sg; # other spaces
436  
437 11 $$h =~ s,>\n\s*<,><,sg; # LF/spaces between tags..
438  
439 # Remove excess spaces within tags.. note, we could parse out the elements
440 # and rewrite for excess spaces between elements. perhaps next version.
441 # removed due to problems with > and < in tag elements..
442 #$$h =~ s,\s+>,>,sg;
443 #$$h =~ s,<\s+,<,sg;
444 # do this again later..
445 }
446  
447 11 ▶ if ($do_entities) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Clean::do_entities) 11 0 11
448 11 $$h =~ s,&quot;,\",sg;
449 # Simplify long entity names if using default charset...
450 11 $$h =~ m,charset=([^\"]+)\",;
451 11 ▶ if (!defined($1) || ($1 eq 'iso-8859-1')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $1 or $1 eq "iso-8859-1") 10 1 11
Condition100%3/3
not defined $1 or $1 eq "iso-8859-1"
l!l&&r!l&&!r
911
MC/DC100%2/2
not defined $1 or $1 eq "iso-8859-1"
not defined $1 $1 eq "iso-8859-1"
Truth table100%3/3
not(A) or B
A= not defined $1B= $1 eq "iso-8859-1"
ABresult
1X1
011
000
452 10 ▶ $$h =~ s,&([A-z]+);,($_ENTITIES{$1}) ? chr($_ENTITIES{$1}) : $&,sige;
Branch: 1 branch
BranchTrueFalseTotal
$HTML::Clean::_ENTITIES{$1} ? : 4 303 307
453 }
454 }
455  
456 11 ▶ if ($do_meta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Clean::do_meta) 11 0 11
457 11 foreach my $m (split(/\s+/, $do_meta)) {
458 22 $$h =~ s,<meta name="$m"[^>]*?>,,sig;
459 }
460 }
461 11 ▶ if ($do_contenttype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Clean::do_contenttype) 11 0 11
462 # Don't need this, since it is the default for most web servers
463 # Also gets rid of 'blinking pages' in older versions of netscape.
464 11 $$h =~ s,<meta http-equiv="Content-Type".*?content="text/html;.*?charset=iso-8859-1">,,sig;
465 }
466  
467 11 ▶ if ($do_defcolor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Clean::do_defcolor) 11 0 11
468 11 $$h =~ s,(<[^<]+?color=['"]?\#[0-9A-Fa-f]+["']?),_defcolorcheck($&),sige;
469 }
470 11 ▶ if ($do_comments) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Clean::do_comments) 11 0 11
471 # don't strip server side includes..
472 # try not to get javascript, or styles...
473 11 $$h =~ s,<!--.*?-->,_commentcheck($&),sige;
474  
475 # Remove javascript comments
476 11 $$h =~ s,<script[^>]*(java|ecma)script[^>]*>.*?</script>,_jscomments($&),sige;
477 }
478  
479 11 ▶ if ($do_javascript) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Clean::do_javascript) 11 0 11
480 #
481 11 $$h =~ s,<script[^>]*(java|ecma)script[^>]*>.*?</script>,_javascript($&),sige;
482 }
483  
484 11 ▶ if ($do_blink) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Clean::do_blink) 11 0 11
485 11 $$h =~ s,<BLINK>,,sgi;
486 11 $$h =~ s,</BLINK>,,sgi;
487 }
488  
489 11 ▶ if ($do_dequote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Clean::do_dequote) 11 0 11
490 11 ▶ while ($$h =~ s,<([A-z]+ [A-z]+=)(['"])([A-z0-9]+)\2(\s*?[^>]*?>),<$1$3$4,sig)
Branch: 1 branch
BranchTrueFalseTotal
if ($$h =~ s/<([A-z]+ [A-z]+=)(['"])([A-z0-9]+)\2(\s*?[^>]*?>)/<$1$3$4/sig) 6 11 17
491 {
492 # Remove alphanumeric quotes. Note, breaks DTD..
493 ;
494 }
495 }
496 # remove <b></b>, etc..
497 11 ▶ if ($do_emptytags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Clean::do_emptytags) 11 0 11
498 11 my $pat = $do_emptytags;
499 11 $pat =~ s/\s+/|/g;
500  
501 11 ▶ while ($$h =~ s,<($pat)(\s+[^>]*?)?>\s*</\1>,,siog){}
Branch: 1 branch
BranchTrueFalseTotal
if ($$h =~ s[<($pat)(\s+[^>]*?)?>\s*</\1>][]gios) 3 11 14
502  
503 }
504 11 ▶ if ($do_htmldefaults) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Clean::do_htmldefaults) 11 0 11
505 # Tables
506 # seems to break things..
507 #$$h =~ s,(<table[^>]*)\s+border=0([^>]*>),$1$2,sig;
508 11 $$h =~ s,(<td[^>]*)\s+rowspan=1([^>]*>),$1$2,sig;
509 11 $$h =~ s,(<td[^>]*)\s+colspan=1([^>]*>),$1$2,sig;
510  
511 #
512  
513 # P, TABLE tags are default left aligned..
514 # lynx is inconsistent in this manner though..
515  
516 11 $$h =~ s,<(P|table|td)( [^>]*)align=\"?left\"?([^>]*)>,<$1$2$3>,sig;
517  
518 # OL start=1
519 11 $$h =~ s,(<OL [^>]*)start=\"?1\"?([^>]*>),$1$2,sig;
520  
521 # FORM
522 11 $$h =~ s,(<form [^>]*)method=\"?get\"?([^>]*>),$1$2,sig;
523 11 $$h =~ s,(<form [^>]*)enctype=\"application/x-www-form-urlencoded\"([^>]*>),$1$2,sig;
524  
525 # hr
526 11 $$h =~ s,(<hr [^>]*)align=\"?center\"?([^>]*>),$1$2,sig;
527 11 $$h =~ s,(<hr [^>]*)width=\"?100%\"?([^>]*>),$1$2,sig;
528  
529 # URLs
530 11 $$h =~ s,(href|src)(=\"?http://[^/:]+):80/,$1$2/,sig;
531 }
532  
533 11 ▶ if ($do_whitespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Clean::do_whitespace) 11 0 11
534 # remove space within tags <center > becomes <center>
535 11 $$h =~ s,\s+>,>,sg;
536 11 $$h =~ s,<\s+,<,sg;
537 # join lines with a space at the beginning/end of the line
538 # and a line that begins with a tag
539 11 $$h =~ s,>\n ,> ,sig;
540 11 $$h =~ s, \n<, <,sig;
541 }
542  
543 11 ▶ if ($do_lowercasetags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Clean::do_lowercasetags) 11 0 11
544 # translate tags to lowercase to (hopefully) improve compressability..
545  
546 # simple tags <H1>, </H1> etc.
547 11 $$h =~ s,(<[/]?[a-zA-Z][a-zA-Z0-9_-]*\s*>),\L$1\E,sg;
548  
549 # the rest..
550 11 $$h =~ s/(<[a-zA-Z][a-zA-Z0-9_-]*)(\s+.*?>)/_lowercasetag($1,$2)/sge;
551 }
552 }
553  
554 sub _lowercasetag {
555 2101 ▶ my($prefix, $body) = @_;
Subroutine _lowercasetag: called CC=1 SCAR=0.0
556 2101 $prefix =~ s/^(.+)$/\L$1\E/;
557 2101 $body =~ s/(\s+[a-zA-Z][a-zA-Z0-9_-]*)(\s*=\s*[^"\s]+|\s*=\s*"[^"]*"|>|\s)/\L$1\E$2/sg;
558 2101 return $prefix.$body;
559 }
560  
561 # set options based on the level provided.. INTERNAL
562  
563 sub _level_defaults($) {
564 11 ▶ my ($level) = @_;
Subroutine _level_defaults: called CC=13 SCAR=29.1
565  
566 11 $do_whitespace = 1; # always do this...
567  
568 # level 2
569 11 ▶ $do_shortertags = ($level > 1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$level > 1 ? : 11 0 11
570 11 ▶ $do_meta = ($level > 1) ? "generator formatter" : "";
Branch: 1 branch
BranchTrueFalseTotal
$level > 1 ? : 11 0 11
571 11 ▶ $do_contenttype = ($level > 1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$level > 1 ? : 11 0 11
572  
573 # level 3
574 11 ▶ $do_entities = ($level > 2) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$level > 2 ? : 11 0 11
575 11 ▶ $do_blink = ($level > 2) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$level > 2 ? : 11 0 11
576  
577 # level 4
578 11 ▶ $do_comments = ($level > 3) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$level > 3 ? : 11 0 11
579 11 ▶ $do_dequote = ($level > 3) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$level > 3 ? : 11 0 11
580 11 ▶ $do_defcolor = ($level > 3) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$level > 3 ? : 11 0 11
581 11 ▶ $do_emptytags = ($level > 3) ? 'b i font center' : 0;
Branch: 1 branch
BranchTrueFalseTotal
$level > 3 ? : 11 0 11
582 11 ▶ $do_javascript = ($level > 3) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$level > 3 ? : 11 0 11
583 11 ▶ $do_htmldefaults = ($level > 3) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$level > 3 ? : 11 0 11
584 11 ▶ $do_lowercasetags = ($level > 3) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$level > 3 ? : 11 0 11
585  
586 # higher levels reserved for more intensive optimizations.
587 }
588  
589 ######################################################################
590  
591 =head2 compat()
592  
593 This function improves the cross-platform compatibility of your HTML.
594 Currently checks for the following problems:
595  
596 =over 8
597  
598 =item Insuring all IMG tags have ALT elements.
599  
600 =item Use of Arial, Futura, or Verdana as a font face.
601  
602 =item Positioning the <TITLE> tag immediately after the <head> tag.
603  
604 =back
605  
606 =cut
607  
608 sub compat {
609 3 ▶ my($self, $level, $options) = @_;
Subroutine compat: called CC=2 SCAR=7.8
Pod documented
610  
611 3 my $h = $self->{'DATA'};
612  
613 3 $$h =~ s/face="arial"/face="arial,helvetica,sansserif"/sgi;
614 3 $$h =~ s/face="(verdana|futura)"/face="$1,arial,helvetica,sansserif"/sgi;
615  
616 # insure that <title> tag is directly after the <head> tag
617 # Some search engines only search the first N chars. (PLweb for instance..)
618  
619 3 ▶ if ($$h =~ s,<title>(.*)</title>,,si) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$h =~ s[<title>(.*)</title>][]is) 0 3 3
620 0 my $title = $1;
621 0 $$h =~ s,<head>,<head><title>$title</title>,si;
622 }
623  
624 # Look for IMG without ALT tags.
625 3 $$h =~ s/(<img[^>]+>)/_imgalt($1)/segi;
626 }
627  
628 sub _imgalt {
629 0 ▶ my($tag) = @_;
Subroutine _imgalt: not called CC=2 SCAR=17.9
630  
631 0 ▶ $tag =~ s/>/ alt="">/ if ($tag !~ /alt=/i);
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ /alt=/i 0 0 0
632 0 return($tag);
633 }
634  
635 =head2 defrontpage();
636  
637 This function converts pages created with Microsoft Frontpage to
638 something a Unix server will understand a bit better. This function
639 currently does the following:
640  
641 =over 8
642  
643 =item Converts Frontpage 'hit counters' into a unix specific format.
644  
645 =item Removes some frontpage specific html comments
646  
647 =back
648  
649 =cut
650  
651  
652 sub defrontpage {
653 0 ▶ my($self) = @_;
Subroutine defrontpage: not called CC=2 SCAR=17.9
Pod documented
654  
655 0 my $h = $self->{'DATA'};
656  
657 0 ▶ while ($$h =~ s,<img\sSRC="[\./]*_vti_bin/fpcount.exe(/.*/).Page=(.*?)\|.*?\s(.*?)>,<img src="/counter?link=$1$2" $3>,xis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$h =~ s[<img\sSRC="[\./]*_vti_bin/fpcount.exe(/.*/).Page=(.*?)\|.*?\s(.*?)>][<img src="/counter?link=$1$2" $3>]six) 0 0 0
658 0 print "Converted a Hitcounter.. $1, $2, $3\n";
659 }
660 0 $$h =~ s,<!--(mstheme|msthemeseparator|msnavigation)-->,,sgx;
661 }
662  
663  
664 =head1 SEE ALSO
665  
666 =head2 Modules
667  
668 FrontPage::Web, FrontPage::File
669  
670 =head2 Web Sites
671  
672 =over 6
673  
674 =item Distribution Site - http://people.itu.int/~lindner/
675  
676 =back
677  
678 =head1 AUTHORS
679  
680 Paul Lindner for the International Telecommunication Union (ITU)
681  
682 =head1 COPYRIGHT
683  
684 The HTML::Strip module is Copyright (c) 1998,99 by the ITU, Geneva Switzerland.
685 All rights reserved.
686  
687 You may distribute under the terms of either the GNU General Public
688 License or the Artistic License, as specified in the Perl README file.
689  
690 =cut
691  
692 2 1;
693 __END__
↑ Summary