[1007] | 1 | #!/usr/bin/perl |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use utf8; |
---|
| 6 | #use bigint; |
---|
| 7 | use List::MoreUtils qw(uniq); |
---|
| 8 | use HTML::TreeBuilder; |
---|
| 9 | use Data::Dumper; |
---|
| 10 | use Getopt::Long; |
---|
| 11 | use UNIVERSAL 'isa'; |
---|
| 12 | |
---|
[1043] | 13 | my $output_dir = '../html/bdm_doc'; |
---|
| 14 | my $input_dir = '../../../../library/doc/html/'; |
---|
[1007] | 15 | my $filename = 'annotated.html'; |
---|
| 16 | my @classes = @ARGV; |
---|
| 17 | ;;;print STDERR Dumper \@classes; |
---|
| 18 | |
---|
| 19 | GetOptions( |
---|
| 20 | 'filename=s' => \$filename, |
---|
| 21 | 'output-dir=s' => \$output_dir, |
---|
| 22 | 'input-dir=s' => \$input_dir, |
---|
| 23 | ) or die('Wrong options format'); |
---|
| 24 | |
---|
| 25 | my $out_filename = $output_dir . '/' . $filename; |
---|
| 26 | open (my $OUT, ">$out_filename") |
---|
| 27 | or die "cannot open $filename: $!"; |
---|
| 28 | $filename = $input_dir . '/' . $filename; |
---|
| 29 | |
---|
| 30 | print STDERR "Processing $filename\n"; |
---|
| 31 | |
---|
| 32 | my $html = HTML::TreeBuilder->new(); |
---|
[1039] | 33 | $html->store_comments(1); |
---|
[1007] | 34 | $html->parse_file($filename); |
---|
| 35 | my $body = $html->content_array_ref()->[1]; |
---|
| 36 | |
---|
| 37 | my $i = 0; |
---|
| 38 | my $div_content; |
---|
| 39 | while ($div_content = $body->content_array_ref()->[$i++]) { |
---|
| 40 | last if (defined $div_content->attr('class')) |
---|
| 41 | && ($div_content->attr('class') eq 'contents'); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | $i = 0; |
---|
| 45 | my $table; |
---|
| 46 | while ($table = $div_content->content_array_ref()->[$i++]) { |
---|
| 47 | last if isa($table, "HTML::Element") && ( $table->tag() eq 'table' ); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | $i = 0; |
---|
[1039] | 51 | my @to_delete; |
---|
[1007] | 52 | while (my $node = $table->content_array_ref()->[$i++]) { |
---|
| 53 | #;;print_content($node); |
---|
| 54 | my $class = $node->content_array_ref()->[0] #td |
---|
| 55 | ->content_array_ref()->[0] #a |
---|
| 56 | ->content_array_ref()->[0]; #bdm::ARX |
---|
| 57 | |
---|
[1039] | 58 | #;;map {print STDERR " $_\n"} grep {$_ eq $class} @classes; |
---|
| 59 | #;;print STDERR "$class\n"; |
---|
[1007] | 60 | |
---|
[1039] | 61 | if (grep {$_ eq $class} @classes) { |
---|
| 62 | ;;;print "$class is in @classes\n"; |
---|
| 63 | next; |
---|
| 64 | }; |
---|
[1007] | 65 | |
---|
[1039] | 66 | #;;print STDERR "$class will be detached\n"; |
---|
| 67 | #;;print STDERR $node->tag() . ' ' . $node->{_parent}->tag() . "\n"; |
---|
[1007] | 68 | |
---|
[1039] | 69 | push @to_delete, $node; |
---|
[1007] | 70 | } |
---|
| 71 | |
---|
[1039] | 72 | $_->delete() for @to_delete; |
---|
| 73 | |
---|
| 74 | #;;print_content($table); |
---|
| 75 | |
---|
[1007] | 76 | #print the result |
---|
| 77 | print $OUT $html->as_HTML(); |
---|
| 78 | |
---|
| 79 | $html->delete; |
---|
| 80 | |
---|
| 81 | #============================================================================= |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | # depth-first search |
---|
| 85 | sub dfs { |
---|
| 86 | my $node = shift or return; |
---|
| 87 | return if !ref $node; # do not process text nodes |
---|
| 88 | |
---|
| 89 | my $function = shift or return; |
---|
| 90 | my $data = shift || {}; |
---|
| 91 | my $depth = shift || 0; |
---|
| 92 | my $limit = shift || 10000; #inf; |
---|
| 93 | return if $depth > $limit; |
---|
| 94 | |
---|
| 95 | $function->($node, $data, $depth, $limit); |
---|
| 96 | |
---|
| 97 | for ($node->content_list()) { |
---|
| 98 | dfs($_, $function, $data, $depth+1, $limit); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | # print html subtree -- just for debugging |
---|
| 103 | sub print_content { |
---|
| 104 | my ($node, $depth, $limit) = @_; |
---|
| 105 | my $print_tag = sub { |
---|
| 106 | my ($node, undef, $depth) = @_; |
---|
| 107 | print STDERR " " x $depth, $node->tag(), "\n"; |
---|
| 108 | #;;print $node->as_HTML() if $node->tag() eq '~comment'; |
---|
| 109 | }; |
---|
| 110 | dfs($node, $print_tag, {}, $depth, $limit); |
---|
| 111 | } |
---|