--- trunk2/lib/WebPAC.pm 2004/06/17 12:05:01 367 +++ trunk2/lib/WebPAC.pm 2004/06/17 20:44:45 371 @@ -7,6 +7,7 @@ use Text::Iconv; use Config::IniFiles; use XML::Simple; +use Template; use Data::Dumper; @@ -60,7 +61,7 @@ # read global.conf # - $self->{global_config_file} = new Config::IniFiles( -file => 'global.conf' ) || croak "can't open 'global.conf'"; + my $config = new Config::IniFiles( -file => 'global.conf' ) || croak "can't open 'global.conf'"; # read global config parametars foreach my $var (qw( @@ -70,8 +71,9 @@ dbi_passwd show_progress my_unac_filter + output_template )) { - $self->{global_config}->{$var} = $self->{global_config_file}->val('global', $var); + $self->{'global_config'}->{$var} = $config->val('global', $var); } # @@ -80,7 +82,18 @@ $self->{indexer_config_file} = new Config::IniFiles( -file => $self->{config_file} ) || croak "can't open '$self->{config_file}'"; + # create UTF-8 convertor for import_xml files $self->{'utf2cp'} = Text::Iconv->new('UTF-8' ,$self->{'code_page'}); + + # create Template toolkit instance + $self->{'tt'} = Template->new( + INCLUDE_PATH => ($self->{'global_config_file'}->{'output_template'} || './output_template'), +# FILTERS => { +# 'foo' => \&foo_filter, +# }, + EVAL_PERL => 1, + ); + return $self; } @@ -220,7 +233,7 @@ $self->{'tag'} = $type2tag{$type_base}; - print STDERR "using type ",$self->{'type'}," tag ",$self->{'tag'},"\n" if ($self->{'debug'}); + print STDERR "using type '",$self->{'type'},"' tag <",$self->{'tag'},">\n" if ($self->{'debug'}); my $f = "./import_xml/".$self->{'type'}.".xml"; confess "import_xml file '$f' doesn't exist!" if (! -e "$f"); @@ -232,8 +245,6 @@ ForceContent => 1 ); - print Dumper($self->{'import_xml'}); - } =head2 create_lookup @@ -338,6 +349,19 @@ =cut +# internal function to eval code +sub _eval { + my $self = shift; + + my $code = shift || return; + no strict 'subs'; + my $ret = eval $code; + if ($@) { + print STDERR "problem with eval code [$code]: $@\n"; + } + return $ret; +} + sub fill_in { my $self = shift; @@ -361,10 +385,11 @@ if ($found) { if ($eval_code) { my $eval = $self->fill_in($rec,$eval_code,$i); - return if (! eval $eval); + return if (! $self->_eval($eval)); } # do we have lookups? if ($format =~ /\[[^\[\]]+\]/o) { +print "## probable lookup: $format\n"; return $self->lookup($format); } else { return $format; @@ -391,6 +416,7 @@ if ($tmp =~ /\[[^\[\]]+\]/o) { my @in = ( $tmp ); +print "## lookup $tmp\n"; my @out; while (my $f = shift @in) { if ($f =~ /\[([^\[\]]+)\]/) { @@ -471,7 +497,7 @@ if ($eval_code) { my $eval = $self->fill_in($rec,$eval_code,$i); - return if (! eval $eval); + return if (! $self->_eval($eval)); } return $out; @@ -508,7 +534,7 @@ Create in-memory data structure which represents layout from C. It is used later to produce output. - my $ds = $webpac->data_structure($rec); + my @ds = $webpac->data_structure($rec); =cut @@ -539,7 +565,7 @@ $self->{tags_by_order} = \@sorted_tags; } - my $ds; + my @ds; foreach my $field (@sorted_tags) { @@ -561,12 +587,42 @@ } } - push @{$ds->{$field}}, $row if ($row); + if ($row) { + $row->{'tag'} = $field; + push @ds, $row; + } } - print "data_structure => ",Dumper($ds); + return @ds; + +} + +=head2 output + +Create output from in-memory data structure using Template Toolkit template. + +my $text = $webpac->output( template => 'text.tt', data => @ds ); + +=cut + +sub output { + my $self = shift; + + my $args = {@_}; + + confess("need template name") if (! $args->{'template'}); + confess("need data array") if (! $args->{'data'}); + my $out; + + $self->{'tt'}->process( + $args->{'template'}, + $args, + \$out + ) || confess $self->{'tt'}->error(); + + return $out; } 1;