--- trunk/scripts/est-spider 2006/08/15 16:38:06 178 +++ trunk/scripts/est-spider 2006/08/25 11:59:04 179 @@ -8,6 +8,8 @@ #use File::MMagic; use File::MMagic::XS qw/:compat/; use Time::HiRes qw/time/; +use HTML::TreeBuilder; +use Data::Dump qw/dump/; my $collection; # name which will be inserted my $path_add; # add additional info in path @@ -78,8 +80,103 @@ user => 'admin', passwd => 'admin', croak_on_error => 1, + create => 1, ); +# +# check if hhc file exists, and if it does, extract information from it +# + +my $hhc_file; +# try to find hhc +find({ wanted => sub { + return unless( m!\.hhc$!i ); + $hhc_file = $_; + warn "using $hhc_file for tree structure\n"; + }, + follow => 1, + follow_skip => 2, + no_chdir => 1, +}, $dir); + +my $meta; + +if ($hhc_file) { + + sub param { + my ($el) = @_; + + my $n; + foreach my $p ( $el->find('param') ) { + $n->{ $p->attr('name') } = $p->attr('value'); + } + + if ( ! defined($n->{Local}) ) { + warn "### skipped = ",dump($n),$/; + return; + } + + my $path = $dir . '/' . $n->{Local}; + $path =~ s!//!/!g; + $path = lc($path); + + $n->{path} = $path; + + my $nr = $n->{ImageNumber} || next; + + if ($nr == 27) { + $meta->{title} = $n->{Name}; + $meta->{index_path} = $path; + } elsif ($nr == 21) { + $meta->{toc_path} = $path; + } elsif ($nr == 1) { + $meta->{foreword_path} = $path; + } elsif ($nr == 11) { + # nop + } else { + warn "unknown ImageNumber: $nr\n"; + } + + return $n; + } + + my $tree = HTML::TreeBuilder->new; + $tree->parse_file($hhc_file); + + my $prefix = $collection ? ( $collection . ' :: ' ) : ''; + + my @prefix; + my $depth = 0; + + foreach my $e ( $tree->look_down( sub { $_[0]->tag =~ m/(object)/ } ) ) { + +# printf("%05s %s\n", $e->parent->address(), $e->as_HTML() ); + + my $l = ($e->depth() / 2) - 1; + + $prefix[ 0 ] = $meta->{title} || ''; + + my $n = param($e); + $prefix[ $l ] = $n->{Name}; + + next unless ($n->{path}); + + my $t = ''; + my @p; + foreach my $i ( 0 .. $l ) { + push @p, $prefix[ $i ] if ($prefix[ $i ]); + } + $t = join(' :: ', @p ) if (@p); + + $meta->{path2title}->{ $n->{path} } = $t; + + } + + $tree->delete; + +} + + find({ wanted => \&file, follow => 1, follow_skip => 2, @@ -116,12 +213,21 @@ # create a document object my $doc = new Search::Estraier::Document; - my $title = $1 if ($contents =~ m#(.+?)#is); + my $title; - # chop long titles to 100 chars - $title = substr($title, 0, 100) . '...' if ($title && length($title) > 100); - # use path if no title is found - $title ||= $path; + if ( defined($meta->{path2title}->{lc($path)}) ) { + $title = $meta->{path2title}->{lc($path)}; + warn " $title\n"; + } else { + + $title = $1 if ($contents =~ m#(.+?)#is); + + # chop long titles to 100 chars + $title = substr($title, 0, 100) . '...' if ($title && length($title) > 100); + # use path if no title is found + $title ||= $path; + + } # add attributes to the document object $doc->add_attr('@uri', "file:///$path");