Skip to content

Commit

Permalink
#34 --in-place fixing mechanism introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jun 4, 2023
1 parent ed6eebf commit 0bfb6c7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
18 changes: 15 additions & 3 deletions bibcop.pl
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,7 @@ sub fail {
" -v, --version Print the current version of the tool and exit\n" .
" -?, --help Print this help screen\n" .
" --fix Fix the errors and print a new version of the .bib file to the console\n" .
" -i, --in-place When used together with --fix, modifies the file in place, doesn't print it to the console\n" .
" --verbose Print supplementary debugging information\n" .
" --no:XXX Disable one of the following checks (e.g. --no:wraps):\n" .
" tags Only some tags are allowed, while some of them are mandatory\n" .
Expand All @@ -828,12 +829,16 @@ sub fail {
my $bib; { local $/; $bib = <$fh>; }
my @entries = entries($bib);
if (exists $args{'--fix'}) {
my $fixed = '';
for my $i (0..(@entries+0 - 1)) {
my %entry = %{ $entries[$i] };
my $type = $entry{':type'};
if (not exists $blessed{$type}) {
error("I don't know what to do with \@$type type of BibTeX entry");
}
if (not exists $entry{':name'}) {
error("I don't know what to do with an entry without a name");
}
my $tags = $blessed{$entry{':type'}};
my %allowed = map { $_ => 1 } @$tags;
my @lines;
Expand All @@ -856,12 +861,19 @@ sub fail {
}
push(@lines, " $tag = {$value},");
}
info("\@$type\{$entry{':name'},");
$fixed = $fixed . "\@$type\{$entry{':name'},\n";
my @sorted = sort @lines;
foreach my $line (@sorted) {
info($line);
$fixed = $fixed . $line . "\n";
}
info("}\n");
$fixed = $fixed . "}\n";
}
if (exists $args{'-i'} or exists $args{'--in-place'}) {
open(my $out, '>', $file) or error('Cannot open file for writing: ' . $file);
print $out $fixed;
close($out);
} else {
info($fixed);
}
} else {
debug((@entries+0) . ' entries found in ' . $file);
Expand Down
48 changes: 48 additions & 0 deletions perl-tests/fixing-in-place.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# (The MIT License)
#
# Copyright (c) 2022-2023 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

package bibcop;

use strict;
use warnings;
use File::Temp qw/ tempfile /;

my ($out, $temp) = tempfile();
print $out '@article{knuth, author = {Knuth, Donald E}} @book{lamport, title={LaTeX}}';
close($out);

my $stdout = `perl ./bibcop.pl --in-place --fix '$temp' 2>&1`;
if (not $stdout eq '') {
print $stdout;
print "This output was not expected!\n";
exit 1;
}

open(my $in, '<', $temp);
my $after; { local $/; $after = <$in>; }

if (index($after, 'Knuth, Donald E.') == 1) {
print "Didn't fix in place\n";
exit 1;
}

1;

0 comments on commit 0bfb6c7

Please sign in to comment.