| 1 |
#!/usr/bin/perl |
| 2 |
## skip Test::Tabs |
| 3 |
|
| 4 |
use Getopt::Long; |
| 5 |
use HTML::HTML5::Parser; |
| 6 |
|
| 7 |
my $output = $ENV{HTML_OUTPUT} || 'debug:json'; |
| 8 |
my $help; |
| 9 |
GetOptions( |
| 10 |
'output|o=s' => \$output, |
| 11 |
'help|usage|h' => \$help, |
| 12 |
); |
| 13 |
|
| 14 |
if ($help) |
| 15 |
{ |
| 16 |
my $name = $0; |
| 17 |
print <<HELP and exit(1); |
| 18 |
Usage: cat document.html | $0 --output=FORMAT |
| 19 |
|
| 20 |
Reads HTML from STDIN and prints it out in a format useful for debugging. |
| 21 |
|
| 22 |
Formats: |
| 23 |
|
| 24 |
clarkml - ClarkML |
| 25 |
debug - Debugging tree |
| 26 |
debug:json - Debugging tree as JSON (default) |
| 27 |
errors - List of errors |
| 28 |
html - HTML |
| 29 |
parser - Parser internals |
| 30 |
parser:json - Parser internals as JSON |
| 31 |
xml - XHTML |
| 32 |
|
| 33 |
HELP |
| 34 |
} |
| 35 |
|
| 36 |
sub load |
| 37 |
{ |
| 38 |
my ($pkg) = @_; |
| 39 |
unless (eval "use $pkg; 1;") |
| 40 |
{ |
| 41 |
die "This output format requires $pkg.\n"; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
my $p = HTML::HTML5::Parser->new; |
| 46 |
my $h = join '', <>; |
| 47 |
my $hash; |
| 48 |
|
| 49 |
if ($output =~ /debug/i) |
| 50 |
{ |
| 51 |
load('XML::LibXML::Debugging'); |
| 52 |
$hash = $p->parse_string($h)->toDebuggingHash; |
| 53 |
} |
| 54 |
elsif ($output =~ /clark/i) |
| 55 |
{ |
| 56 |
load('XML::LibXML::Debugging'); |
| 57 |
print $p->parse_string($h)->toClarkML; |
| 58 |
} |
| 59 |
elsif ($output =~ /html/i) |
| 60 |
{ |
| 61 |
load('HTML::HTML5::Writer'); |
| 62 |
print HTML::HTML5::Writer->new->document($p->parse_string($h)); |
| 63 |
} |
| 64 |
elsif ($output =~ /parser/i) |
| 65 |
{ |
| 66 |
$p->parse_string($h); |
| 67 |
$hash = $p; |
| 68 |
} |
| 69 |
elsif ($output =~ /err/i) |
| 70 |
{ |
| 71 |
$p->parse_string($h); |
| 72 |
print "$_\n" foreach $p->errors; |
| 73 |
} |
| 74 |
else |
| 75 |
{ |
| 76 |
print $p->parse_string($h)->toString; |
| 77 |
} |
| 78 |
|
| 79 |
if (defined $hash and $output =~ /json/) |
| 80 |
{ |
| 81 |
load('JSON'); |
| 82 |
print to_json($hash, {pretty=>1,canonical=>1}); |
| 83 |
} |
| 84 |
elsif (defined $hash) |
| 85 |
{ |
| 86 |
load('Data::Dumper'); |
| 87 |
print Dumper($hash); |
| 88 |
} |