5 |
|
|
6 |
use Carp; |
use Carp; |
7 |
use Log::Log4perl qw(get_logger :levels); |
use Log::Log4perl qw(get_logger :levels); |
8 |
use locale; |
use YAML; |
9 |
|
|
10 |
=head1 NAME |
=head1 NAME |
11 |
|
|
23 |
|
|
24 |
my @tree = ({ |
my @tree = ({ |
25 |
# level 0 |
# level 0 |
26 |
code_arr => sub { @{$l->{$_[0]}} }, |
code_arr => sub { @{$_[0]} }, |
27 |
filter_code => sub { shift }, |
filter_code => sub { shift }, |
28 |
lookup_v900 => sub { |
lookup_v900 => sub { |
29 |
my ($c,$p) = @_; |
my ($c,$p) = @_; |
32 |
}, |
}, |
33 |
lookup_term => sub { shift @{$l->{"d:".$_[0]}} }, |
lookup_term => sub { shift @{$l->{"d:".$_[0]}} }, |
34 |
lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} }, |
lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} }, |
35 |
have_children => sub { defined($l->{$_[1]}) }, |
have_children => sub { return $l->{$_[1]} }, |
|
child_code => sub { return $_[1] }, |
|
36 |
},{ |
},{ |
37 |
# level 1 |
# level 1 |
38 |
code_arr => sub { @{$l->{$_[0]}} }, |
code_arr => sub { @{$_[0]} }, |
39 |
filter_code => sub { shift }, |
filter_code => sub { shift }, |
40 |
lookup_v900 => sub { |
lookup_v900 => sub { |
41 |
my ($c,$p) = @_; |
my ($c,$p) = @_; |
45 |
lookup_term => sub { shift @{$l->{"d:".$_[0]}} }, |
lookup_term => sub { shift @{$l->{"d:".$_[0]}} }, |
46 |
lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} }, |
lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} }, |
47 |
have_children => sub { 0 }, |
have_children => sub { 0 }, |
|
child_code => sub { 0 }, |
|
48 |
)}; |
)}; |
49 |
|
|
50 |
|
You can, however, create recursion with C<have_children_at_level> discussed |
51 |
|
below, but you loose ability to limit tree depth or to specify different |
52 |
|
style for each level. |
53 |
|
|
54 |
Documentation for each element of tree is little sparse, but here it is: |
Documentation for each element of tree is little sparse, but here it is: |
55 |
|
|
56 |
=over 5 |
=over 5 |
76 |
|
|
77 |
Lookup value which will be called C<$v900> from now on. |
Lookup value which will be called C<$v900> from now on. |
78 |
|
|
79 |
my $v900 = $t->{'lookup_v900'}->($code,$start_code); |
my $v900 = $t->{'lookup_v900'}->($code); |
80 |
|
|
81 |
=item lookup_term |
=item lookup_term |
82 |
|
|
92 |
|
|
93 |
=item have_children |
=item have_children |
94 |
|
|
95 |
Returns C<true> or C<false> depending if current node have children. |
Returns children for next iteration of tree generation or undef. |
96 |
|
|
97 |
|
my $next_lvl = $t->{'have_children'}->($code,$v900); |
98 |
|
|
99 |
if ($t->{'have_children'}->($code,$v900,$start_code)) { ... } |
=item have_children_at_level |
100 |
|
|
101 |
=item child_code |
Returns children for next iteration and next level. |
102 |
|
|
103 |
Returns child code for next iteration of tree generation. |
my ($level,$next_lvl) = $t->{'have_children_at_level'}->($code,$v900); |
104 |
|
|
105 |
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 |
106 |
|
above) to stop recursion. |
107 |
|
|
108 |
=back |
=back |
109 |
|
|
156 |
$self->{'show_ids'} = []; |
$self->{'show_ids'} = []; |
157 |
$self->{'hide_ids'} = []; |
$self->{'hide_ids'} = []; |
158 |
|
|
159 |
my $html = $self->unroll(0,''); |
my $html = $self->unroll(0,()); |
160 |
|
|
161 |
my $html_file = $self->{'dir'}.'/'.$self->{'html'}; |
my $html_file = $self->{'dir'}.'/'.$self->{'html'}; |
162 |
|
|
186 |
|
|
187 |
Generate tree recursively. |
Generate tree recursively. |
188 |
|
|
189 |
my $html = $tree->unroll($level,$start_code); |
my $html = $tree->unroll($level,$data_arr); |
190 |
|
|
191 |
=cut |
=cut |
192 |
|
|
193 |
sub unroll { |
sub unroll { |
194 |
my $self = shift; |
my $self = shift; |
195 |
|
|
196 |
my ($level,$start_code) = @_; |
my ($level,$data_arr) = @_; |
197 |
|
|
198 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
199 |
|
|
200 |
|
if (! defined($level)) { |
201 |
|
$log->warn("level is undef, stoping recursion..."); |
202 |
|
return; |
203 |
|
} |
204 |
|
|
205 |
|
my $next_level = $level + 1; |
206 |
|
|
207 |
$log->logconfess("need level") unless (defined($level)); |
$log->logconfess("need level") unless (defined($level)); |
208 |
$log->logconfess("need start_code") unless (defined($start_code)); |
#$log->logconfess("need data_arr") unless (defined($data_arr)); |
209 |
|
|
210 |
my $tree = $self->{'tree'}; |
my $tree = $self->{'tree'}; |
211 |
|
|
214 |
# all levels passed? |
# all levels passed? |
215 |
return if (! defined($tree->[$level])); |
return if (! defined($tree->[$level])); |
216 |
|
|
217 |
$log->debug("unroll level $level, start code $start_code"); |
$log->debug("unroll level $level"); |
218 |
|
|
219 |
my $html; |
my $html; |
220 |
|
|
221 |
foreach my $code ($tree->[$level]->{'code_arr'}->($start_code)) { |
foreach my $code ($tree->[$level]->{'code_arr'}->($data_arr)) { |
222 |
|
|
223 |
if ($code = $tree->[$level]->{'filter_code'}->($code)) { |
if ($code = $tree->[$level]->{'filter_code'}->($code)) { |
224 |
|
|
225 |
$log->debug("# $level filter passed code $code"); |
$log->debug("# $level filter passed code $code"); |
226 |
|
|
227 |
my $v900 = $tree->[$level]->{'lookup_v900'}->($code,$start_code) || $log->warn("can't lookup_v900($code,$start_code)"); |
my $v900 = $tree->[$level]->{'lookup_v900'}->($code) || $log->warn("can't lookup_v900($code)") && return; |
228 |
$log->debug("# $level lookup_v900($code,$start_code) = $v900"); |
$log->debug("# $level lookup_v900($code) = $v900"); |
229 |
|
|
230 |
my $term = $tree->[$level]->{'lookup_term'}->($code,$v900) || $log->warn("can't lookup_term($code,$v900)"); |
my $term = $tree->[$level]->{'lookup_term'}->($code,$v900) || $log->warn("can't lookup_term($code,$v900)") && return; |
231 |
$log->debug("# $level lookup_term($code,$v900) = $term"); |
$log->debug("# $level lookup_term($code,$v900) = $term"); |
232 |
|
|
233 |
my $mfn = $tree->[$level]->{'lookup_mfn'}->($code,$v900) || $log->warn("can't lookup_mfn($code,$v900)"); |
my $mfn = $tree->[$level]->{'lookup_mfn'}->($code,$v900) || $log->warn("can't lookup_mfn($code,$v900)") && return; |
234 |
$log->debug("# $level lookup_mfn($code,$v900) = $mfn"); |
$log->debug("# $level lookup_mfn($code,$v900) = $mfn"); |
235 |
|
|
236 |
$log->debug("$code -> $v900 : $term [$mfn]"); |
$log->debug("$code -> $v900 : $term [$mfn]"); |
237 |
|
|
238 |
my ($link_start,$link_end) = ('',''); |
my ($link_start,$link_end) = ('',''); |
239 |
|
|
240 |
my $have_children = $tree->[$level]->{'have_children'}->($code,$v900,$start_code); |
my $have_children = $tree->[$level]->{'have_children'}->($code,$v900); |
241 |
if ($have_children) { |
|
242 |
($link_start,$link_end) = (qq{<a href="#mfn$mfn" onClick="return toggle_display('id$mfn');">},qq{</a>}); |
if (! $have_children) { |
243 |
} else { |
$log->debug("# $level doesn't have_children($code,$v900)"); |
244 |
$log->debug("# $level doesn't have_children($code,$v900,$start_code)"); |
($next_level,$have_children) = $tree->[$level]->{'have_children_at_level'}->($code,$v900) if ($tree->[$level]->{'have_children_at_level'}); |
245 |
|
$log->debug("# $level have_children($code,$v900) on level $next_level") if ($have_children); |
246 |
|
|
247 |
} |
} |
248 |
|
|
249 |
|
($link_start,$link_end) = (qq{<a href="#mfn$mfn" onClick="return toggle_display('id$mfn');">},qq{</a>}) if ($have_children); |
250 |
|
|
251 |
my $mfn_link = "thes/$mfn.html"; |
my $mfn_link = "thes/$mfn.html"; |
252 |
if (-e "out/$mfn_link") { |
if (-e "out/$mfn_link") { |
253 |
$html .= " " x $level . |
$html .= " " x $level . |
278 |
push @{$self->{'show_ids'}}, "id$mfn"; |
push @{$self->{'show_ids'}}, "id$mfn"; |
279 |
} |
} |
280 |
|
|
281 |
$html .= $self->unroll($level+1, $tree->[$level]->{'child_code'}->($code,$v900,$start_code)); |
$html .= $self->unroll($next_level, $have_children); |
282 |
|
|
283 |
$html .= " " x $level . qq{</ul>\n}; |
$html .= " " x $level . qq{</ul>\n}; |
284 |
|
|