root/applications/bdmtoolbox/doc/from_bdm/filter_annotated.pl @ 1371

Revision 1109, 2.9 kB (checked in by miro, 14 years ago)

templated classes remain in filtered annotated.html file

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