11 |
|
|
12 |
=head1 VERSION |
=head1 VERSION |
13 |
|
|
14 |
Version 0.04 |
Version 0.08 |
15 |
|
|
16 |
=cut |
=cut |
17 |
|
|
18 |
our $VERSION = '0.04'; |
our $VERSION = '0.08'; |
19 |
|
|
20 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
21 |
|
|
47 |
code defined as code ref on format after field substitution to producing |
code defined as code ref on format after field substitution to producing |
48 |
output |
output |
49 |
|
|
50 |
|
There is one built-in filter called C<regex> which can be use like this: |
51 |
|
|
52 |
|
filter{regex(s/foo/bar/)} |
53 |
|
|
54 |
=item * |
=item * |
55 |
|
|
56 |
optional C<lookup{...}> will be then performed. See C<WebPAC::Lookups>. |
optional C<lookup{...}> will be then performed. See C<WebPAC::Lookups>. |
121 |
|
|
122 |
$log->warn("no prefix defined. please check that!") unless ($self->{'prefix'}); |
$log->warn("no prefix defined. please check that!") unless ($self->{'prefix'}); |
123 |
|
|
124 |
|
$log->debug("using lookup regex: ", $self->{lookup_regex}) if ($r && $l); |
125 |
|
|
126 |
|
if (! $self->{filter} || ! $self->{filter}->{regex}) { |
127 |
|
$log->debug("adding built-in filter regex"); |
128 |
|
$self->{filter}->{regex} = sub { |
129 |
|
my ($val, $regex) = @_; |
130 |
|
eval "\$val =~ $regex"; |
131 |
|
return $val; |
132 |
|
}; |
133 |
|
} |
134 |
|
|
135 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
136 |
} |
} |
137 |
|
|
157 |
|
|
158 |
$log->debug("data_structure rec = ", sub { Dumper($rec) }); |
$log->debug("data_structure rec = ", sub { Dumper($rec) }); |
159 |
|
|
160 |
$log->logdie("need unique ID (mfn) in field 000 of record ", sub { Dumper($rec) } ) unless (defined($rec->{'000'})); |
$log->logdie("need unique ID (mfn) in field 000 of record " . Dumper($rec) ) unless (defined($rec->{'000'})); |
161 |
|
|
162 |
my $id = $rec->{'000'}->[0] || $log->logdie("field 000 isn't array!"); |
my $id = $rec->{'000'}->[0] || $log->logdie("field 000 isn't array!"); |
163 |
|
|
170 |
$log->debug("cache miss, creating"); |
$log->debug("cache miss, creating"); |
171 |
} |
} |
172 |
|
|
|
undef $self->{'currnet_filename'}; |
|
|
undef $self->{'headline'}; |
|
|
|
|
173 |
my @sorted_tags; |
my @sorted_tags; |
174 |
if ($self->{tags_by_order}) { |
if ($self->{tags_by_order}) { |
175 |
@sorted_tags = @{$self->{tags_by_order}}; |
@sorted_tags = @{$self->{tags_by_order}}; |
194 |
$log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH'); |
$log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH'); |
195 |
$format = $tag->{'value'} || $tag->{'content'}; |
$format = $tag->{'value'} || $tag->{'content'}; |
196 |
|
|
|
$log->debug("format: $format"); |
|
|
|
|
197 |
my @v; |
my @v; |
198 |
if ($self->{'lookup_regex'} && $format =~ $self->{'lookup_regex'}) { |
if ($self->{'lookup_regex'} && $format =~ $self->{'lookup_regex'}) { |
199 |
@v = $self->fill_in_to_arr($rec,$format); |
@v = $self->fill_in_to_arr($rec,$format); |
200 |
} else { |
} else { |
201 |
@v = $self->parse_to_arr($rec,$format); |
@v = $self->parse_to_arr($rec,$format); |
202 |
} |
} |
203 |
next if (! @v); |
if (! @v) { |
204 |
|
$log->debug("$field <",$self->{tag},"> format: $format no values"); |
205 |
|
# next; |
206 |
|
} else { |
207 |
|
$log->debug("$field <",$self->{tag},"> format: $format values: ", join(",", @v)); |
208 |
|
} |
209 |
|
|
210 |
if ($tag->{'sort'}) { |
if ($tag->{'sort'}) { |
211 |
@v = $self->sort_arr(@v); |
@v = $self->sort_arr(@v); |
228 |
|
|
229 |
foreach my $type (@types) { |
foreach my $type (@types) { |
230 |
# append to previous line? |
# append to previous line? |
231 |
$log->debug("type: $type ",sub { join(" ",@v) }, $row->{'append'} || 'no append'); |
$log->debug("tag $field / $type [",sub { join(",",@v) }, "] ", $row->{'append'} || 'no append'); |
232 |
if ($tag->{'append'}) { |
if ($tag->{'append'}) { |
233 |
|
|
234 |
# I will delimit appended part with |
# I will delimit appended part with |
291 |
|
|
292 |
my $text = $webpac->parse($rec,'eval{"v901^a" eq "Deskriptor"}descriptor: v250^a', $i); |
my $text = $webpac->parse($rec,'eval{"v901^a" eq "Deskriptor"}descriptor: v250^a', $i); |
293 |
|
|
294 |
|
Filters are implemented here. While simple form of filters looks like this: |
295 |
|
|
296 |
|
filter{name_of_filter} |
297 |
|
|
298 |
|
but, filters can also have variable number of parametars like this: |
299 |
|
|
300 |
|
filter{name_of_filter(param,param,param)} |
301 |
|
|
302 |
=cut |
=cut |
303 |
|
|
304 |
|
my $warn_once; |
305 |
|
|
306 |
sub parse { |
sub parse { |
307 |
my $self = shift; |
my $self = shift; |
308 |
|
|
320 |
|
|
321 |
my @out; |
my @out; |
322 |
|
|
323 |
$log->debug("format: $format"); |
$log->debug("format: $format [$i]"); |
324 |
|
|
325 |
my $eval_code; |
my $eval_code; |
326 |
# remove eval{...} from beginning |
# remove eval{...} from beginning |
330 |
# remove filter{...} from beginning |
# remove filter{...} from beginning |
331 |
$filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); |
$filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); |
332 |
|
|
333 |
|
# did we found any (att all) field from format in row? |
334 |
|
my $found_any; |
335 |
|
# prefix before first field which we preserve it $found_any |
336 |
my $prefix; |
my $prefix; |
337 |
my $all_found=0; |
|
338 |
|
my $f_step = 1; |
339 |
|
|
340 |
while ($format =~ s/^(.*?)(v|s)(\d+)(?:\^(\w))?//s) { |
while ($format =~ s/^(.*?)(v|s)(\d+)(?:\^(\w))?//s) { |
341 |
|
|
342 |
my $del = $1 || ''; |
my $del = $1 || ''; |
343 |
$prefix ||= $del if ($all_found == 0); |
$prefix = $del if ($f_step == 1); |
344 |
|
|
345 |
|
my $fld_type = lc($2); |
346 |
|
|
347 |
# repeatable index |
# repeatable index |
348 |
my $r = $i; |
my $r = $i; |
349 |
$r = 0 if (lc("$2") eq 's'); |
if ($fld_type eq 's') { |
350 |
|
if ($found_any->{'v'}) { |
351 |
|
$r = 0; |
352 |
|
} else { |
353 |
|
return; |
354 |
|
} |
355 |
|
} |
356 |
|
|
357 |
my $found = 0; |
my $found = 0; |
358 |
my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found); |
my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found); |
359 |
|
|
360 |
if ($found) { |
if ($found) { |
361 |
push @out, $del; |
$found_any->{$fld_type} += $found; |
362 |
|
|
363 |
|
# we will skip delimiter before first occurence of field! |
364 |
|
push @out, $del unless($found_any->{$fld_type} == 1); |
365 |
push @out, $tmp; |
push @out, $tmp; |
|
$all_found += $found; |
|
366 |
} |
} |
367 |
|
$f_step++; |
368 |
} |
} |
369 |
|
|
370 |
return if (! $all_found); |
# test if any fields found? |
371 |
|
return if (! $found_any->{'v'} && ! $found_any->{'s'}); |
372 |
|
|
373 |
my $out = join('',@out); |
my $out = join('',@out); |
374 |
|
|
388 |
return if (! $self->_eval($eval)); |
return if (! $self->_eval($eval)); |
389 |
} |
} |
390 |
|
|
391 |
if ($filter_name && $self->{'filter'}->{$filter_name}) { |
if ($filter_name) { |
392 |
$log->debug("about to filter{$filter_name} format: $out"); |
my @filter_args; |
393 |
$out = $self->{'filter'}->{$filter_name}->($out); |
if ($filter_name =~ s/(\w+)\((.*)\)/$1/) { |
394 |
return unless(defined($out)); |
@filter_args = split(/,/, $2); |
395 |
$log->debug("filter result: $out"); |
} |
396 |
|
if ($self->{'filter'}->{$filter_name}) { |
397 |
|
$log->debug("about to filter{$filter_name} format: $out with arguments: ", join(",", @filter_args)); |
398 |
|
unshift @filter_args, $out; |
399 |
|
$out = $self->{'filter'}->{$filter_name}->(@filter_args); |
400 |
|
return unless(defined($out)); |
401 |
|
$log->debug("filter result: $out"); |
402 |
|
} elsif (! $warn_once->{$filter_name}) { |
403 |
|
$log->warn("trying to use undefined filter $filter_name"); |
404 |
|
$warn_once->{$filter_name}++; |
405 |
|
} |
406 |
} |
} |
407 |
|
|
408 |
return $out; |
return $out; |
482 |
} |
} |
483 |
|
|
484 |
my $found = 0; |
my $found = 0; |
485 |
|
my $just_single = 1; |
486 |
|
|
487 |
my $eval_code; |
my $eval_code; |
488 |
# remove eval{...} from beginning |
# remove eval{...} from beginning |
494 |
|
|
495 |
# do actual replacement of placeholders |
# do actual replacement of placeholders |
496 |
# repeatable fields |
# repeatable fields |
497 |
$format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges; |
if ($format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges) { |
498 |
|
$just_single = 0; |
499 |
|
} |
500 |
|
|
501 |
# non-repeatable fields |
# non-repeatable fields |
502 |
$format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found)/ges; |
if ($format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found)/ges) { |
503 |
|
return if ($i > 0 && $just_single); |
504 |
|
} |
505 |
|
|
506 |
if ($found) { |
if ($found) { |
507 |
$log->debug("format: $format"); |
$log->debug("format: $format"); |
518 |
# do we have lookups? |
# do we have lookups? |
519 |
if ($self->{'lookup'}) { |
if ($self->{'lookup'}) { |
520 |
if ($self->{'lookup'}->can('lookup')) { |
if ($self->{'lookup'}->can('lookup')) { |
521 |
return $self->{'lookup'}->lookup($format); |
my @lookup = $self->{lookup}->lookup($format); |
522 |
|
$log->debug("lookup $format", join(", ", @lookup)); |
523 |
|
return @lookup; |
524 |
} else { |
} else { |
525 |
$log->warn("Have lookup object but can't invoke lookup method"); |
$log->warn("Have lookup object but can't invoke lookup method"); |
526 |
} |
} |
556 |
my $i = 0; |
my $i = 0; |
557 |
my @arr; |
my @arr; |
558 |
|
|
559 |
while (my @v = $self->fill_in($rec,$format_utf8,$i++)) { |
while (my $v = $self->fill_in($rec,$format_utf8,$i++)) { |
560 |
push @arr, @v; |
push @arr, $v; |
561 |
} |
} |
562 |
|
|
563 |
$log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); |
$log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); |
603 |
if ($$rec->{$f}->[$i] =~ /HASH/o) { |
if ($$rec->{$f}->[$i] =~ /HASH/o) { |
604 |
my $out; |
my $out; |
605 |
foreach my $k (keys %{$$rec->{$f}->[$i]}) { |
foreach my $k (keys %{$$rec->{$f}->[$i]}) { |
606 |
$out .= $$rec->{$f}->[$i]->{$k}." "; |
my $v = $$rec->{$f}->[$i]->{$k}; |
607 |
|
$out .= "$v " if ($v); |
608 |
} |
} |
609 |
return $out; |
return $out; |
610 |
} else { |
} else { |