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 |
126 |
Create new tree object |
Create new tree object |
127 |
|
|
128 |
my $tree = new WebPAC::Tree( |
my $tree = new WebPAC::Tree( |
|
dir => './out', |
|
|
html => 'browse.html', |
|
|
template => './output_template/tree.tt', |
|
|
js => 'tree-ids.js', |
|
129 |
tree => \@tree, |
tree => \@tree, |
130 |
log => 'log4perl.conf', |
log => 'log4perl.conf', |
131 |
); |
); |
132 |
|
|
|
C<dir> is output directory in which html files and JavaScript files will be |
|
|
created (think of it as C<public_html>). |
|
|
|
|
|
C<html> is name of output html file. |
|
|
|
|
|
C<template> is name of template. It uses Template Toolkit syntax [% var %], |
|
|
but doesn't really use TT. |
|
|
|
|
|
C<js> is name of JavaScript file with shown and hidden ids. |
|
|
|
|
133 |
C<tree> is tree array with levels of tree described above. |
C<tree> is tree array with levels of tree described above. |
134 |
|
|
135 |
C<log> is optional parametar which specify filename of L<Log::Log4Perl> |
C<log> is optional parametar which specify filename of L<Log::Log4Perl> |
147 |
|
|
148 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
149 |
|
|
150 |
foreach my $p (qw(dir html template js tree)) { |
$log->logconfess("need tree") unless ($self->{'tree'}); |
|
$log->logconfess("need $p") unless ($self->{$p}); |
|
|
} |
|
151 |
|
|
152 |
$self->{'show_ids'} = []; |
$self->{'show_ids'} = []; |
153 |
$self->{'hide_ids'} = []; |
$self->{'hide_ids'} = []; |
154 |
|
|
155 |
my $html = $self->unroll(0,()); |
$self->{'tree_html'} = $self->unroll(0,()); |
156 |
|
|
157 |
my $html_file = $self->{'dir'}.'/'.$self->{'html'}; |
if (! $self->{'tree_html'}) { |
158 |
|
$log->warn("no html generated by unroll..."); |
159 |
|
return; |
160 |
|
} |
161 |
|
|
162 |
|
return $self; |
163 |
|
} |
164 |
|
|
165 |
|
=head2 output |
166 |
|
|
167 |
|
Create output files from tree object |
168 |
|
|
169 |
|
$tree->output( |
170 |
|
dir => './out', |
171 |
|
html => 'browse.html', |
172 |
|
template_dir => './output_template/', |
173 |
|
template_tree => 'tree.tt', |
174 |
|
template_node => 'node.tt', |
175 |
|
js => 'tree-ids.js', |
176 |
|
); |
177 |
|
|
178 |
|
C<dir> is output directory in which html files and JavaScript files will be |
179 |
|
created (think of it as C<public_html>). |
180 |
|
|
181 |
|
C<html> is name of output html file. |
182 |
|
|
183 |
open(TEMPLATE, $self->{'template'}) || $log->logdie("can't open '",$self->{'template'},": $!"); |
C<template_dir> is directory with Template Toolkit templates. |
184 |
my $tmpl; |
|
185 |
while(<TEMPLATE>) { |
C<template_tree> is name of template to produce tree. |
186 |
$tmpl .= $_; |
|
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. |
191 |
|
|
192 |
|
=cut |
193 |
|
|
194 |
|
sub output { |
195 |
|
my $self = shift; |
196 |
|
|
197 |
|
my $args = {@_}; |
198 |
|
|
199 |
|
my $log = $self->_get_logger(); |
200 |
|
|
201 |
|
foreach my $p (qw(dir html template_dir template_tree js)) { |
202 |
|
$log->logconfess("need $p") unless ($args->{$p}); |
203 |
|
} |
204 |
|
|
205 |
|
my $html = $self->{'tree_html'}; |
206 |
|
unless ($html) { |
207 |
|
$log->warn("no html, output aborted"); |
208 |
|
return; |
209 |
|
} |
210 |
|
|
211 |
|
my $html_file = $args->{'dir'}.'/'.$args->{'html'}; |
212 |
|
|
213 |
|
$log->debug("templates are in ",$args->{'template_dir'}); |
214 |
|
|
215 |
|
my $tt = Template->new( |
216 |
|
INCLUDE_PATH => $args->{'template_dir'}, |
217 |
|
); |
218 |
|
|
219 |
|
my $var = { |
220 |
|
js => $args->{'dir'}.'/'.$args->{'js'}, |
221 |
|
tree => $html, |
222 |
|
}; |
223 |
|
|
224 |
|
$log->info("creating '$html_file' with tree from ",$args->{'template_tree'}); |
225 |
|
|
226 |
|
$tt->process($args->{'template_tree'}, $var, $html_file) || $log->logdie("template error: ",$tt->error()); |
227 |
|
|
228 |
|
my $js_file = $args->{'dir'}."/".$args->{'js'}; |
229 |
|
$log->info("creating '$js_file' with tree data"); |
230 |
|
$self->generate_js( |
231 |
|
file => $js_file, |
232 |
|
); |
233 |
|
|
234 |
|
if (! $args->{'nodes'}) { |
235 |
|
$log->warn("skipping node creation"); |
236 |
|
return $self; |
237 |
} |
} |
|
close(TEMPLATE); |
|
238 |
|
|
239 |
$log->info("creating '$html_file' with tree"); |
foreach my $mfn (keys %{$self->{'node_html'}}) { |
240 |
|
|
241 |
my $js_arr_file = $self->{'js'}; |
my $html_file = $args->{'dir'}."/".$args->{'nodes'}."/${mfn}.html"; |
|
$tmpl =~ s/\[%\s+js\s+%\]/$js_arr_file/gi; |
|
|
$tmpl =~ s/\[%\s+tree\s+%\]/$html/gi; |
|
242 |
|
|
243 |
open(HTML, ">", $html_file) || $log->logdie("can't open '$html_file': $!"); |
$log->debug("creating tree node $html_file"); |
|
print HTML $tmpl; |
|
|
close(HTML); |
|
244 |
|
|
245 |
$self->generate_js(); |
$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 |
} |
} |
297 |
my $v900 = $tree->[$level]->{'lookup_v900'}->($code) || $log->warn("can't lookup_v900($code)") && return; |
my $v900 = $tree->[$level]->{'lookup_v900'}->($code) || $log->warn("can't lookup_v900($code)") && return; |
298 |
$log->debug("# $level lookup_v900($code) = $v900"); |
$log->debug("# $level lookup_v900($code) = $v900"); |
299 |
|
|
300 |
my $term = $tree->[$level]->{'lookup_term'}->($code,$v900) || $log->warn("can't lookup_term($code,$v900)") && return; |
my $term = $tree->[$level]->{'lookup_term'}->($code,$v900) || $log->warn("can't lookup_term($code,$v900)") && next; |
301 |
$log->debug("# $level lookup_term($code,$v900) = $term"); |
$log->debug("# $level lookup_term($code,$v900) = $term"); |
302 |
|
|
303 |
my $mfn = $tree->[$level]->{'lookup_mfn'}->($code,$v900) || $log->warn("can't lookup_mfn($code,$v900)") && return; |
my $mfn = $tree->[$level]->{'lookup_mfn'}->($code,$v900) || $log->warn("can't lookup_mfn($code,$v900)") && next; |
304 |
$log->debug("# $level lookup_mfn($code,$v900) = $mfn"); |
$log->debug("# $level lookup_mfn($code,$v900) = $mfn"); |
305 |
|
|
306 |
$log->debug("$code -> $v900 : $term [$mfn]"); |
$log->debug("$code -> $v900 : $term [$mfn]"); |
320 |
|
|
321 |
my $mfn_link = "thes/$mfn.html"; |
my $mfn_link = "thes/$mfn.html"; |
322 |
if (-e "out/$mfn_link") { |
if (-e "out/$mfn_link") { |
323 |
|
$term =~ s# *\* *# <img src="img/crovoc.png" border="0">#; |
324 |
$html .= " " x $level . |
$html .= " " x $level . |
325 |
qq{<li>${link_start}<span id="o$mfn">${term}</span>${link_end}}. |
qq{<li>${link_start}<span id="o$mfn">${term}</span>${link_end}}. |
326 |
qq{ <a href="$mfn_link">»</a></li>\n}; |
qq{ <a href="$mfn_link" onClick="javascript:return popup(this);"><img src="img/listic.png" border="0"></a></li>\n}; |
327 |
} else { |
} else { |
328 |
$log->warn("file 'out/$mfn_link' doesn't exist, skipping"); |
$log->warn("file 'out/$mfn_link' doesn't exist, skipping"); |
329 |
} |
} |
334 |
my $style = $tree->[$level]->{'style'}; |
my $style = $tree->[$level]->{'style'}; |
335 |
|
|
336 |
$html .= " " x $level . |
$html .= " " x $level . |
337 |
qq{<a name="mfn$mfn"></a>\n <ul id="id$mfn"}. |
qq{<ul id="id$mfn"}. |
338 |
($style ? ' style="'.$style.'"' : ''). |
($style ? ' style="'.$style.'"' : ''). |
339 |
qq{>\n}; |
qq{>\n}; |
340 |
|
|
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 |
} |
} |
370 |
Generate JavaScript arrays C<show> and C<hide> used to toggle display of |
Generate JavaScript arrays C<show> and C<hide> used to toggle display of |
371 |
elements. |
elements. |
372 |
|
|
373 |
$tree->generate_js(); |
$tree->generate_js( |
374 |
|
file = "./out/tree-ids.js", |
375 |
|
); |
376 |
|
|
377 |
=cut |
=cut |
378 |
|
|
379 |
sub generate_js { |
sub generate_js { |
380 |
my $self = shift; |
my $self = shift; |
381 |
|
|
382 |
|
my $args = {@_}; |
383 |
|
|
384 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
385 |
|
|
386 |
my $js_file = $self->{'dir'}.'/'.$self->{'js'}; |
my $js_file = $args->{'file'}; |
387 |
|
$log->die("need file") unless ($args->{'file'}); |
388 |
|
|
389 |
$log->info("creating '$js_file' with arrays of shown and hidden ids"); |
$log->info("creating '$js_file' with arrays of shown and hidden ids"); |
390 |
|
|