--- trunk2/lib/WebPAC.pm 2004/09/14 17:07:59 441 +++ trunk2/lib/WebPAC.pm 2004/10/30 23:04:37 560 @@ -36,12 +36,19 @@ config_file => 'name.conf', code_page => 'ISO-8859-2', low_mem => 1, + filter => { + 'lower' => sub { lc($_[0]) }, + }, ); Default C is C. Default is not to use C options (see L below). +There is optinal parametar C which specify different filters which +can be applied using C notation. +Same filters can be used in Template Toolkit files. + This method will also read configuration files C (used by indexer and Web font-end) and configuration file specified by C @@ -106,9 +113,7 @@ # create Template toolkit instance $self->{'tt'} = Template->new( INCLUDE_PATH => ($self->{'global_config_file'}->{'output_template'} || './output_template'), -# FILTERS => { -# 'foo' => \&foo_filter, -# }, + FILTERS => $self->{'filter'}, EVAL_PERL => 1, ); @@ -138,6 +143,8 @@ $self->{'db'} = $db; } + $log->debug("filters defined: ",Dumper($self->{'filter'})); + return $self; } @@ -209,6 +216,8 @@ if (my $s = $self->{'start_mfn'}) { $log->info("skipping to MFN $s"); $startmfn = $s; + } else { + $self->{'start_mfn'} = $startmfn; } $maxmfn = $startmfn + $self->{limit_mfn} if ($self->{limit_mfn}); @@ -264,7 +273,7 @@ } - $self->{'current_mfn'} = $startmfn; + $self->{'current_mfn'} = -1; $self->{'last_pcnt'} = 0; $log->debug("max mfn: $maxmfn"); @@ -287,7 +296,15 @@ my $log = $self->_get_logger(); - my $mfn = $self->{'current_mfn'}++ || $log->logconfess("it seems that you didn't load database!"); + $log->logconfess("it seems that you didn't load database!") unless ($self->{'current_mfn'}); + + if ($self->{'current_mfn'} == -1) { + $self->{'current_mfn'} = $self->{'start_mfn'}; + } else { + $self->{'current_mfn'}++; + } + + my $mfn = $self->{'current_mfn'}; if ($mfn > $self->{'max_mfn'}) { $self->{'current_mfn'} = $self->{'max_mfn'}; @@ -304,6 +321,19 @@ } } +=head2 mfn + +Returns current record number (MFN). + + print $webpac->mfn; + +=cut + +sub mfn { + my $self = shift; + return $self->{'current_mfn'}; +} + =head2 progress_bar Draw progress bar on STDERR. @@ -329,15 +359,17 @@ $self->{'last_pcnt'} ||= 1; - my $p = int($curr * 100 / $max); + my $p = int($curr * 100 / $max) || 1; # reset on re-run if ($p < $self->{'last_pcnt'}) { $self->{'last_pcnt'} = $p; $self->{'last_t'} = time(); - $self->{'last_curr'} = 1; + $self->{'last_curr'} = undef; } + $self->{'last_t'} ||= time(); + if ($p != $self->{'last_pcnt'}) { my $last_curr = $self->{'last_curr'} || $curr; @@ -537,6 +569,8 @@ # iteration (for repeatable fields) my $i = shift || 0; + $log->logdie("infitite loop in format $format") if ($i > ($self->{'max_mfn'} || 9999)); + # FIXME remove for speedup? $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); @@ -550,8 +584,15 @@ # remove eval{...} from beginning $eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); + my $filter_name; + # remove filter{...} from beginning + $filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); + # do actual replacement of placeholders + # repeatable fields $format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges; + # non-repeatable fields + $format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found)/ges; if ($found) { $log->debug("format: $format"); @@ -559,6 +600,12 @@ my $eval = $self->fill_in($rec,$eval_code,$i); return if (! $self->_eval($eval)); } + if ($filter_name && $self->{'filter'}->{$filter_name}) { + $log->debug("filter '$filter_name' for $format"); + $format = $self->{'filter'}->{$filter_name}->($format); + return unless(defined($format)); + $log->debug("filter result: $format"); + } # do we have lookups? if ($format =~ /$LOOKUP_REGEX/o) { $log->debug("format '$format' has lookup"); @@ -651,16 +698,24 @@ # remove eval{...} from beginning $eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); + my $filter_name; + # remove filter{...} from beginning + $filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); + my $prefix; my $all_found=0; - while ($format =~ s/^(.*?)v(\d+)(?:\^(\w))?//s) { + while ($format =~ s/^(.*?)(v|s)(\d+)(?:\^(\w))?//s) { my $del = $1 || ''; $prefix ||= $del if ($all_found == 0); + # repeatable index + my $r = $i; + $r = 0 if (lc("$2") eq 's'); + my $found = 0; - my $tmp = $self->get_data(\$rec,$2,$3,$i,\$found); + my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found); if ($found) { push @out, $del; @@ -684,10 +739,17 @@ } if ($eval_code) { - my $eval = $self->fill_in($rec,$eval_code,$i); - $log->debug("about to eval{",$eval,"} format: $out"); + my $eval = $self->fill_in($rec,$eval_code,$i) || return; + $log->debug("about to eval{$eval} format: $out"); return if (! $self->_eval($eval)); } + + if ($filter_name && $self->{'filter'}->{$filter_name}) { + $log->debug("about to filter{$filter_name} format: $out"); + $out = $self->{'filter'}->{$filter_name}->($out); + return unless(defined($out)); + $log->debug("filter result: $out"); + } return $out; } @@ -873,7 +935,7 @@ # delimiter (or ,) my $d = $tag->{'delimiter'}; # default delimiter - $d ||= ", "; + $d ||= " "; my $last = pop @{$row->{$type}}; $d = "" if (! $last); @@ -1045,7 +1107,7 @@ $log->debug("eval: ",$code," [",$ret,"]"); - return $ret || 0; + return $ret || undef; } =head2 _sort_by_order