#!/usr/bin/perl -w

use strict;
use HTML::PullParser;
use HTML::Tagset;

my $handle;
my $indstr= " ";

if( @ARGV && $ARGV[0] =~ /^-(\d+)$/ ) {
    $indstr= " " x $1;
    shift @ARGV;
}

if( defined($ARGV[0]) ) {
    open $handle, $ARGV[0] || die "Cannot open source file $ARGV[0]: $!";
}
else {
    $handle= *STDIN{IO};
}

my $p= HTML::PullParser->new( file => $handle,
		text => '"v", text',
		start => '"s", text, tagname, attr',
		end => '"e", text' );

my $ind= 0;

open OUT, "| less" and select OUT;

while( defined(my $tok= $p->get_token()) )
{
    if( $tok->[0] eq "e" && $ind > 0 ) {
        --$ind;
    }
    $tok->[1] =~ s/\s+$//;
    $tok->[2] =~ s!/$!! if $tok->[2];     # catch xhtml incompatibility
    print $indstr x $ind, $tok->[1], "\n" if length($tok->[1]);
    if( $tok->[0] eq "s" && ! ($HTML::Tagset::emptyElement{$tok->[2]} || $tok->[3]->{"/"}) ) {
        ++$ind;
    }
}

close $handle if defined($ARGV[0]);

