/[webpac]/trunk2/lib/WebPAC/Tree.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk2/lib/WebPAC/Tree.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 571 by dpavlin, Tue Oct 19 17:43:52 2004 UTC revision 572 by dpavlin, Mon Nov 1 14:55:16 2004 UTC
# Line 6  use strict; Line 6  use strict;
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    
# Line 14  WebPAC::Tree - create tree from lookup d Line 15  WebPAC::Tree - create tree from lookup d
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:
# Line 33  of tree individually (so you can limit d Line 34  of tree individually (so you can limit d
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]} },
# Line 45  of tree individually (so you can limit d Line 47  of tree individually (so you can limit d
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
# Line 105  Returns children for next iteration and Line 108  Returns children for next iteration and
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
# Line 157  Create output files from tree object Line 169  Create output files from tree object
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    
# Line 166  created (think of it as C<public_html>). Line 180  created (think of it as C<public_html>).
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    
# Line 180  sub output { Line 198  sub output {
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    
# Line 192  sub output { Line 210  sub output {
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    
# Line 313  sub unroll { Line 349  sub unroll {
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                  }                  }

Legend:
Removed from v.571  
changed lines
  Added in v.572

  ViewVC Help
Powered by ViewVC 1.1.26