| 1 | # -*- mode: Perl; -*- | ||
| 2 | package NewsClipper::HTMLTools; | ||
| 3 | |||
| 4 | # This package contains a set of useful functions for manipulating HTML. | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | # Used to make relative URLs absolute | ||
| 8 | use URI; | ||
| 9 | # For exporting of functions | ||
| 10 | use Exporter; | ||
| 11 | |||
| 12 | use vars qw( @ISA @EXPORT $VERSION ); | ||
| 13 | |||
| 14 | @ISA = qw( Exporter ); | ||
| 15 | @EXPORT = qw( ExtractText MakeLinksAbsolute StripTags | ||
| 16 | EscapeHTMLChars StripAttributes HTMLsubstr | ||
| 17 | TrimOpenTags GetAttributeValue ExtractTables ); | ||
| 18 | |||
| 19 | $VERSION = 0.72; | ||
| 20 | |||
| 21 | use NewsClipper::Globals; | ||
| 22 | |||
| 23 | # ------------------------------------------------------------------------------ | ||
| 24 | |||
| 25 | # Takes a bunch of html in the first argument (as one long string or a ref to | ||
| 26 | # one). Uses the remaining arguments to strip out attributes. By default, | ||
| 27 | # these are taken to be alt,class. Takes one long string (or a reference to | ||
| 28 | # one) and returns the same. | ||
| 29 | |||
| 30 | sub StripAttributes($@) | ||
| 31 | { | ||
| 32 | my $html = shift; | ||
| 33 | my @tags = @_; | ||
| 34 | |||
| 35 | return undef unless defined $html; | ||
| 36 | |||
| 37 | unless (UNIVERSAL::isa(\$html,'SCALAR') || UNIVERSAL::isa($html,'SCALAR')) | ||
| 38 | { | ||
| 39 | die reformat dequote<<" EOF"; | ||
| 40 | StripAttributes only takes values of type "SCALAR". | ||
| 41 | EOF | ||
| 42 | } | ||
| 43 | |||
| 44 | my $returnRef = 0; | ||
| 45 | |||
| 46 | if (ref $html) | ||
| 47 | { | ||
| 48 | $returnRef = ref $html; | ||
| 49 | $html = $$html; | ||
| 50 | } | ||
| 51 | |||
| 52 | @tags = qw(alt class) | ||
| 53 | if $#tags == -1; | ||
| 54 | |||
| 55 | # Make the pattern from @tags | ||
| 56 | local $" = '|'; | ||
| 57 | my $pattern = "@tags"; | ||
| 58 | |||
| 59 | my $oldhtml; | ||
| 60 | do | ||
| 61 | { | ||
| 62 | $oldhtml = $html; | ||
| 63 | |||
| 64 | # Strip out anything that matches the pattern inside a tag with ' quotes | ||
| 65 | $html =~ s#(<[^>]+?)\s*\b($pattern)\b\s*=\s*'[^']*'([^>]*>)#$1$3#sig; | ||
| 66 | |||
| 67 | # Strip out anything that matches the pattern inside a tag with " quotes | ||
| 68 | $html =~ s#(<[^>]+?)\s*\b($pattern)\b\s*=\s*"[^"]*"([^>]*>)#$1$3#sig; | ||
| 69 | |||
| 70 | # Strip out anything that matches the pattern inside a tag without quotes | ||
| 71 | $html =~ s#(<[^>]+?)\s*\b($pattern)\b\s*=\s*[^'"]\S*[^'"]([^>]*>)#$1$3#sig; | ||
| 72 | } while ($oldhtml ne $html); | ||
| 73 | |||
| 74 | return bless (\$html,$returnRef) if $returnRef; | ||
| 75 | return $html; | ||
| 76 | } | ||
| 77 | |||
| 78 | # ------------------------------------------------------------------------------ | ||
| 79 | |||
| 80 | # Takes a bunch of html in the first argument (as one long string or a ref to | ||
| 81 | # one). Uses the remaining arguments to strip out tags. By default, these are | ||
| 82 | # taken to be strong,h1,h2,h3,h4,h5,h6,b,i,u,tt,font,big,small,strike, which | ||
| 83 | # (hopefully) strips out the formatting. | ||
| 84 | |||
| 85 | sub StripTags($@) | ||
| 86 | { | ||
| 87 | my $html = shift; | ||
| 88 | my @tags = @_; | ||
| 89 | |||
| 90 | return undef unless defined $html; | ||
| 91 | |||
| 92 | unless (UNIVERSAL::isa(\$html,'SCALAR') || UNIVERSAL::isa($html,'SCALAR')) | ||
| 93 | { | ||
| 94 | die reformat dequote<<" EOF"; | ||
| 95 | StripTags only takes values of type "SCALAR". | ||
| 96 | EOF | ||
| 97 | } | ||
| 98 | |||
| 99 | my $returnRef = 0; | ||
| 100 | |||
| 101 | if (ref $html) | ||
| 102 | { | ||
| 103 | $returnRef = ref $html; | ||
| 104 | $html = $$html; | ||
| 105 | } | ||
| 106 | |||
| 107 | @tags = qw(strong em h1 h2 h3 h4 h5 h6 b i u tt font big small strike) | ||
| 108 | if $#tags == -1; | ||
| 109 | |||
| 110 | # Make the pattern from @tags | ||
| 111 | my $temp = $"; | ||
| 112 | $" = '|'; | ||
| 113 | my $pattern = "@tags"; | ||
| 114 | $" = $temp; | ||
| 115 | |||
| 116 | # Strip out anything that matches the pattern | ||
| 117 | $html =~ s#<\s*/?\b($pattern)\b[^>]*>##sig; | ||
| 118 | |||
| 119 | return bless (\$html,$returnRef) if $returnRef; | ||
| 120 | return $html; | ||
| 121 | } | ||
| 122 | |||
| 123 | # ------------------------------------------------------------------------------ | ||
| 124 | |||
| 125 | # Extracts all text between the starting and ending patterns. '^' and '$' can | ||
| 126 | # be used for the starting and ending patterns to signify start of text and | ||
| 127 | # end of text. Takes one long string (or a reference to one) and returns the | ||
| 128 | # same. | ||
| 129 | |||
| 130 | sub ExtractText($$$) | ||
| 131 | { | ||
| 132 | my $html = shift; | ||
| 133 | my $startPattern = shift; | ||
| 134 | my $endPattern = shift; | ||
| 135 | |||
| 136 | return undef | ||
| 137 | unless defined $html && defined $startPattern && defined $endPattern; | ||
| 138 | |||
| 139 | unless (UNIVERSAL::isa(\$html,'SCALAR') || UNIVERSAL::isa($html,'SCALAR')) | ||
| 140 | { | ||
| 141 | die reformat dequote<<" EOF"; | ||
| 142 | ExtractText only takes values of type "SCALAR". | ||
| 143 | EOF | ||
| 144 | } | ||
| 145 | |||
| 146 | my $returnRef = 0; | ||
| 147 | |||
| 148 | if (ref $html) | ||
| 149 | { | ||
| 150 | $returnRef = ref $html; | ||
| 151 | $html = $$html; | ||
| 152 | } | ||
| 153 | |||
| 154 | # Makes things a lot faster | ||
| 155 | return if $startPattern ne '^' && $html !~ /$startPattern/s; | ||
| 156 | |||
| 157 | if (($startPattern eq '^') && ($endPattern eq '$')) | ||
| 158 | { | ||
| 159 | return $html; | ||
| 160 | } | ||
| 161 | if (($startPattern ne '^') && ($endPattern ne '$')) | ||
| 162 | { | ||
| 163 | return '' unless $html =~ s/.*?$startPattern//s; | ||
| 164 | return '' unless $html =~ s/$endPattern.*//s; | ||
| 165 | return $html; | ||
| 166 | } | ||
| 167 | if (($startPattern ne '^') && ($endPattern eq '$')) | ||
| 168 | { | ||
| 169 | $html =~ s/.*?$startPattern(.*)/$1/s; | ||
| 170 | return '' if $1 eq ''; | ||
| 171 | return $html; | ||
| 172 | } | ||
| 173 | if (($startPattern eq '^') && ($endPattern ne '$')) | ||
| 174 | { | ||
| 175 | $html =~ s/(.*?)$endPattern.*/$1/s; | ||
| 176 | return '' if $1 eq ''; | ||
| 177 | return $html; | ||
| 178 | } | ||
| 179 | |||
| 180 | return bless (\$html,$returnRef) if $returnRef; | ||
| 181 | return $html; | ||
| 182 | } | ||
| 183 | |||
| 184 | # ------------------------------------------------------------------------------ | ||
| 185 | |||
| 186 | # Takes a bunch of html in the first argument (as one long string or a ref to | ||
| 187 | # one). Uses the remaining arguments to trim unclosed tags from the beginning | ||
| 188 | # and end of the html. By default, these are taken to be every possible | ||
| 189 | # enclosing-style tag. | ||
| 190 | |||
| 191 | sub TrimOpenTags($@) | ||
| 192 | { | ||
| 193 | my $html = shift; | ||
| 194 | my @tags = @_; | ||
| 195 | |||
| 196 | return undef unless defined $html; | ||
| 197 | |||
| 198 | unless (UNIVERSAL::isa(\$html,'SCALAR') || UNIVERSAL::isa($html,'SCALAR')) | ||
| 199 | { | ||
| 200 | die reformat dequote<<" EOF"; | ||
| 201 | TrimOpenTags only takes values of type "SCALAR". | ||
| 202 | EOF | ||
| 203 | } | ||
| 204 | |||
| 205 | my $returnRef = 0; | ||
| 206 | |||
| 207 | if (ref $html) | ||
| 208 | { | ||
| 209 | $returnRef = ref $html; | ||
| 210 | $html = $$html; | ||
| 211 | } | ||
| 212 | |||
| 213 | @tags = qw(strong em h1 h2 h3 h4 h5 h6 b i u tt font big small strike a html | ||
| 214 | title head body div span blockquote q code samp kbd var dfn | ||
| 215 | address ins del acronym abbr s sub sup pre center blink marquee | ||
| 216 | multicol layer ilayer nolayer map object nobr ul ol dl menu form | ||
| 217 | button label select optgroup textarea fieldset legend table tr | ||
| 218 | td th tfoot thead caption col colgroup frameset noframes iframe | ||
| 219 | script noscript applet server style bdo) | ||
| 220 | if $#tags == -1; | ||
| 221 | |||
| 222 | foreach my $tag (@tags) | ||
| 223 | { | ||
| 224 | # If we see a starting tag... | ||
| 225 | if ($html =~ /^(.*)(<\s*\b$tag\b[^>]*>)(.*?)$/si) | ||
| 226 | { | ||
| 227 | my ($start,$tagtext,$end) = ($1,$2,$3); | ||
| 228 | # If there isn't a closing tag... | ||
| 229 | if ($end !~ /<\s*\/\s*\b$tag\b[^>]*>/si) | ||
| 230 | { | ||
| 231 | $html = $start.$end; | ||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | # If we see an ending tag... | ||
| 236 | if ($html =~ /^(.*?)(<\s*\/\s*\b$tag\b[^>]*>)(.*)$/si) | ||
| 237 | { | ||
| 238 | my ($start,$tagtext,$end) = ($1,$2,$3); | ||
| 239 | # If there isn't a starting tag... | ||
| 240 | if ($start !~ /<\s*\b$tag\b[^>]*>/si) | ||
| 241 | { | ||
| 242 | $html = $start.$end; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | } | ||
| 246 | |||
| 247 | return bless (\$html,$returnRef) if $returnRef; | ||
| 248 | return $html; | ||
| 249 | } | ||
| 250 | |||
| 251 | # ------------------------------------------------------------------------------ | ||
| 252 | |||
| 253 | # Takes a substring from HTML, but only counts the non-tag characters. It also | ||
| 254 | # tries to remove starting HTML tags that have been trimmed off... Arguments | ||
| 255 | # are the text, an offset, and the length. Takes one long string (or a | ||
| 256 | # reference to one) and returns the same. | ||
| 257 | |||
| 258 | sub HTMLsubstr($$;$) | ||
| 259 | { | ||
| 260 | my $html = shift; | ||
| 261 | my $offset = shift; | ||
| 262 | my $length = shift || 32700; | ||
| 263 | |||
| 264 | return undef unless defined $html; | ||
| 265 | |||
| 266 | unless (UNIVERSAL::isa(\$html,'SCALAR') || UNIVERSAL::isa($html,'SCALAR')) | ||
| 267 | { | ||
| 268 | die reformat dequote<<" EOF"; | ||
| 269 | HTMLsubstr only takes values of type "SCALAR". | ||
| 270 | EOF | ||
| 271 | } | ||
| 272 | |||
| 273 | my $returnRef = 0; | ||
| 274 | |||
| 275 | if (ref $html) | ||
| 276 | { | ||
| 277 | $returnRef = ref $html; | ||
| 278 | $html = $$html; | ||
| 279 | } | ||
| 280 | |||
| 281 | # First split the html into characters | ||
| 282 | my @chars = $html =~ /(.)/sg; | ||
| 283 | |||
| 284 | my $returnVal = ''; | ||
| 285 | |||
| 286 | my $count = 0; | ||
| 287 | my $counting = 1; | ||
| 288 | |||
| 289 | foreach my $char (@chars) | ||
| 290 | { | ||
| 291 | # Stop counting if we see a < | ||
| 292 | if ($char eq '<') | ||
| 293 | { | ||
| 294 | $counting = 0; | ||
| 295 | } | ||
| 296 | |||
| 297 | # Start counting when we see a >, but don't count the > | ||
| 298 | if ($char eq '>') | ||
| 299 | { | ||
| 300 | $counting = 1; | ||
| 301 | $count-- unless $count == 0; | ||
| 302 | } | ||
| 303 | |||
| 304 | last if $count > $offset + $length; | ||
| 305 | |||
| 306 | $returnVal .= $char if $count >= $offset; | ||
| 307 | |||
| 308 | $count++ if $counting; | ||
| 309 | } | ||
| 310 | |||
| 311 | unless (defined $returnVal) | ||
| 312 | { | ||
| 313 | warn "News Clipper encountered an error taking an HTML stubstring.\n". | ||
| 314 | "Please submit a bug report at http://newsclipper.sourceforge.net/\n"; | ||
| 315 | return ''; | ||
| 316 | } | ||
| 317 | |||
| 318 | # But wait! What if we chopped off a starting <font>, <tt> etc from the | ||
| 319 | # beginning, or an ending </font>, </tt>, etc from the end? | ||
| 320 | $returnVal = TrimOpenTags($returnVal); | ||
| 321 | |||
| 322 | return bless (\$returnVal,$returnRef) if $returnRef; | ||
| 323 | return $returnVal; | ||
| 324 | } | ||
| 325 | |||
| 326 | # ------------------------------------------------------------------------------ | ||
| 327 | |||
| 328 | # Escapes & < and > in text. Note that this should only be used on non-HTML | ||
| 329 | # text. ('AT&T' gets turned into 'AT&T') Takes one long string (or a | ||
| 330 | # reference to one) and returns the same. | ||
| 331 | |||
| 332 | sub EscapeHTMLChars($) | ||
| 333 | { | ||
| 334 | my $html = shift; | ||
| 335 | |||
| 336 | return undef unless defined $html; | ||
| 337 | |||
| 338 | unless (UNIVERSAL::isa(\$html,'SCALAR') || UNIVERSAL::isa($html,'SCALAR')) | ||
| 339 | { | ||
| 340 | die reformat dequote<<" EOF"; | ||
| 341 | EscapeHTMLChars only takes values of type "SCALAR". | ||
| 342 | EOF | ||
| 343 | } | ||
| 344 | |||
| 345 | my $returnRef = 0; | ||
| 346 | |||
| 347 | if (ref $html) | ||
| 348 | { | ||
| 349 | $returnRef = ref $html; | ||
| 350 | $html = $$html; | ||
| 351 | } | ||
| 352 | |||
| 353 | # Escape HTML characters | ||
| 354 | $html =~ s/&/&/sg; | ||
| 355 | $html =~ s/</</sg; | ||
| 356 | $html =~ s/>/>/sg; | ||
| 357 | |||
| 358 | return bless (\$html,$returnRef) if $returnRef; | ||
| 359 | return $html; | ||
| 360 | } | ||
| 361 | |||
| 362 | # ------------------------------------------------------------------------------ | ||
| 363 | |||
| 364 | # Searches text for "a href" or "img src" tags and makes them absolute. We | ||
| 365 | # should probably be doing this with HTML::Parser. Note that this function | ||
| 366 | # erroneously recognizes stuff like <a href='XXX">. This is pretty rare, and | ||
| 367 | # doing it this way is faster than recognizing two quotes and no quotes | ||
| 368 | # separately. Takes one long string (or a reference to one) and returns the | ||
| 369 | # same. | ||
| 370 | |||
| 371 | sub MakeLinksAbsolute($$) | ||
| 372 | { | ||
| 373 | my $url = shift; | ||
| 374 | my $html = shift; | ||
| 375 | |||
| 376 | return undef unless defined $html; | ||
| 377 | |||
| 378 | unless (UNIVERSAL::isa(\$html,'SCALAR') || UNIVERSAL::isa($html,'SCALAR')) | ||
| 379 | { | ||
| 380 | die reformat dequote<<" EOF"; | ||
| 381 | MakeLinksAbsolute only takes values of type "SCALAR". | ||
| 382 | EOF | ||
| 383 | } | ||
| 384 | |||
| 385 | my $returnRef = 0; | ||
| 386 | |||
| 387 | if (ref $html) | ||
| 388 | { | ||
| 389 | $returnRef = ref $html; | ||
| 390 | $html = $$html; | ||
| 391 | } | ||
| 392 | |||
| 393 | $html =~ s/ | ||
| 394 | # First look for an a, img, or area, followed later by an href or src | ||
| 395 | (<\s*(?:a|img|area)\b[^>]*(?:href|src)\s*=\s* | ||
| 396 | # Then an optional quote | ||
| 397 | ['"]?) | ||
| 398 | # Then the interesting part | ||
| 399 | ([^'"> ]+) | ||
| 400 | # Then another optional quote | ||
| 401 | (['"]? | ||
| 402 | # And the left-overs | ||
| 403 | [^>]*>) | ||
| 404 | / | ||
| 405 | # Then construct the new link from the prefix and suffix | ||
| 406 | $1.sprintf("%s",URI->new($2)->abs($url)).$3 | ||
| 407 | /segix; | ||
| 408 | |||
| 409 | return bless (\$html,$returnRef) if $returnRef; | ||
| 410 | return $html; | ||
| 411 | } | ||
| 412 | |||
| 413 | # ------------------------------------------------------------------------------ | ||
| 414 | |||
| 415 | # Searches HTML for a tag and an attribute, and returns the value of the | ||
| 416 | # attribute for the first tag encountered. Returns undef if the value can't be | ||
| 417 | # found. | ||
| 418 | |||
| 419 | sub GetAttributeValue($$$) | ||
| 420 | { | ||
| 421 | my $html = shift; | ||
| 422 | my $tag = shift; | ||
| 423 | my $attribute = shift; | ||
| 424 | |||
| 425 | return undef | ||
| 426 | unless defined $html && defined $tag && defined $attribute; | ||
| 427 | |||
| 428 | unless (UNIVERSAL::isa(\$html,'SCALAR') || UNIVERSAL::isa($html,'SCALAR')) | ||
| 429 | { | ||
| 430 | die reformat dequote<<" EOF"; | ||
| 431 | GetAttributeValue only takes values of type "SCALAR". | ||
| 432 | EOF | ||
| 433 | } | ||
| 434 | |||
| 435 | $html = $$html if ref $html; | ||
| 436 | |||
| 437 | if ($html =~ / | ||
| 438 | # First look for a <$tag, followed later by an $attribute | ||
| 439 | <\s*(?:$tag)\b[^>]*\b(?:$attribute)\b\s*=\s* | ||
| 440 | # Then an optional quote | ||
| 441 | ['"]? | ||
| 442 | # Then the interesting part | ||
| 443 | ([^'"> ]+) | ||
| 444 | # Then another optional quote | ||
| 445 | ['"]? | ||
| 446 | # And the left-overs | ||
| 447 | [^>]*> | ||
| 448 | /six) | ||
| 449 | { | ||
| 450 | return $1; | ||
| 451 | } | ||
| 452 | else | ||
| 453 | { | ||
| 454 | return undef; | ||
| 455 | } | ||
| 456 | } | ||
| 457 | |||
| 458 | # ------------------------------------------------------------------------------ | ||
| 459 | |||
| 460 | # Extracts all tables. Nested tables are not extracted separately--they are | ||
| 461 | # treated as part of the enclosing table. Takes one long string (or a | ||
| 462 | # reference to one) and returns an array of strings (or references to strings). | ||
| 463 | |||
| 464 | sub ExtractTables($) | ||
| 465 | { | ||
| 466 | my $html = shift; | ||
| 467 | |||
| 468 | return undef unless defined $html; | ||
| 469 | |||
| 470 | unless (UNIVERSAL::isa(\$html,'SCALAR') || UNIVERSAL::isa($html,'SCALAR')) | ||
| 471 | { | ||
| 472 | die reformat dequote<<" EOF"; | ||
| 473 | ExtractTables only takes values of type "SCALAR". | ||
| 474 | EOF | ||
| 475 | } | ||
| 476 | |||
| 477 | my $returnRef = 0; | ||
| 478 | |||
| 479 | if (ref $html) | ||
| 480 | { | ||
| 481 | $returnRef = ref $html; | ||
| 482 | $html = $$html; | ||
| 483 | } | ||
| 484 | |||
| 485 | my @tables; | ||
| 486 | my $depth=0; | ||
| 487 | my $newtable; | ||
| 488 | |||
| 489 | while ($html =~ /\G(.*?)(<\s*table\b[^>]*>|<\s*\/\s*table\s*>)/isg) | ||
| 490 | { | ||
| 491 | my ($prefix,$match) = ($1,$2); | ||
| 492 | |||
| 493 | # Start of table | ||
| 494 | if ($match =~ /<\s*table/i) | ||
| 495 | { | ||
| 496 | $depth++; | ||
| 497 | |||
| 498 | # Brand new table | ||
| 499 | if ($depth == 1) | ||
| 500 | { | ||
| 501 | $newtable = $match; | ||
| 502 | } | ||
| 503 | # Internal table | ||
| 504 | else | ||
| 505 | { | ||
| 506 | $newtable .= "$prefix$match"; | ||
| 507 | } | ||
| 508 | } | ||
| 509 | # End of table | ||
| 510 | else | ||
| 511 | { | ||
| 512 | # We might see an out-of-place </table> | ||
| 513 | next if $depth == 0; | ||
| 514 | |||
| 515 | $depth--; | ||
| 516 | $newtable .= "$prefix$match"; | ||
| 517 | |||
| 518 | # Done with table | ||
| 519 | if ($depth == 0) | ||
| 520 | { | ||
| 521 | if ($returnRef) | ||
| 522 | { | ||
| 523 | push (@tables, \$newtable); | ||
| 524 | } | ||
| 525 | else | ||
| 526 | { | ||
| 527 | push (@tables, $newtable); | ||
| 528 | } | ||
| 529 | } | ||
| 530 | # Internal table | ||
| 531 | else | ||
| 532 | { | ||
| 533 | $newtable .= "$prefix$match"; | ||
| 534 | } | ||
| 535 | } | ||
| 536 | } | ||
| 537 | |||
| 538 | return @tables; | ||
| 539 | } | ||
| 540 | |||
| 541 | 1; |