| 1 | package Markup::Tree; | ||
| 2 | $VERSION = '1.2.2'; | ||
| 3 | |||
| 4 | #################################################### | ||
| 5 | # This module is protected under the terms of the | ||
| 6 | # GNU GPL. Please see | ||
| 7 | # http://www.opensource.org/licenses/gpl-license.php | ||
| 8 | # for more information. | ||
| 9 | #################################################### | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use XML::Parser; | ||
| 13 | use HTML::TreeBuilder; | ||
| 14 | use Markup::TreeNode; | ||
| 15 | use File::Temp qw/ :POSIX /; | ||
| 16 | use LWP::Simple; | ||
| 17 | |||
| 18 | require Exporter; | ||
| 19 | require Carp; | ||
| 20 | |||
| 21 | our @ISA = qw(Exporter); | ||
| 22 | |||
| 23 | sub new { | ||
| 24 | my $invocant = shift(); | ||
| 25 | my $class = ref($invocant) || $invocant; | ||
| 26 | $class = bless { | ||
| 27 | _parser => '', | ||
| 28 | _globals => {}, | ||
| 29 | _tree => Markup::TreeNode->new( element_type => '-->root', tagname => '-->root', level => 0 ), | ||
| 30 | markup => 'html', | ||
| 31 | parser_options => undef, | ||
| 32 | no_squash_whitespace => 0, | ||
| 33 | no_indent => 0 | ||
| 34 | }, $class; | ||
| 35 | $class->init(@_); | ||
| 36 | return $class; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub init { | ||
| 40 | my $self = shift(); | ||
| 41 | my %arg = @_; | ||
| 42 | |||
| 43 | foreach (keys %arg) { | ||
| 44 | if (exists $self->{$_}) { | ||
| 45 | $self->{$_} = $arg{$_}; | ||
| 46 | } | ||
| 47 | else { | ||
| 48 | Carp::croak ("unrecognized option $_"); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | if ($self->{'markup'} eq 'xml') { | ||
| 53 | $self->{'_parser'} = XML::Parser->new( %{ $self->{'parser_options'} } ); | ||
| 54 | } | ||
| 55 | else { | ||
| 56 | if ((!exists $self->{'parser_options'}->{'api_version'}) || | ||
| 57 | ($self->{'parser_options'}->{'api_version'} != 3)) | ||
| 58 | { | ||
| 59 | $self->{'parser_options'}->{'api_version'} = 3; | ||
| 60 | } | ||
| 61 | |||
| 62 | for ($self->{'_parser'}) { | ||
| 63 | $_ = HTML::TreeBuilder->new( %{ $self->{'parser_options'} } ); | ||
| 64 | $_->ignore_unknown(0); | ||
| 65 | $_->no_space_compacting(1); | ||
| 66 | $_->store_comments(1); | ||
| 67 | $_->store_declarations(1); | ||
| 68 | $_->store_pis(1); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | $self->_init_hookups () if ($self->{'markup'} eq 'xml'); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _work_around_pis { | ||
| 76 | my $fh = shift(); | ||
| 77 | my $data; | ||
| 78 | my $fix = sub { | ||
| 79 | my ($style, $text) = @_; | ||
| 80 | $text =~ s/"/%QUOTE%/g; | ||
| 81 | return "\"{pi:language=$style:$text}\""; | ||
| 82 | }; | ||
| 83 | $data = join '', <$fh>; | ||
| 84 | |||
| 85 | # within ""s? | ||
| 86 | $data =~ s/"<%(.+?)%>"/$fix->('asp-style', $1)/seg; | ||
| 87 | $data =~ s/"<\?(?:php(?:\d)?)?(.+?)\?>"/$fix->('asp-style', $1)/seg; | ||
| 88 | |||
| 89 | $data =~ s/<%(.+?)%>/<pi language = "asp-style">$1<\/pi>/sg; | ||
| 90 | $data =~ s/<\?(?:php(?:\d)?)?(.+?)\?>/<pi language = "php-style">$1<\/pi>/sg; | ||
| 91 | |||
| 92 | seek $fh, 0, 0; | ||
| 93 | |||
| 94 | return $data; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub parse_file { | ||
| 98 | my ($self, $file) = @_; | ||
| 99 | |||
| 100 | $file = _mk_filehandle($file); | ||
| 101 | |||
| 102 | if ($self->{'markup'} eq 'xml') { | ||
| 103 | $self->parse($file); | ||
| 104 | } | ||
| 105 | else { | ||
| 106 | $self->parse (_work_around_pis($file)); | ||
| 107 | $self->eof(); | ||
| 108 | } | ||
| 109 | |||
| 110 | close ($file); | ||
| 111 | return $self; # so you can say Markup::Tree->new()->parse_file('foo.html'); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub parse { | ||
| 115 | $_ = shift(); | ||
| 116 | $_->{'_parser'}->parse (shift()); | ||
| 117 | return $_; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub eof { | ||
| 121 | my $self = shift(); | ||
| 122 | if ($self->{'markup'} ne 'xml') { | ||
| 123 | $self->{'_parser'}->eof (); | ||
| 124 | $self->{'_parser'}->elementify(); | ||
| 125 | $self->_init_hookups(); | ||
| 126 | } | ||
| 127 | return $self; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub _init_hookups { | ||
| 131 | my $self = shift(); | ||
| 132 | |||
| 133 | $self->{'_globals'}->{'level'} = 0; | ||
| 134 | $self->{'_globals'}->{'last_node'} = $self->{'_tree'}; | ||
| 135 | |||
| 136 | if ($self->{'markup'} eq 'xml') { | ||
| 137 | $self->_init_xml_hookups (); | ||
| 138 | } | ||
| 139 | else { | ||
| 140 | $self->_init_html_hookups (); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | # TODO: bring anything before html up | ||
| 145 | sub _init_html_hookups { | ||
| 146 | my $self = shift(); | ||
| 147 | my $build_tree; | ||
| 148 | |||
| 149 | $build_tree = sub { | ||
| 150 | my $tag = shift(); | ||
| 151 | my $node; | ||
| 152 | my %options = ( level => $self->{'_globals'}->{'level'} ); | ||
| 153 | my $parent = $self->{'_globals'}->{'last_node'}; | ||
| 154 | my %mapping = ( '~text' => '-->text', | ||
| 155 | '~comment' => '-->comment', | ||
| 156 | '~declaration' => '-->declaration', | ||
| 157 | '~pi' => '-->pi' ); # unlikely | ||
| 158 | |||
| 159 | @_ = $tag->all_external_attr(); | ||
| 160 | while (@_) { $options{'attr'}->{pop(@_)} = pop(@_); } | ||
| 161 | delete $options{'attr'}->{'/'}; | ||
| 162 | $tag->objectify_text(); | ||
| 163 | |||
| 164 | if (exists $mapping{$tag->tag()}) { | ||
| 165 | $options{'element_type'} = $mapping{$tag->tag()}; | ||
| 166 | $options{'tagname'} = $options{'element_type'}; | ||
| 167 | } | ||
| 168 | else { | ||
| 169 | $options{'element_type'} = 'tag'; | ||
| 170 | $options{'tagname'} = $tag->tag(); | ||
| 171 | } | ||
| 172 | |||
| 173 | if ($options{'element_type'} eq '-->text') { | ||
| 174 | $options{'text'} = $tag->{'text'}; | ||
| 175 | if ($self->{'no_squash_whitespace'}) { | ||
| 176 | if (ref($self->{'no_squash_whitespace'}) eq 'ARRAY') { | ||
| 177 | my $squash = 1; | ||
| 178 | foreach (@{ $self->{'no_squash_whitespace'} }) { | ||
| 179 | if ($_ eq $self->{'_globals'}->{'last_node'}->{'tagname'}) { | ||
| 180 | $squash = 0; | ||
| 181 | last; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | if ($squash) { | ||
| 185 | $options{'text'} = _squash_whitespace ($options{'text'}); | ||
| 186 | return if (!$options{'text'}); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | } | ||
| 190 | else { | ||
| 191 | $options{'text'} = _squash_whitespace ($options{'text'}); | ||
| 192 | return if (!$options{'text'}); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | $node = Markup::TreeNode->new(%options); | ||
| 197 | |||
| 198 | while (($node->{'level'} - 1) != $parent->{'level'}) { | ||
| 199 | if ($parent->{'parent'} eq '(empty)') { last; } | ||
| 200 | $parent = $parent->{'parent'}; | ||
| 201 | } | ||
| 202 | |||
| 203 | $node->attach_parent($parent); | ||
| 204 | |||
| 205 | $self->{'_globals'}->{'last_node'} = $node; | ||
| 206 | |||
| 207 | foreach my $child ($tag->content_list) { | ||
| 208 | $self->{'_globals'}->{'level'}++; | ||
| 209 | if ($child->isa('HTML::Element')) { | ||
| 210 | $build_tree->($child); | ||
| 211 | } | ||
| 212 | else { | ||
| 213 | Carp::croak("Don't recognize $child!"); | ||
| 214 | } | ||
| 215 | $self->{'_globals'}->{'level'}--; | ||
| 216 | } | ||
| 217 | }; | ||
| 218 | |||
| 219 | $build_tree->($self->{'_parser'}); | ||
| 220 | } | ||
| 221 | |||
| 222 | sub _squash_whitespace { | ||
| 223 | my $text = shift(); | ||
| 224 | $text =~ s/^(?:\s+)/ /sm; | ||
| 225 | $text =~ s/(?:\s+)$/ /sm; | ||
| 226 | $text =~ s/(\s){2,}/$1/gsm; | ||
| 227 | return ($text =~ m/^(?:\s+)?$/) ? undef : $text; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub _init_xml_hookups { | ||
| 231 | my $self = shift(); | ||
| 232 | $self->{'_globals'}->{'new'} = [ 1, undef ]; | ||
| 233 | |||
| 234 | $self->{'_parser'}->setHandlers('Start', sub { | ||
| 235 | my ($expat, $tag, @attrs) = @_; | ||
| 236 | my %attrs; | ||
| 237 | while (scalar(@attrs)) { | ||
| 238 | $attrs{pop(@attrs)} = pop(@attrs); | ||
| 239 | } | ||
| 240 | delete $attrs{'/'}; | ||
| 241 | my $node = Markup::TreeNode->new (element_type => 'tag', tagname => $tag, attr => \%attrs, | ||
| 242 | level => $self->{'_globals'}->{'level'}); | ||
| 243 | my $parent = $self->{'_globals'}->{'last_node'}; | ||
| 244 | |||
| 245 | while (($node->{'level'} - 1) != $parent->{'level'}) { | ||
| 246 | if ($parent->{'parent'} eq '(empty)') { last; } | ||
| 247 | $parent = $parent->{'parent'}; | ||
| 248 | } | ||
| 249 | |||
| 250 | $node->attach_parent($parent); | ||
| 251 | |||
| 252 | $self->{'_globals'}->{'last_node'} = $node; | ||
| 253 | $self->{'_globals'}->{'level'}++; | ||
| 254 | $self->{'_globals'}->{'new'} = [ 1, undef ]; | ||
| 255 | }); | ||
| 256 | |||
| 257 | $self->{'_parser'}->setHandlers('Char', sub { | ||
| 258 | my ($expat, $text) = @_; | ||
| 259 | |||
| 260 | if ($self->{'no_squash_whitespace'}) { | ||
| 261 | if (ref($self->{'no_squash_whitespace'}) eq 'ARRAY') { | ||
| 262 | my $squash = 1; | ||
| 263 | foreach (@{ $self->{'no_squash_whitespace'} }) { | ||
| 264 | if ($_ eq $self->{'_globals'}->{'last_node'}->{'tagname'}) { | ||
| 265 | $squash = 0; | ||
| 266 | last; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | $text = _squash_whitespace ($text) if ($squash); | ||
| 270 | return if (!$text); | ||
| 271 | } | ||
| 272 | } | ||
| 273 | else { | ||
| 274 | $text = _squash_whitespace ($text); | ||
| 275 | return if (!$text); | ||
| 276 | } | ||
| 277 | |||
| 278 | if ($self->{'_globals'}->{'new'}->[0]) { | ||
| 279 | my $estruct = Markup::TreeNode->new(element_type => '-->text', tagname => '-->text', | ||
| 280 | level => ($self->{'_globals'}->{'last_node'}->{'level'} + 1), | ||
| 281 | text => $text); | ||
| 282 | |||
| 283 | $self->{'_globals'}->{'last_node'}->attach_child($estruct); | ||
| 284 | |||
| 285 | $self->{'_globals'}->{'new'} = [ 0, $estruct ]; | ||
| 286 | } | ||
| 287 | else { | ||
| 288 | $self->{'_globals'}->{'new'}->[1]->{'text'} .= $text; | ||
| 289 | } | ||
| 290 | } ); | ||
| 291 | |||
| 292 | $self->{'_parser'}->setHandlers('End', sub { | ||
| 293 | $self->{'_globals'}->{'level'}--; | ||
| 294 | }); | ||
| 295 | } | ||
| 296 | |||
| 297 | sub get_node { | ||
| 298 | my ($self, $description) = @_; | ||
| 299 | $description = lc $description; | ||
| 300 | my $fault = 1; | ||
| 301 | |||
| 302 | foreach (qw(first last start end root copy-of copy copy_of)) { | ||
| 303 | if ($description eq $_) { | ||
| 304 | $fault = 0; | ||
| 305 | last; | ||
| 306 | } | ||
| 307 | } | ||
| 308 | |||
| 309 | if ($fault) { | ||
| 310 | Carp::croak ("Unknown node description $description."); | ||
| 311 | } | ||
| 312 | |||
| 313 | if (($description eq 'copy-of') || ($description eq 'copy') || ($description eq 'copy_of')) { | ||
| 314 | return ($self->{'_tree'}->copy_of()); | ||
| 315 | } | ||
| 316 | |||
| 317 | if (($description eq 'first') || ($description eq 'start')) { | ||
| 318 | return ($self->{'_tree'}->next_node()); | ||
| 319 | } | ||
| 320 | |||
| 321 | if (($description eq 'last') || ($description eq 'end')) { | ||
| 322 | my $ret; | ||
| 323 | $self->foreach_node(sub { $ret = shift(); }); | ||
| 324 | return ($ret); | ||
| 325 | } | ||
| 326 | |||
| 327 | return $self->{'_tree'}; | ||
| 328 | } | ||
| 329 | |||
| 330 | sub foreach_node { | ||
| 331 | my ($self, $start_callback, $end_callback, $start_from) = @_; | ||
| 332 | my $walk_tree; | ||
| 333 | |||
| 334 | if (!$start_from) { | ||
| 335 | if ($end_callback && UNIVERSAL::isa($end_callback, 'Markup::TreeNode')) { | ||
| 336 | $start_from = $end_callback; | ||
| 337 | $end_callback = undef; | ||
| 338 | } | ||
| 339 | else { | ||
| 340 | $start_from = $self->{'_tree'}; | ||
| 341 | } | ||
| 342 | } | ||
| 343 | |||
| 344 | if (!($start_callback || (ref($start_callback) ne 'CODE'))) { | ||
| 345 | Carp::croak ("parameter 0 is not a CODE reference"); | ||
| 346 | } | ||
| 347 | |||
| 348 | $walk_tree = sub { | ||
| 349 | my $tree = shift(); | ||
| 350 | |||
| 351 | if (ref($tree) eq 'ARRAY') { | ||
| 352 | for (my $i = 0; $i < scalar(@{ $tree }); $i++) { | ||
| 353 | $walk_tree->($tree->[$i]); | ||
| 354 | } | ||
| 355 | } | ||
| 356 | elsif ($tree->isa('Markup::TreeNode')) { | ||
| 357 | if ($tree->{'element_type'} eq '-->ignore') { | ||
| 358 | $walk_tree->($tree->{'children'}); | ||
| 359 | return; | ||
| 360 | } | ||
| 361 | return if (!$start_callback->($tree)); | ||
| 362 | $walk_tree->($tree->{'children'}); | ||
| 363 | if ($end_callback && ref($end_callback) eq 'CODE') { | ||
| 364 | return if (!$end_callback->($tree)); | ||
| 365 | } | ||
| 366 | } | ||
| 367 | }; | ||
| 368 | |||
| 369 | $walk_tree->($start_from); | ||
| 370 | } | ||
| 371 | |||
| 372 | sub save_as { | ||
| 373 | my ($self, $file, $file_type) = @_; | ||
| 374 | |||
| 375 | if (!$file_type) { | ||
| 376 | if ($file =~ m/\.xml$/i) { | ||
| 377 | $file_type = 'xml'; | ||
| 378 | } | ||
| 379 | else { | ||
| 380 | $file_type = 'html'; | ||
| 381 | } | ||
| 382 | } | ||
| 383 | |||
| 384 | $file_type = lc $file_type; | ||
| 385 | |||
| 386 | $file = _mk_filehandle_write($file); | ||
| 387 | |||
| 388 | print $file '<?xml version = "1.0" encoding = "iso-8859-1"?>'."\n"; | ||
| 389 | if ($file_type =~ '(?:x)?html') { | ||
| 390 | print $file '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" '; | ||
| 391 | print $file '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n"; | ||
| 392 | } | ||
| 393 | |||
| 394 | $self->foreach_node( | ||
| 395 | sub { | ||
| 396 | my $node = shift(); | ||
| 397 | |||
| 398 | return 1 if ($node->{'element_type'} eq '-->root'); | ||
| 399 | return 1 if ($node->{'element_type'} eq '-->declaration'); | ||
| 400 | |||
| 401 | print $file '<!-- '.$node->{'text'}." -->\n" and return 1 | ||
| 402 | if ($node->{'element_type'} eq '-->comment'); | ||
| 403 | |||
| 404 | print $file $self->_rindent($node); | ||
| 405 | unless ($node->{'element_type'} eq '-->text') { | ||
| 406 | print $file "<".$node->{'tagname'}; | ||
| 407 | foreach (keys %{ $node->{'attr'} }) { | ||
| 408 | print $file " $_ = \"".$node->{'attr'}->{$_}."\""; | ||
| 409 | } | ||
| 410 | if (!scalar(@{$node->{'children'} || []})) { | ||
| 411 | print $file ' /'; | ||
| 412 | } | ||
| 413 | } | ||
| 414 | print $file (($node->{'element_type'} eq '-->text') ? $node->{'text'} : ">")."\n"; | ||
| 415 | return 1; | ||
| 416 | }, | ||
| 417 | sub { | ||
| 418 | my $node = shift(); | ||
| 419 | |||
| 420 | return 1 if ($node->{'element_type'} eq '-->root'); | ||
| 421 | return 1 if ($node->{'element_type'} eq '-->declaration'); | ||
| 422 | return 1 if (!scalar(@{$node->{'children'} || []})); | ||
| 423 | return 1 if ($node->{'element_type'} eq '-->text'); | ||
| 424 | |||
| 425 | print $file $self->_rindent($node); | ||
| 426 | print $file '</'.$node->{'tagname'}.">\n"; | ||
| 427 | |||
| 428 | return 1; | ||
| 429 | } | ||
| 430 | ); | ||
| 431 | |||
| 432 | close ($file) or Carp::croak ("Could not close file - $!"); | ||
| 433 | } | ||
| 434 | |||
| 435 | sub _rindent { | ||
| 436 | my ($self, $node) = @_; | ||
| 437 | my $ident = $node->{'level'}; | ||
| 438 | my $buf = ""; | ||
| 439 | |||
| 440 | if ($self->{'no_indent'}) { | ||
| 441 | return if (ref($self->{'no_indent'}) ne 'ARRAY'); | ||
| 442 | my $indent = 1; | ||
| 443 | foreach (@{ $self->{'no_indent'} }) { | ||
| 444 | if ($_ eq $node->{'tagname'}) { $indent = 0; last; } | ||
| 445 | } | ||
| 446 | |||
| 447 | return if (!$indent); | ||
| 448 | } | ||
| 449 | |||
| 450 | while ($ident--) { | ||
| 451 | $buf .= "\t"; | ||
| 452 | } | ||
| 453 | |||
| 454 | return $buf; | ||
| 455 | } | ||
| 456 | |||
| 457 | sub _mk_filehandle { | ||
| 458 | my $something = shift(); | ||
| 459 | |||
| 460 | return $something if (ref($something) eq 'GLOB'); | ||
| 461 | |||
| 462 | if ($something =~ m-^(?:ht|f)tp://-) { | ||
| 463 | my ($data, $fn) = ('', tmpnam()); | ||
| 464 | $data = get($something) or Carp::croak("Could not access url $something. $!"); | ||
| 465 | print $fn $data; | ||
| 466 | seek ($fn, 0, 0); | ||
| 467 | return ($fn); | ||
| 468 | } | ||
| 469 | |||
| 470 | open (TMP, $something) or Carp::croak("Could not open $something for reading. $!"); | ||
| 471 | return (\*TMP); | ||
| 472 | } | ||
| 473 | |||
| 474 | # won't really work on GLOBs X( | ||
| 475 | sub _mk_filehandle_write { | ||
| 476 | my $something = shift(); | ||
| 477 | |||
| 478 | if (ref($something) eq 'GLOB') { | ||
| 479 | open (W, ">&=", fileno($something)); | ||
| 480 | return (\*W); | ||
| 481 | } | ||
| 482 | |||
| 483 | if ($something =~ m-^(?:ht|f)tp://-) { | ||
| 484 | Carp::croak("writable urls are not supported"); | ||
| 485 | } | ||
| 486 | |||
| 487 | open (TMP, '>', $something) or Carp::croak("Could not open $something for reading. $!"); | ||
| 488 | return (\*TMP); | ||
| 489 | } | ||
| 490 | |||
| 491 | sub tree { shift()->{'_tree'}; } | ||
| 492 | |||
| 493 | sub copy_of { | ||
| 494 | my $self = shift(); | ||
| 495 | my ($newbie => %options); # if you don't know you betta' axe somebody! | ||
| 496 | |||
| 497 | foreach (keys %{ $self }) { | ||
| 498 | $options{$_} = $self->{$_}; | ||
| 499 | } | ||
| 500 | |||
| 501 | foreach (qw(_parser _globals _tree)) { | ||
| 502 | delete $options{$_}; | ||
| 503 | } | ||
| 504 | |||
| 505 | $newbie = $self->new(%options); | ||
| 506 | |||
| 507 | $newbie->{'_tree'} = $self->get_node('first')->copy_of(); | ||
| 508 | |||
| 509 | return ($newbie); | ||
| 510 | } | ||
| 511 | |||
| 512 | 1; | ||
| 513 | |||
| 514 | __END__ |