From 6c16c0ca2f22e8ee249f448d928101fd33240a1b Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Sun, 12 May 2024 12:00:02 +0200 Subject: improve biblatex tidying, to support multiple JabRef comments, and not use temporary files --- bin/biblatex-tidy | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++ bin/biblatex-tidy.sh | 16 -------- 2 files changed, 103 insertions(+), 16 deletions(-) create mode 100755 bin/biblatex-tidy delete mode 100755 bin/biblatex-tidy.sh (limited to 'bin') diff --git a/bin/biblatex-tidy b/bin/biblatex-tidy new file mode 100755 index 0000000..39ed4f4 --- /dev/null +++ b/bin/biblatex-tidy @@ -0,0 +1,103 @@ +#!/usr/bin/perl + +# tidy BibLaTeX file + +# Depends: bibclean 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 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 the the second. + +If no filenames are given, +then data is expected on STDIN +and cleaned data is emitted on STDOUT. + +Internally, +the tool B 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 cleaned, +because B 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<< >> + +=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 . + +=cut + +1; diff --git a/bin/biblatex-tidy.sh b/bin/biblatex-tidy.sh deleted file mode 100755 index a29632d..0000000 --- a/bin/biblatex-tidy.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/sh - -# tidy BibLaTeX file - -# Depends: bibclean - -set -eu - -INFILE=$1 - -# workaround: strip JabRef comment that bibclean chokes on -perl -gpe 's/^\@Comment\{jabref-meta: databaseType:biblatex;\}$//m' < $INFILE > $INFILE~ -bibclean -max-width 0 $INFILE~ > $INFILE~~ -printf '\n%s\n' '@Comment{jabref-meta: databaseType:biblatex;}' >> $INFILE~~ -mv -f $INFILE~~ $INFILE -rm -f $INFILE~ -- cgit v1.2.3