Skip to content

Commit

Permalink
#43 empty tags not showing after fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jan 12, 2024
1 parent 486626b commit 264a614
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 2 deletions.
17 changes: 15 additions & 2 deletions bibcop.pl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package bibcop;

use warnings;
use strict;
use File::Basename;

# Hash of incoming command line arguments.
my %args = map { $_ => 1 } @ARGV;
Expand Down Expand Up @@ -516,14 +517,17 @@ sub process_entry {
# Fix one entry.
sub entry_fix {
my (%entry) = @_;
if (not exists $entry{':type'}) {
error("I don't know what to do with an entry without a type");
}
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 $tags = $blessed{$type};
my %allowed = map { $_ => 1 } @$tags;
my @lines;
foreach my $tag (keys %entry) {
Expand All @@ -543,7 +547,9 @@ sub entry_fix {
if ($tag =~ /title|booktitle|journal/) {
$value = '{' . $value . '}';
}
push(@lines, " $tag = {$value},");
if (not $value eq '') {
push(@lines, " $tag = {$value},");
}
}
my $fixed = "\@$type\{$entry{':name'},\n";
my @sorted = sort @lines;
Expand Down Expand Up @@ -607,6 +613,9 @@ sub fix_pages {
if ($value =~ /^[1-9][0-9]*$/) {
return $value;
}
if ($value eq '') {
return $value;
}
my ($left, $right) = split(/---|--|-|–|—|\s/, $value);
$left //= $right;
if ($left eq '') {
Expand Down Expand Up @@ -884,6 +893,10 @@ sub fail {
}
}

if (not basename($0) eq 'bibcop.pl') {
return 1;
}

if (@ARGV+0 eq 0 or exists $args{'--help'} or exists $args{'-?'}) {
info("Bibcop is a Style Checker of BibTeX Files\n\n" .
"Usage:\n" .
Expand Down
1 change: 1 addition & 0 deletions perl-tests/checking.pl
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/perl
# (The MIT License)
#
# Copyright (c) 2022-2024 Yegor Bugayenko
Expand Down
1 change: 1 addition & 0 deletions perl-tests/checks.pl
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/perl
# (The MIT License)
#
# Copyright (c) 2022-2024 Yegor Bugayenko
Expand Down
1 change: 1 addition & 0 deletions perl-tests/cli.pl
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/perl
# (The MIT License)
#
# Copyright (c) 2022-2024 Yegor Bugayenko
Expand Down
44 changes: 44 additions & 0 deletions perl-tests/entry_fixing.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/perl
# (The MIT License)
#
# Copyright (c) 2022-2024 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;

require './bibcop.pl';

fixes({':name' => 'knuth78', ':type' => 'book', 'title' => 'The TeX Book'}, "\@book{knuth78,\n title = {{The TeX Book}},\n}\n\n");
fixes({':name' => 'k', ':type' => 'article', 'title' => 'Book', 'pages' => ''}, "\@article{k,\n title = {{Book}},\n}\n\n");

sub fixes {
my ($hash, $expected) = @_;
my %entry = %$hash;
my $after = entry_fix(%entry);
if ($after ne $expected) {
print "Didn't fix \"entry\" into \"$expected\", but into \"$after\" instead\n";
exit 1;
}
}

1;
1 change: 1 addition & 0 deletions perl-tests/fixing-in-place.pl
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/perl
# (The MIT License)
#
# Copyright (c) 2022-2024 Yegor Bugayenko
Expand Down
1 change: 1 addition & 0 deletions perl-tests/fixing.pl
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/perl
# (The MIT License)
#
# Copyright (c) 2022-2024 Yegor Bugayenko
Expand Down
1 change: 1 addition & 0 deletions perl-tests/functions.pl
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/perl
# (The MIT License)
#
# Copyright (c) 2022-2024 Yegor Bugayenko
Expand Down
1 change: 1 addition & 0 deletions perl-tests/parsing.pl
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/perl
# (The MIT License)
#
# Copyright (c) 2022-2024 Yegor Bugayenko
Expand Down
1 change: 1 addition & 0 deletions tests.pl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ sub show {
require './perl-tests/parsing.pl';
require './perl-tests/checking.pl';
require './perl-tests/fixing.pl';
require './perl-tests/entry_fixing.pl';
require './perl-tests/functions.pl';
require './perl-tests/checks.pl';
require './perl-tests/cli.pl';
Expand Down

0 comments on commit 264a614

Please sign in to comment.