diff options
Diffstat (limited to 'Master/texmf-dist/scripts/bibcop')
-rwxr-xr-x | Master/texmf-dist/scripts/bibcop/bibcop.pl | 98 |
1 files changed, 60 insertions, 38 deletions
diff --git a/Master/texmf-dist/scripts/bibcop/bibcop.pl b/Master/texmf-dist/scripts/bibcop/bibcop.pl index 081ac6bbdfb..2b44f2d5712 100755 --- a/Master/texmf-dist/scripts/bibcop/bibcop.pl +++ b/Master/texmf-dist/scripts/bibcop/bibcop.pl @@ -21,11 +21,12 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -# 2024-01-11 0.0.16 +# 2024-01-12 0.0.17 package bibcop; use warnings; use strict; +use File::Basename; # Hash of incoming command line arguments. my %args = map { $_ => 1 } @ARGV; @@ -301,7 +302,7 @@ sub check_typography { } } foreach my $s (@space_before) { - if ($value =~ /^.*[^\{\s]\Q$s\E.*$/) { + if ($value =~ /^.*[^\{\s\\]\Q$s\E.*$/) { return "In the '$tag', put a space before the $symbols{$s}" } } @@ -513,6 +514,52 @@ sub process_entry { return @errors; } +# 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{$type}; + my %allowed = map { $_ => 1 } @$tags; + my @lines; + foreach my $tag (keys %entry) { + if ($tag =~ /^:/) { + next; + } + if (not exists $allowed{$tag} and not exists $allowed{$tag . '?'}) { + next; + } + my $value = clean_tex($entry{$tag}); + my $fixer = "fix_$tag"; + my $fixed = $value; + if (defined &{$fixer}) { + no strict 'refs'; + $value = $fixer->($value); + } + if ($tag =~ /title|booktitle|journal/) { + $value = '{' . $value . '}'; + } + if (not $value eq '') { + push(@lines, " $tag = {$value},"); + } + } + my $fixed = "\@$type\{$entry{':name'},\n"; + my @sorted = sort @lines; + foreach my $line (@sorted) { + $fixed = $fixed . $line . "\n"; + } + $fixed = $fixed . "}\n\n"; + return $fixed; +} + sub fix_author { my ($value) = @_; my @authors = split(/\s+and\s+/, $value); @@ -566,10 +613,15 @@ 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 '') { $left = $right; } + $right //= $left; if ($right eq '') { $right = $left; } @@ -841,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" . @@ -861,7 +917,7 @@ if (@ARGV+0 eq 0 or exists $args{'--help'} or exists $args{'-?'}) { " --latex Report errors in LaTeX format using \\PackageWarningNoLine command\n\n" . "If any issues, report to GitHub: https://github.com/yegor256/bibcop"); } elsif (exists $args{'--version'} or exists $args{'-v'}) { - info('0.0.16 2024-01-11'); + info('0.0.17 2024-01-12'); } else { my ($file) = grep { not($_ =~ /^-.*$/) } @ARGV; if (not $file) { @@ -874,41 +930,7 @@ if (@ARGV+0 eq 0 or exists $args{'--help'} or exists $args{'-?'}) { 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; - foreach my $tag (keys %entry) { - if ($tag =~ /^:/) { - next; - } - if (not exists $allowed{$tag} and not exists $allowed{$tag . '?'}) { - next; - } - my $value = clean_tex($entry{$tag}); - my $fixer = "fix_$tag"; - my $fixed = $value; - if (defined &{$fixer}) { - no strict 'refs'; - $value = $fixer->($value); - } - if ($tag =~ /title|booktitle|journal/) { - $value = '{' . $value . '}'; - } - push(@lines, " $tag = {$value},"); - } - $fixed = $fixed . "\@$type\{$entry{':name'},\n"; - my @sorted = sort @lines; - foreach my $line (@sorted) { - $fixed = $fixed . $line . "\n"; - } - $fixed = $fixed . "}\n\n"; + $fixed = $fixed . entry_fix(%entry); } if (exists $args{'-i'} or exists $args{'--in-place'}) { open(my $out, '>', $file) or error('Cannot open file for writing: ' . $file); |