Subject: | How to sort bib file and preserve original formatting? |
Date: | Mon, 21 Nov 2016 12:28:43 +0100 |
To: | bug-Text-BibTeX [...] rt.cpan.org |
From: | Håkon Hægland <hakon.hagland [...] gmail.com> |
Hello.
Thanks for providing this useful module.
I am trying to sort a bib file and at the same time preserve formatting.
Here
is a sample bib file:
@string{AG = {Applied Geochemistry}}
@string{AIAA = {AIAA Journal}}
@Article{zwi05:co2,
title = {Preinjection characterisation and evaluation of
{CO2} sequestration potential in the {Haizume
Formation, Niigata Basin, Japan}. Geochemical
modelling of water-minerals-{CO2} interaction},
author = {Zwingmann, N. and Mito, S. and Sorai, M. and Ohsumi,
T.},
journal = AG,
volume = 60,
number = 2,
pages = {249--258},
year = 2005
}
@Book{ara65:flu,
author = {Aravin, V.I. and Numerov, S.N.},
title = {Theory of fluid flow in undeformable porous media},
publisher = {Israel Program for Scientific Translations},
year = 1965,
address = {Jerusalem}
}
Here is the script I used:
use feature qw(say);
use strict;
use warnings;
use Text::BibTeX qw( :metatypes :nodetypes);
my $bibfile = Text::BibTeX::File->new( 'test.bib' );
$bibfile->preserve_values( 1 );
my $in_preamble = 1;
my %entries;
while (my $entry = Text::BibTeX::Entry->new( $bibfile )) {
if ( !$entry->parse_ok ) {
warn "BibTeX parse error: $!";
next;
}
my $meta_type = $entry->metatype;
if ( $in_preamble ) {
if ( $meta_type != BTE_REGULAR ) {
$entry->print( \*STDOUT );
next;
}
else {
$in_preamble = 0;
}
}
my $key = $entry->key;
$entries{$key} = $entry;
}
for my $key ( sort keys %entries ) {
my $entry = $entries{$key};
$entry->print( \*STDOUT );
}
Here is the output:
@string{
ag = {Applied Geochemistry},
}
@string{
aiaa = {AIAA Journal},
}
@book{ara65:flu,
author = {Aravin, V.I. and Numerov, S.N.},
title = {Theory of fluid flow in undeformable porous media},
publisher = {Israel Program for Scientific Translations},
year = 1965,
address = {Jerusalem},
}
@article{zwi05:co2,
title = {Preinjection characterisation and evaluation of
{CO2} sequestration potential in the {Haizume
Formation, Niigata Basin, Japan}. Geochemical modelling
of water-minerals-{CO2} interaction},
author = {Zwingmann, N. and Mito, S. and Sorai, M. and Ohsumi,
T.},
journal = AG,
volume = 60,
number = 2,
pages = {249--258},
year = 2005,
}
and here is the expected output:
@string{AG = {Applied Geochemistry}}
@string{AIAA = {AIAA Journal}}
@Book{ara65:flu,
author = {Aravin, V.I. and Numerov, S.N.},
title = {Theory of fluid flow in undeformable porous media},
publisher = {Israel Program for Scientific Translations},
year = 1965,
address = {Jerusalem}
}
@Article{zwi05:co2,
title = {Preinjection characterisation and evaluation of
{CO2} sequestration potential in the {Haizume
Formation, Niigata Basin, Japan}. Geochemical
modelling of water-minerals-{CO2} interaction},
author = {Zwingmann, N. and Mito, S. and Sorai, M. and Ohsumi,
T.},
journal = AG,
volume = 60,
number = 2,
pages = {249--258},
year = 2005
}
as can be seen the formatting has been messed up, and @string items has
been expanded to multiple lines and lowercased.
Is this possible to solve with Text::BibTeX ?
Best regards,
Håkon Hægland