use warnings; use strict; use HTML::TreeBuilder; my $Tree = HTML::TreeBuilder->new (); $Tree->parse ("

First para.
Second line.

Second para

"); my $Text; my $empty_element_map = $Tree->_empty_element_map; my(@C) = [$Tree]; # a stack containing lists of children # I is a stack of indexes to current position in corresponding lists in @C # In each of these, 0 is the active point my(@I) = (-1); # initial value must be -1 for each list my @Context = ""; # Contains stack of current nodes # scratch: my $this; # current node my $content_r; # child list of $this my $TagName; # Loop over the tree while (@C) { # Post processing # Move to next item in this frame if(!defined($I[0]) or ++$I[0] >= @{$C[0]}) { $this = $Context [0]; if (defined $this and ref $this) {# Close tag my $StartTag = $this->starttag (); $StartTag =~ s/[\r\n]*//gs; if ($StartTag =~ /^/g) { $Text .= "

"; } } shift @Context; shift @I; shift @C; next; } $this = $C[0][$I[0]]; if (! ref $this) {# Add the text $Text .= $this; } else {# Process this element my $StartTag = $this->starttag (); $StartTag =~ s/[\r\n]*//gs; if ($StartTag =~ /^(<(?:p|br)\b.*?>)/g) { $Text .= "$1" } } # Now queue up content list for the current element... if( ref $this and not ( # ...except for those which not($content_r = $this->{'_content'} and @$content_r) and # ...have empty content lists $this->{'_empty_element'} || $empty_element_map->{$this->{'_tag'} || ''} # ...and that don't get post-order callbacks ) ) { unshift @Context, $this; unshift @I, -1; unshift @C, $content_r || []; } } print $Text; ####

First para.
Second line.

Second para