summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2024-05-12 12:00:02 +0200
committerJonas Smedegaard <dr@jones.dk>2024-06-04 17:35:41 +0200
commit6c16c0ca2f22e8ee249f448d928101fd33240a1b (patch)
treeb563693d810730f53461c64648bb29df4bff3012
parentf6bf252b4faac6ad6c26db40adc950b329043370 (diff)
improve biblatex tidying, to support multiple JabRef comments, and not use temporary files
-rw-r--r--Makefile4
-rwxr-xr-xbin/biblatex-tidy103
-rwxr-xr-xbin/biblatex-tidy.sh16
3 files changed, 105 insertions, 18 deletions
diff --git a/Makefile b/Makefile
index 2ffbbe7..1b91bf1 100644
--- a/Makefile
+++ b/Makefile
@@ -9,7 +9,7 @@ ARTICLES = waste worlding
ONTOLOGIES = learning waste
-PERL_FILES = bin/hedgedoc2quarto bin/xmp2rdfxml
+PERL_FILES = bin/biblatex-tidy bin/hedgedoc2quarto bin/xmp2rdfxml
TIDY_MARKDOWN = worlding
@@ -100,7 +100,7 @@ lint-markdown:
tidy: tidy-bibtex tidy-perl
tidy-bibtex: ref.bib
- bin/biblatex-tidy.sh $<
+ bin/biblatex-tidy $<
tidy-perl:
perltidy -b -bext='/' --profile=.perltidyrc $(PERL_FILES)
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<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 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<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;
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~