- #!/usr/bin/perl
- # tidy BibLaTeX file
- # Depends: bibclean libipc-run3-perl libpath-tiny-perl
- use v5.36;
- use strict;
- use IPC::Run3 qw/run3/;
- =head1 NAME
- biblatex-tidy - tidy BibLaTeX data
- =head1 VERSION
- Version 0.0.1
- =head1 SYNOPSIS
- biblatex-tidy INFILE
- biblatex-tidy INFILE OUTFILE
- biblatex-tidy < INFILE > OUTFILE
- =head1 DESCRIPTION
- B<biblatex-tidy> reformats BibLaTeX data.
- If a only a single filename is given,
- the file contents are replaced.
- If two filenames are given,
- the first is read,
- and cleaned output is saved to the second.
- If no filenames are given,
- then data is expected on STDIN,
- and cleaned data is emitted on STDOUT.
- Internally,
- the tool B<bibclean> is called internally to do the actual tidying.
- If data contains a line beginning with C<@Comment{jabref-meta:>,
- typically added by JabRef at the end of BibLaTeX data,
- then the remaining data is I<not> cleaned,
- because B<bibclean> fails to parse such comments.
- =cut
- # slurp INFILE if passed as first argument, or else STDIN
- my ( $infile, $outfile, $bogus ) = @ARGV;
- die 'Too many arguments: expected INFILE and OUTFILE' if $bogus;
- @ARGV = ($infile) if $infile;
- my $content = do { local $/ = undef; <> };
- # put aside eventual trailing JabRef comments to not confuse bibclean
- my ( $data, $comments ) = split /(?=\n\@Comment\{jabref-meta:)/, $content, 2;
- # call bibclean on comment-stripped data
- my @command = qw(bibclean -max-width 0);
- my $newdata;
- run3( \@command, \$data, \$newdata,
- { binmode_stdin => ':utf8', binmode_stdout => ':utf8' } )
- or die "Failed to execute @command: $?";
- # save/replace/spew cleaned data, reviving eventual trailing comments
- if ($infile) {
- open( FH, '>', $outfile || $infile ) or die $!;
- print FH $newdata, $comments;
- }
- else {
- print $newdata, $comments;
- }
- =encoding UTF-8
- =head1 AUTHOR
- Jonas Smedegaard C<< <dr@jones.dk> >>
- =head1 COPYRIGHT AND LICENSE
- Copyright © 2024 Jonas Smedegaard
- This program is free software:
- you can redistribute it and/or modify it
- under the terms of the GNU Affero General Public License
- as published by the Free Software Foundation,
- either version 3, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY;
- without even the implied warranty
- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- See the GNU Affero General Public License for more details.
- You should have received a copy
- of the GNU Affero General Public License along with this program.
- If not, see <https://www.gnu.org/licenses/>.
- =cut
- 1;
|