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