6 |
use Carp; |
use Carp; |
7 |
use Log::Log4perl qw(get_logger :levels); |
use Log::Log4perl qw(get_logger :levels); |
8 |
use YAML; |
use YAML; |
9 |
|
use Template; |
10 |
|
|
11 |
=head1 NAME |
=head1 NAME |
12 |
|
|
15 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
16 |
|
|
17 |
This module will create tree from lookup data. It requires quite complicated |
This module will create tree from lookup data. It requires quite complicated |
18 |
data structure, but once you get hang of that, it's peace of case :-) |
data structure, but once you get hang of that, it's peace of cake :-) |
19 |
|
|
20 |
Data structure for tree definition is non-recursive, and defines each level |
Data structure for tree definition is non-recursive, and defines each level |
21 |
of tree individually (so you can limit depth of tree) like this: |
of tree individually (so you can limit depth of tree) like this: |
34 |
lookup_term => sub { shift @{$l->{"d:".$_[0]}} }, |
lookup_term => sub { shift @{$l->{"d:".$_[0]}} }, |
35 |
lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} }, |
lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} }, |
36 |
have_children => sub { return $l->{$_[1]} }, |
have_children => sub { return $l->{$_[1]} }, |
37 |
|
iframe => 1, |
38 |
},{ |
},{ |
39 |
# level 1 |
# level 1 |
40 |
code_arr => sub { @{$_[0]} }, |
code_arr => sub { @{$_[0]} }, |
47 |
lookup_term => sub { shift @{$l->{"d:".$_[0]}} }, |
lookup_term => sub { shift @{$l->{"d:".$_[0]}} }, |
48 |
lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} }, |
lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} }, |
49 |
have_children => sub { 0 }, |
have_children => sub { 0 }, |
50 |
|
style => 'display: none', |
51 |
)}; |
)}; |
52 |
|
|
53 |
You can, however, create recursion with C<have_children_at_level> discussed |
You can, however, create recursion with C<have_children_at_level> discussed |
108 |
It's safe to return undef just for next level data (C<$next_lvl> in example |
It's safe to return undef just for next level data (C<$next_lvl> in example |
109 |
above) to stop recursion. |
above) to stop recursion. |
110 |
|
|
111 |
|
=item iframe |
112 |
|
|
113 |
|
This optional option will create all children nodes in separate file, and iframe in tree html, |
114 |
|
so that generated tee html will have resonable size with large number of nodes. |
115 |
|
|
116 |
|
=item style |
117 |
|
|
118 |
|
Optional option to specify style of this node. |
119 |
|
|
120 |
=back |
=back |
121 |
|
|
122 |
=head1 METHODS |
=head1 METHODS |
169 |
$tree->output( |
$tree->output( |
170 |
dir => './out', |
dir => './out', |
171 |
html => 'browse.html', |
html => 'browse.html', |
172 |
template => './output_template/tree.tt', |
template_dir => './output_template/', |
173 |
|
template_tree => 'tree.tt', |
174 |
|
template_node => 'node.tt', |
175 |
js => 'tree-ids.js', |
js => 'tree-ids.js', |
176 |
); |
); |
177 |
|
|
180 |
|
|
181 |
C<html> is name of output html file. |
C<html> is name of output html file. |
182 |
|
|
183 |
C<template> is name of template. It uses Template Toolkit syntax [% var %], |
C<template_dir> is directory with Template Toolkit templates. |
184 |
but doesn't really use TT. |
|
185 |
|
C<template_tree> is name of template to produce tree. |
186 |
|
|
187 |
|
C<template_node> is (optional) name of template for node (if C<iframe> |
188 |
|
options is used within tree definition). |
189 |
|
|
190 |
C<js> is name of JavaScript file with shown and hidden ids. |
C<js> is name of JavaScript file with shown and hidden ids. |
191 |
|
|
198 |
|
|
199 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
200 |
|
|
201 |
foreach my $p (qw(dir html template js)) { |
foreach my $p (qw(dir html template_dir template_tree js)) { |
202 |
$log->logconfess("need $p") unless ($args->{$p}); |
$log->logconfess("need $p") unless ($args->{$p}); |
203 |
} |
} |
204 |
|
|
210 |
|
|
211 |
my $html_file = $args->{'dir'}.'/'.$args->{'html'}; |
my $html_file = $args->{'dir'}.'/'.$args->{'html'}; |
212 |
|
|
213 |
open(TEMPLATE, $args->{'template'}) || $log->logdie("can't open '",$args->{'template'},": $!"); |
$log->debug("templates are in ",$args->{'template_dir'}); |
214 |
my $tmpl; |
|
215 |
while(<TEMPLATE>) { |
my $tt = Template->new( |
216 |
$tmpl .= $_; |
INCLUDE_PATH => $args->{'template_dir'}, |
217 |
} |
); |
|
close(TEMPLATE); |
|
218 |
|
|
219 |
$log->info("creating '$html_file' with tree"); |
my $var = { |
220 |
|
js => $args->{'dir'}.'/'.$args->{'js'}, |
221 |
|
tree => $html, |
222 |
|
}; |
223 |
|
|
224 |
my $js_arr_file = $args->{'js'}; |
$log->info("creating '$html_file' with tree from ",$args->{'template_tree'}); |
|
$tmpl =~ s/\[%\s+js\s+%\]/$js_arr_file/gi; |
|
|
$tmpl =~ s/\[%\s+tree\s+%\]/$html/gi; |
|
225 |
|
|
226 |
open(HTML, ">", $html_file) || $log->logdie("can't open '$html_file': $!"); |
$tt->process($args->{'template_tree'}, $var, $html_file) || $log->logdie("template error: ",$tt->error()); |
|
print HTML $tmpl; |
|
|
close(HTML); |
|
227 |
|
|
228 |
|
my $js_file = $args->{'dir'}."/".$args->{'js'}; |
229 |
|
$log->info("creating '$js_file' with tree data"); |
230 |
$self->generate_js( |
$self->generate_js( |
231 |
file => $args->{'dir'}."/".$args->{'js'}, |
file => $js_file, |
232 |
); |
); |
233 |
|
|
234 |
|
if (! $args->{'nodes'}) { |
235 |
|
$log->warn("skipping node creation"); |
236 |
|
return $self; |
237 |
|
} |
238 |
|
|
239 |
|
foreach my $mfn (keys %{$self->{'node_html'}}) { |
240 |
|
|
241 |
|
my $html_file = $args->{'dir'}."/".$args->{'nodes'}."/${mfn}.html"; |
242 |
|
|
243 |
|
$log->debug("creating tree node $html_file"); |
244 |
|
|
245 |
|
$var = { |
246 |
|
node => $self->{'node_html'}->{$mfn}, |
247 |
|
}; |
248 |
|
|
249 |
|
$tt->process($args->{'template_node'}, $var, $html_file) || $log->logdie("template error: ",$tt->error()); |
250 |
|
} |
251 |
|
|
252 |
return $self; |
return $self; |
253 |
} |
} |
254 |
|
|
349 |
push @{$self->{'show_ids'}}, "id$mfn"; |
push @{$self->{'show_ids'}}, "id$mfn"; |
350 |
} |
} |
351 |
|
|
352 |
$html .= $self->unroll($next_level, $have_children); |
if ($tree->[$level]->{'iframe'}) { |
353 |
|
# unroll to separate file |
354 |
|
$self->{'node_html'}->{$mfn} = $self->unroll($next_level, $have_children); |
355 |
|
|
356 |
|
} else { |
357 |
|
# unroll at base HTML |
358 |
|
$html .= $self->unroll($next_level, $have_children); |
359 |
|
} |
360 |
|
|
361 |
$html .= " " x $level . qq{</ul>\n}; |
$html .= " " x $level . qq{</ul>\n}; |
362 |
|
|
363 |
} |
} |