root/library/doc/toolbox_doc/filter_annotated.pl @ 1039

Revision 1039, 2.7 kB (checked in by miro, 14 years ago)

fix

  • Property svn:executable set to *
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use utf8;
6#use bigint;
7use List::MoreUtils qw(uniq);
8use HTML::TreeBuilder;
9use Data::Dumper;
10use Getopt::Long;
11use UNIVERSAL 'isa';
12
13my $output_dir = '../toolbox_html';
14my $input_dir  = '../html';
15my $filename   = 'annotated.html';
16my @classes = @ARGV;
17;;;print STDERR Dumper \@classes;
18
19GetOptions(
20    'filename=s'   => \$filename,
21    'output-dir=s' => \$output_dir,
22    'input-dir=s'  => \$input_dir,
23) or die('Wrong options format');
24
25my $out_filename = $output_dir . '/' . $filename;
26open (my $OUT, ">$out_filename") 
27    or die "cannot open $filename: $!";
28$filename = $input_dir . '/' . $filename;
29
30print STDERR "Processing $filename\n";
31
32my $html = HTML::TreeBuilder->new();
33$html->store_comments(1);
34$html->parse_file($filename);
35my $body = $html->content_array_ref()->[1];
36
37my $i = 0;
38my $div_content;
39while ($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;
45my $table;
46while ($table = $div_content->content_array_ref()->[$i++]) {
47    last if isa($table, "HTML::Element") && ( $table->tag() eq 'table' );
48}
49
50$i = 0;
51my @to_delete;
52while (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
58    #;;map {print STDERR " $_\n"} grep {$_ eq $class} @classes;
59    #;;print STDERR "$class\n";
60
61    if (grep {$_ eq $class} @classes) {
62        ;;;print "$class is in @classes\n";
63        next;
64    };
65
66    #;;print STDERR "$class will be detached\n";
67    #;;print STDERR $node->tag() . ' ' . $node->{_parent}->tag() . "\n";
68
69    push @to_delete, $node;
70}
71
72$_->delete() for @to_delete;
73
74#;;print_content($table);
75
76#print the result
77print $OUT $html->as_HTML();
78
79$html->delete;
80
81#=============================================================================
82
83
84# depth-first search
85sub 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
103sub 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}
Note: See TracBrowser for help on using the browser.