ADD: changed to perl only bibtex parser + multi page support

This commit is contained in:
Dominik Meyer 2021-02-25 19:35:02 +01:00
parent fccff5c467
commit 20b694ec76
No known key found for this signature in database
GPG Key ID: B4C312B600606B64
1 changed files with 45 additions and 79 deletions

View File

@ -3,10 +3,10 @@ package HiD::Generator::BibtexPage;
use Moose;
with 'HiD::Generator';
use HiD::Page;
use Text::BibTeX;
use Text::BibTeX::Bib;
use BibTeX::Parser;
use IO::File;
our $VERSION = "0.3";
our $VERSION = "0.4";
# a map of bibtex publication item names to real/speaking names
@ -23,7 +23,7 @@ my $type_map = {
=method generate
reads the configured bibtex file and generates an html version
reads the configured bibtex files and generates an html version
=cut
@ -31,94 +31,60 @@ sub generate {
my( $self , $site ) = @_;
return unless $site->config->{bibtex_page}{generate};
my $input_file = $site->config->{bibtex_page}{layout}
or die "Must define bibtex_page.layout in config if bibtex_page.generate is enabled";
die("no bibtex pages defined") unless $site->config->{bibtex_page}{pages};
my $bibtex_file = $site->config->{bibtex_page}{bibtex}
or die "Must define bibtex_page.bibtex in config if bibtex_page.generate is enabled";
for my $p (keys %{$site->config->{bibtex_page}{pages}})
{
if (! -e $site->config->{bibtex_page}{bibtex}) {
die "bibtex_page.bibtex:".$site->config->{bibtex_page}{bibtex}." file must exist\n";
}
my $input_file = $site->config->{bibtex_page}{pages}{$p}{layout}
or die "Must define bibtex_page.layout in config if bibtex_page.generate is enabled";
my $url = $site->config->{bibtex_page}{url} // 'publications/';
my $bibtex_file = $site->config->{bibtex_page}{pages}{$p}{bibtex}
or die "Must define bibtex_page.bibtex in config if bibtex_page.generate is enabled";
my $destination = $site->config->{bibtex_page}{destination} // $site->destination;
if (! -e $site->config->{bibtex_page}{pages}{$p}{bibtex}) {
die "bibtex_page.bibtex:".$site->config->{bibtex_page}{bibtex}." file must exist\n";
}
$self->_create_destination_directory_if_needed( $destination );
my $url = $site->config->{bibtex_page}{pages}{$p}{url} // 'publications/';
# here we need to parse the bibtex file and generate the list
my @publications;
my $bibfile = new Text::BibTeX::File($site->config->{bibtex_page}{bibtex});
$bibfile->set_structure ('Bib',sortby => 'year');
while (my $entry = new Text::BibTeX::Entry $bibfile) {
next unless $entry->parse_ok;
my %pub;
$pub{type}=$type_map->{$entry->type};
my $destination = $site->config->{bibtex_page}{pages}{$p}{destination} // $site->destination;
my @names = $entry->names ('author');
my @last;
for my $n (@names){
push(@last, $n->part('last'));
}
$pub{author}=\@last;
if ($entry->type eq "mastersthesis" || $entry->type eq "phdthesis") {
$pub{title}=$entry->get('title') . ", " . $entry->get('school') . ", " . $entry->get('address');
} elsif($entry->type eq "inproceedings") {
$pub{title}=$entry->get('title') . ", " .$entry->get('booktitle') . ", ". $entry->get('address');
} elsif($entry->type eq "misc") {
$pub{title}=$entry->get('title');
if (defined($entry->get('howpublished')) && length($entry->get('howpublished'))>0) {
$pub{title}.="," . $entry->get('howpublished');
}
if (defined($entry->get('address')) && length($entry->get('address'))>0) {
$pub{title}.="," . $entry->get('address');
}
} elsif($entry->type eq "inbook") {
$pub{title} = "Chap: " . $entry->get('chapter') . ", Book: " . $entry->get('title');
$pub{title} .= ", Editor: " . $entry->get('editor') unless !defined($entry->get('editor'));
$pub{title} .= ", " . $entry->get('publisher') unless !defined($entry->get('publisher'));
} elsif($entry->type eq "incollection") {
$pub{title} = $entry->get('title') .", " . $entry->get('booktitle');
$pub{title} .= " Vol. " . $entry->get('volume') unless !defined($entry->get('volume'));
$pub{title} .= ", Editor: " . $entry->get('editor') unless !defined($entry->get('editor'));
$pub{title} .= ", " . $entry->get('publisher') unless !defined($entry->get('publisher'));
} elsif($entry->type eq "article") {
$pub{title} = $entry->get('title') .", " . $entry->get('journal');
$pub{title} .= " Issue " . $entry->get('number') unless !defined($entry->get('number'));
$pub{title} .= " Vol. " . $entry->get('volume') unless !defined($entry->get('volume'));
$pub{title} .= ", " . $entry->get('publisher') unless !defined($entry->get('publisher'));
}
else {
$pub{title}=$entry->get('title');
}
$pub{year}=$entry->get('year');
$pub{month}=$entry->get('month');
$pub{day}=$entry->get('day');
$pub{url}=$entry->get('url');
$pub{source}=$entry->print_s;
$pub{id}=$entry->key;
$self->_create_destination_directory_if_needed( $destination );
# here we need to parse the bibtex file and generate the list
my @publications;
my $fh = IO::File->new($site->config->{bibtex_page}{pages}{$p}{bibtex});
my $bibfile = BibTeX::Parser->new($fh);
my @entries;
while (my $entry = $bibfile->next ) {
if ($entry->parse_ok) {
push(@entries, $entry);
}
}
push(@publications, \%pub);
}
# sort publications by year
@publications=sort { $b->{year} <=> $a->{year} } @publications;
@publications=sort { $b->{year} <=> $a->{year} } @entries;
# create the new page
my $page = HiD::Page->new({
dest_dir => $destination ,
hid => $site ,
url => $url ,
input_filename => $input_file ,
layouts => $site->layouts ,
});
$page->metadata->{publications} = \@publications;
# create the new page
my $page = HiD::Page->new({
dest_dir => $destination ,
hid => $site ,
url => $url ,
input_filename => $input_file ,
layouts => $site->layouts ,
});
$page->metadata->{publications} = \@publications;
$site->add_input( "Publication" => 'page' );
$site->add_object( $page );
$site->add_input( "Publication" => 'page' );
$site->add_object( $page );
$site->INFO( "* Injected Bibtex page");
$site->INFO( "* Injected Bibtex page $p");
}
}
__PACKAGE__->meta->make_immutable;