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