summaryrefslogtreecommitdiff
path: root/bin/biblatex-tidy
blob: 39ed4f4573d11b32e8d86d48b9823417a25faae2 (plain)
  1. #!/usr/bin/perl
  2. # tidy BibLaTeX file
  3. # Depends: bibclean libpath-tiny-perl
  4. use v5.36;
  5. use strict;
  6. use IPC::Run3 qw/run3/;
  7. =head1 NAME
  8. biblatex-tidy - tidy BibLaTeX data
  9. =head1 VERSION
  10. Version 0.0.1
  11. =head1 SYNOPSIS
  12. biblatex-tidy INFILE
  13. biblatex-tidy INFILE OUTFILE
  14. biblatex-tidy < INFILE > OUTFILE
  15. =head1 DESCRIPTION
  16. B<biblatex-tidy> reformats BibLaTeX data.
  17. If a only a single filename is given,
  18. the file contents are replaced.
  19. If two filenames are given,
  20. the first is read and cleaned output is saved the the second.
  21. If no filenames are given,
  22. then data is expected on STDIN
  23. and cleaned data is emitted on STDOUT.
  24. Internally,
  25. the tool B<bibclean> is called internally to do the actual tidying.
  26. If data contains a line beginning with C<@Comment{jabref-meta:>,
  27. typically added by JabRef at the end of BibLaTeX data,
  28. then the remaining data is I<not> cleaned,
  29. because B<bibclean> fails to parse such comments.
  30. =cut
  31. # slurp INFILE if passed as first argument, or else STDIN
  32. my ( $infile, $outfile, $bogus ) = @ARGV;
  33. die 'Too many arguments: expected INFILE and OUTFILE' if $bogus;
  34. @ARGV = ($infile) if $infile;
  35. my $content = do { local $/ = undef; <> };
  36. # put aside eventual trailing JabRef comments to not confuse bibclean
  37. my ( $data, $comments ) = split /(?=\n\@Comment\{jabref-meta:)/, $content, 2;
  38. # call bibclean on comment-stripped data
  39. my @command = qw(bibclean -max-width 0);
  40. my $newdata;
  41. run3( \@command, \$data, \$newdata,
  42. { binmode_stdin => ':utf8', binmode_stdout => ':utf8' } )
  43. or die "Failed to execute @command: $?";
  44. # save/replace/spew cleaned data, reviving eventual trailing comments
  45. if ($infile) {
  46. open( FH, '>', $outfile || $infile ) or die $!;
  47. print FH $newdata, $comments;
  48. }
  49. else {
  50. print $newdata, $comments;
  51. }
  52. =encoding UTF-8
  53. =head1 AUTHOR
  54. Jonas Smedegaard C<< <dr@jones.dk> >>
  55. =head1 COPYRIGHT AND LICENSE
  56. Copyright © 2024 Jonas Smedegaard
  57. This program is free software:
  58. you can redistribute it and/or modify it
  59. under the terms of the GNU Affero General Public License
  60. as published by the Free Software Foundation,
  61. either version 3, or (at your option) any later version.
  62. This program is distributed in the hope that it will be useful,
  63. but WITHOUT ANY WARRANTY;
  64. without even the implied warranty
  65. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  66. See the GNU Affero General Public License for more details.
  67. You should have received a copy
  68. of the GNU Affero General Public License along with this program.
  69. If not, see <https://www.gnu.org/licenses/>.
  70. =cut
  71. 1;