App-PDFLibrarian
view release on metacpan or search on metacpan
lib/App/PDFLibrarian/BibTeX.pm view on Meta::CPAN
}
sub format_bib {
my ($opts, @bibentries) = @_;
# check options
$opts->{max_authors} = 0 unless defined($opts->{max_authors});
$opts->{only_first_author} = 0 unless defined($opts->{only_first_author});
# format BibTeX entries
my @fmtbibentries;
foreach my $bibentry (@bibentries) {
# create a copy of BibTeX entry
$bibentry = $bibentry->clone();
# merge BibTeX field names which differ by 's', e.g. 'keyword' and 'keywords'
foreach my $bibfield ($bibentry->fieldlist()) {
if ($bibentry->exists($bibfield) && $bibentry->exists($bibfield . "s")) {
my $bibfieldvalue = $bibentry->get($bibfield);
my $bibfieldsvalue = $bibentry->get($bibfield . "s");
if ($bibfieldvalue eq "") {
$bibfieldvalue = $bibfieldsvalue;
} elsif ($bibfieldsvalue ne "") {
$bibfieldvalue .= ", " . $bibfieldsvalue;
}
$bibentry->set($bibfield, $bibfieldvalue);
$bibentry->delete($bibfield . "s");
}
}
# remove wrapping braces
foreach my $bibfield ($bibentry->fieldlist()) {
my $bibfieldvalue = $bibentry->get($bibfield);
while ($bibfieldvalue =~ /^(\{(?:[^{}]++|(?1))*\})$/) {
$bibfieldvalue = substr($1, 1, -1);
};
$bibentry->set($bibfield, $bibfieldvalue);
}
# replace accented characters with LaTeX accents
foreach my $bibfield ($bibentry->fieldlist()) {
my $bibfieldvalue = $bibentry->get($bibfield);
$bibfieldvalue = replace_accents_with_latex($bibfieldvalue);
$bibentry->set($bibfield, $bibfieldvalue);
}
# uniformly format authors, editors, and collaborations
foreach my $bibfield (qw(author editor collaboration)) {
if ($bibentry->exists($bibfield)) {
# determine author format
my $authorformat;
if ($bibfield eq "collaboration") {
$authorformat = new Text::BibTeX::NameFormat("l");
$authorformat->set_text(BTN_LAST, "{", "}", undef, undef);
} else {
$authorformat = new Text::BibTeX::NameFormat("vljf");
$authorformat->set_text(BTN_LAST, "{", "}", undef, undef);
$authorformat->set_text(BTN_FIRST, undef, undef, undef, ".");
$authorformat->set_options(BTN_FIRST, 1, BTJ_FORCETIE, BTJ_SPACE);
}
# iterate over authors
my @authors = $bibentry->split($bibfield);
foreach my $author (@authors) {
# sanitise author string
$author =~ s/([^\\])~/$1 /g;
$author =~ s/\.\s-/.-/g;
$author =~ s/\bet\sal\.?/others/;
if ($author ne "others") {
# format author
$author = Text::BibTeX::Name->new($author);
$author = $authorformat->apply($author);
# use braces around special character commands
$author =~ s/\\(\W)(\w)/\{\\$1$2\}/g;
$author =~ s/\\(\w+)\s+(\w)/\\$1\{$2\}/g;
# remove duplicate braces
my @parts = split(",", $author);
foreach my $part (@parts) {
while ($part =~ s{\{\{(.*?(\{(?:(?>[^{}]+)|(?2))*\}.*?)*)\}\}}{\{$1\}}) {
next;
}
}
$author = join(",", @parts);
}
}
# truncate author list
if ($opts->{max_authors} > 0 && @authors > $opts->{max_authors}) {
if ($opts->{only_first_author}) {
@authors = ($authors[0]);
} else {
@authors = @authors[0 .. ($opts->{max_authors} - 1)];
}
push @authors, "others";
}
# set BibTeX field to concatenated authors
$bibentry->set($bibfield, join(" and ", @authors));
}
}
# remove trailing periods from title fields
foreach my $bibfield ($bibentry->fieldlist()) {
if ($bibfield =~ /title$/) {
my $bibfieldvalue = $bibentry->get($bibfield);
$bibfieldvalue =~ s/\.+$//;
$bibentry->set($bibfield, $bibfieldvalue);
}
}
# handle e-print journals
if ($bibentry->exists('journal') && $bibentry->exists('archiveprefix') && $bibentry->exists('eprint')) {
( run in 1.424 second using v1.01-cache-2.11-cpan-1bcd28dbaf1 )