| 1 | package Catmandu::Importer::BibTeX; | ||
| 2 | |||
| 3 | 1 | our $VERSION = '0.21'; | |
| 4 | |||
| 5 | 1 | ▶ | use namespace::clean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Catmandu::Sane; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use BibTeX::Parser; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | with 'Catmandu::Importer'; | |
| 11 | |||
| 12 | 6 | ▶ | sub _contributor_name { |
|
Subroutine
_contributor_name: called CC=1 SCAR=0.0
| |||
| 13 | 6 | my ($self, $c) = @_; | |
| 14 | 6 | my $name = $c->to_string; | |
| 15 | 6 | $name =~ tr/\{\}//d; | |
| 16 | 6 | $name; | |
| 17 | } | ||
| 18 | |||
| 19 | 1 | ▶ | sub generator { |
|
Subroutine
generator: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 20 | 1 | my ($self) = @_; | |
| 21 | 1 | my $fh = $self->fh; | |
| 22 | sub { | ||
| 23 | my $parser = BibTeX::Parser->new($fh); | ||
| 24 | while (my $entry = $parser->next) { | ||
| 25 | my $bib = {}; | ||
| 26 | if ($entry->parse_ok) { | ||
| 27 | $bib->{type} = lc $entry->type; | ||
| 28 | |||
| 29 | $bib->{_citekey} = $entry->key; | ||
| 30 | |||
| 31 | if (my @authors = $entry->cleaned_author) { | ||
| 32 | $bib->{author} | ||
| 33 | = [map {$self->_contributor_name($_)} @authors]; | ||
| 34 | } | ||
| 35 | if (my @editors = $entry->cleaned_editor) { | ||
| 36 | $bib->{editor} | ||
| 37 | = [map {$self->_contributor_name($_)} @editors]; | ||
| 38 | } | ||
| 39 | |||
| 40 | for my $field ($entry->fieldlist) { | ||
| 41 | next if $field =~ /^type|author|editor$/; | ||
| 42 | $bib->{$field} = $entry->cleaned_field($field); | ||
| 43 | } | ||
| 44 | |||
| 45 | return $bib; | ||
| 46 | } | ||
| 47 | else { | ||
| 48 | Catmandu::Error->throw($entry->error); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | 1 | } | |
| 54 | |||
| 55 | =head1 NAME | ||
| 56 | |||
| 57 | Catmandu::Importer::BibTeX - a BibTeX importer | ||
| 58 | |||
| 59 | =head1 SYNOPSIS | ||
| 60 | |||
| 61 | use Catmandu::Importer::BibTeX; | ||
| 62 | |||
| 63 | my $importer = Catmandu::Importer::BibTeX->new(file => "/foo/bar.bib"); | ||
| 64 | |||
| 65 | my $n = $importer->each(sub { | ||
| 66 | my $hashref = $_[0]; | ||
| 67 | # ... | ||
| 68 | }); | ||
| 69 | |||
| 70 | =head1 DESCRIPTION | ||
| 71 | |||
| 72 | Citekey and reference type are imported as field C<_citekey> and C<_type>, | ||
| 73 | respectively. | ||
| 74 | |||
| 75 | =head1 METHODS | ||
| 76 | |||
| 77 | All methods of L<Catmandu::Importer> and by this L<Catmandu::Iterable> are | ||
| 78 | inherited. | ||
| 79 | |||
| 80 | =head1 SEE ALSO | ||
| 81 | |||
| 82 | L<Catmandu::Exporter::BibTeX>, | ||
| 83 | L<BibTeX::Parser> | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | 1 | 1; | |