--- trunk2/lib/WebPAC/Tree.pm 2004/09/14 17:10:04 442 +++ trunk2/lib/WebPAC/Tree.pm 2004/09/20 19:13:27 455 @@ -5,7 +5,6 @@ use Carp; use Log::Log4perl qw(get_logger :levels); -use locale; =head1 NAME @@ -23,7 +22,7 @@ my @tree = ({ # level 0 - code_arr => sub { @{$l->{$_[0]}} }, + code_arr => sub { @{$_[0]} }, filter_code => sub { shift }, lookup_v900 => sub { my ($c,$p) = @_; @@ -32,11 +31,10 @@ }, lookup_term => sub { shift @{$l->{"d:".$_[0]}} }, lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} }, - have_children => sub { defined($l->{$_[1]}) }, - child_code => sub { return $_[1] }, + have_children => sub { return $l->{$_[1]} }, },{ # level 1 - code_arr => sub { @{$l->{$_[0]}} }, + code_arr => sub { @{$_[0]} }, filter_code => sub { shift }, lookup_v900 => sub { my ($c,$p) = @_; @@ -46,9 +44,12 @@ lookup_term => sub { shift @{$l->{"d:".$_[0]}} }, lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} }, have_children => sub { 0 }, - child_code => sub { 0 }, )}; +You can, however, create recursion with C discussed +below, but you loose ability to limit tree depth or to specify different +style for each level. + Documentation for each element of tree is little sparse, but here it is: =over 5 @@ -90,15 +91,18 @@ =item have_children -Returns C or C depending if current node have children. +Returns children for next iteration of tree generation or undef. + + my $next_lvl = $t->{'have_children'}->($code,$v900,$start_code); - if ($t->{'have_children'}->($code,$v900,$start_code)) { ... } +=item have_children_at_level -=item child_code +Returns children for next iteration and next level. -Returns child code for next iteration of tree generation. + my ($level,$next_lvl) = $t->{'have_children_at_level'}->($code,$v900,$start_code); - my $child_code = $t->{'child_code'}->($code,$v900,$start_code); +It's safe to return undef just for next level data (C<$next_lvl> in example +above) to stop recursion. =back @@ -192,6 +196,13 @@ my $log = $self->_get_logger(); + if (! defined($level)) { + $log->warn("level is undef, stoping recursion..."); + return; + } + + my $next_level = $level + 1; + $log->logconfess("need level") unless (defined($level)); $log->logconfess("need start_code") unless (defined($start_code)); @@ -226,16 +237,20 @@ my ($link_start,$link_end) = ('',''); my $have_children = $tree->[$level]->{'have_children'}->($code,$v900,$start_code); - if ($have_children) { - ($link_start,$link_end) = (qq{},qq{}); - } else { + + if (! $have_children) { $log->debug("# $level doesn't have_children($code,$v900,$start_code)"); + ($next_level,$have_children) = $tree->[$level]->{'have_children_at_level'}->($code,$v900,$start_code) if ($tree->[$level]->{'have_children_at_level'}); + $log->debug("# $level have_children($code,$v900,$start_code) on level $next_level") if ($have_children); + } + ($link_start,$link_end) = (qq{},qq{}) if ($have_children); + my $mfn_link = "thes/$mfn.html"; if (-e "out/$mfn_link") { $html .= " " x $level . - qq{
  • ${link_start}${term}${link_end}}. + qq{
  • ${link_start}${term}${link_end}}. qq{ »
  • \n}; } else { $log->warn("file 'out/$mfn_link' doesn't exist, skipping"); @@ -262,7 +277,7 @@ push @{$self->{'show_ids'}}, "id$mfn"; } - $html .= $self->unroll($level+1, $tree->[$level]->{'child_code'}->($code,$v900,$start_code)); + $html .= $self->unroll($next_level, $have_children); $html .= " " x $level . qq{\n};