Skip to content

Instantly share code, notes, and snippets.

@fredericd
Created September 19, 2015 10:09
Show Gist options
  • Save fredericd/d74bcd353607899c89f6 to your computer and use it in GitHub Desktop.
Save fredericd/d74bcd353607899c89f6 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
package MARC::Moose::Parser::Horizon;
use Moose;
use Modern::Perl;
extends 'MARC::Moose::Parser';
use MARC::Moose::Record;
use MARC::Moose::Field::Control;
use MARC::Moose::Field::Std;
override 'parse' => sub {
my ($self, $lines) = @_;
my $record = MARC::Moose::Record->new();
while (@$lines) {
my $line = shift @$lines;
if ( $line =~ /^ +([0-9]{3}) (.*)$/ ) {
my $tag = $1;
my @repets = ($2);
while ( $line = shift @$lines ) {
if ( $line =~ /^ (.*)$/ ) {
$repets[-1] .= " $1";
}
elsif ( $line =~ /^ (.*)$/ ) {
push @repets, $1;
}
else {
unshift @$lines, $line;
last;
}
}
for my $repet (@repets) {
if ( $tag eq '000' ) {
say "$repet :: ", length($repet);
$record->_leader($repet);
}
elsif ( $tag lt '010' ) {
$record->append( MARC::Moose::Field::Control->new(
tag => $tag, value => $repet
) );
}
elsif ( $repet =~ /\x1f/ ) {
my @values = split /\x1f/, $repet;
@values = grep { $_ } @values;
my @subf = map {
[ substr($_, 0, 1), substr($_, 1) ];
} @values;
$record->append( MARC::Moose::Field::Std->new(
tag => $tag, subf => \@subf
) );
}
else { # Field without letter => $a
$record->append( MARC::Moose::Field::Std->new(
tag => $tag, subf => [ [ a => $repet ] ]
) );
}
}
}
}
return $record;
};
package MARC::Moose::Reader::File::Horizon;
use Moose;
use Modern::Perl;
use MARC::Moose::Record;
use MARC::Moose::Writer;
use MARC::Moose::Parser::Horizon;
with 'MARC::Moose::Reader::File';
has '+parser' => (
default => sub {
my $parser = MARC::Moose::Parser::Horizon->new();
return $parser;
}
);
sub read {
my $self = shift;
$self->count( $self->count + 1);
my $fh = $self->{fh};
return if eof($fh);
while (<$fh>) {
last if /^Horizon Bib/;
}
return if eof($fh);
my @lines;
while (<$fh>) {
last if /^ [a-zA-Z]/;
chop; chop;
next unless /^ /;
push @lines, $_;
}
$self->parser->parse( \@lines );
}
package Main;
my $reader = MARC::Moose::Reader::File::Horizon->new(
file => 'horizon.txt'
);
my $writer = MARC::Moose::Writer->new(
fh => IO::File->new('horizon.mrc', '>'),
formater => MARC::Moose::Formater::Iso2709->new(),
);
while ( my $record = $reader->read() ) {
print $record->as('Text');
$writer->write($record);
}
@fredericd
Copy link
Author

Read a horizon.txt file. Output the text version of file, and create an horizon.mrc file in ISO2709.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment