Skip Menu |

This queue is for tickets about the Text-BibTeX CPAN distribution.

Report information
The Basics
Id: 118875
Status: rejected
Priority: 0/
Queue: Text-BibTeX

People
Owner: Nobody in particular
Requestors: hakon.hagland [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



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
Dear Håkon, Unfortunately this is not possible with Text::BibTeX. The btparse library constructs an abstract syntax tree, that does not store any information on the original formatting. Therefore, it will output the results using a canonical approach, that will much probably be different from your own preferred format. Nevertheless, the result of using the btparse output in a LaTeX document should be exactly the same of using your original file. If you find such a situation, please let us know. Best regards, alberto On Mon Nov 21 06:28:54 2016, hakon.hagland@gmail.com wrote: Show quoted text
> 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