| 1 |
#!/usr/bin/perl |
| 2 |
# SPDX-FileCopyrightText: Copyright (c) 2022-2025 Yegor Bugayenko |
| 3 |
# SPDX-License-Identifier: MIT |
| 4 |
|
| 5 |
# 2025/04/28 0.0.32 |
| 6 |
package bibcop; |
| 7 |
|
| 8 |
use warnings; |
| 9 |
use strict; |
| 10 |
use POSIX; |
| 11 |
use File::Basename; |
| 12 |
|
| 13 |
# Hash of incoming command line arguments. |
| 14 |
my %args = map { $_ => 1 } @ARGV; |
| 15 |
|
| 16 |
# If you want to add an extra check, just create a new procedure |
| 17 |
# named as "check_*". |
| 18 |
|
| 19 |
# Only these tags are allowed and only these types of entries. |
| 20 |
my %blessed = ( |
| 21 |
'article' => ['doi', 'year', 'title', 'author', 'journal', 'volume', 'number', 'month?', 'pages?'], |
| 22 |
'inproceedings' => ['doi', 'booktitle', 'title', 'author', 'year', 'pages?', 'month?', 'organization?', 'volume?'], |
| 23 |
'incollection' => ['doi', 'booktitle', 'title', 'author', 'year', 'editor', 'pages?', 'month?', 'volume?', 'publisher?'], |
| 24 |
'book' => ['title', 'author', 'year', 'publisher', 'doi?', 'edition?'], |
| 25 |
'phdthesis' => ['title', 'author', 'year', 'school', 'doi?'], |
| 26 |
'misc' => ['title', 'author', 'year', 'eprint?', 'archiveprefix?', 'primaryclass?', 'month?', 'organization?', 'doi?', 'howpublished?', 'note?'], |
| 27 |
); |
| 28 |
|
| 29 |
# See https://research.arizona.edu/faq/what-do-you-mean-when-you-say-use-title-case-proposalproject-titles |
| 30 |
my %minors = map { $_ => 1 } qw/in of at to by the a an and or as if up via yet nor but off on for into vs versus/; |
| 31 |
|
| 32 |
# Check the presence of mandatory tags. |
| 33 |
sub check_mandatory_tags { |
| 34 |
if (exists $args{'--no:tags'}) { |
| 35 |
return; |
| 36 |
} |
| 37 |
my (%entry) = @_; |
| 38 |
my $type = $entry{':type'}; |
| 39 |
if (not exists $blessed{$type}) { |
| 40 |
return "The type of entry is not allowed: '$type'" |
| 41 |
} |
| 42 |
my $mandatory = $blessed{$type}; |
| 43 |
foreach my $tag (@$mandatory) { |
| 44 |
if ($tag =~ /^.*\?$/) { |
| 45 |
next; |
| 46 |
} |
| 47 |
if (not(exists $entry{$tag})) { |
| 48 |
my $listed = listed_tags(%entry); |
| 49 |
if ($tag eq 'doi' and exists $args{'--no:doi'}) { |
| 50 |
next; |
| 51 |
} |
| 52 |
return "A mandatory '$tag' tag for '\@$type' is missing among $listed" |
| 53 |
} |
| 54 |
} |
| 55 |
if (exists $blessed{$type}) { |
| 56 |
my %required = map { $_ => 1 } @$mandatory; |
| 57 |
foreach my $tag (keys %entry) { |
| 58 |
if ($tag =~ /^:/) { |
| 59 |
next; |
| 60 |
} |
| 61 |
if (not(exists $required{$tag}) && not(exists $required{$tag . '?'})) { |
| 62 |
return "The '$tag' tag is not suitable for '$type', use only these: (@$mandatory)" |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
# Check that all major words are capitalized. |
| 69 |
sub check_capitalization { |
| 70 |
if (exists $args{'--no:caps'}) { |
| 71 |
return; |
| 72 |
} |
| 73 |
my (%entry) = @_; |
| 74 |
my %tags = map { $_ => 1 } qw/title booktitle journal publisher organization/; |
| 75 |
foreach my $tag (keys %entry) { |
| 76 |
if (not exists $tags{$tag}) { |
| 77 |
next; |
| 78 |
} |
| 79 |
my @ends = qw/ ; ? . --- : ! /; |
| 80 |
my $value = $entry{$tag}; |
| 81 |
my @words = only_words($value); |
| 82 |
my $pos = 0; |
| 83 |
foreach my $word (@words) { |
| 84 |
$word =~ s/\.$//g; |
| 85 |
$pos = $pos + 1; |
| 86 |
if (not $word =~ /^[A-Za-z]/) { |
| 87 |
next; |
| 88 |
} |
| 89 |
if ($word =~ /^\{.*|.*\}$/) { |
| 90 |
next; |
| 91 |
} |
| 92 |
if (exists $minors{$word}) { |
| 93 |
if ($pos eq 1) { |
| 94 |
return "The minor word '$word' in the '$tag' must be upper-cased since it is the first one" |
| 95 |
} |
| 96 |
my $before = $words[$pos - 2]; |
| 97 |
if (grep(/^\Q$before\E$/, @ends)) { |
| 98 |
return "The minor word '$word' in the '$tag' must be upper-cased, because it follows the '$before'" |
| 99 |
} |
| 100 |
next; |
| 101 |
} |
| 102 |
if (exists $minors{lc($word)}) { |
| 103 |
if ($pos eq 1) { |
| 104 |
next; |
| 105 |
} |
| 106 |
my $before = $words[$pos - 2]; |
| 107 |
if (grep(/^\Q$before\E$/, @ends)) { |
| 108 |
next; |
| 109 |
} |
| 110 |
return "All minor words in the '$tag' must be lower-cased, while @{[as_position($pos)]} word '$word' is not" |
| 111 |
} |
| 112 |
if ($word =~ /^[a-z].*/) { |
| 113 |
return "All major words in the '$tag' must be capitalized, while @{[as_position($pos)]} word '$word' is not" |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
# Check that the 'author' is formatted correctly. |
| 120 |
sub check_author { |
| 121 |
my (%entry) = @_; |
| 122 |
my @tags = qw/author editor/; |
| 123 |
foreach my $tag (@tags) { |
| 124 |
if (not exists $entry{$tag}) { |
| 125 |
next; |
| 126 |
} |
| 127 |
if ($entry{$tag} =~ /^\{.+\}$/) { |
| 128 |
next; |
| 129 |
} |
| 130 |
my $author = clean_tex($entry{$tag}); |
| 131 |
my @authors = split(/\s+and\s+/, $author); |
| 132 |
my $pos = 0; |
| 133 |
for my $a (@authors) { |
| 134 |
$pos += 1; |
| 135 |
if ($a eq 'others') { |
| 136 |
next; |
| 137 |
} |
| 138 |
if (index($a, ' ') != -1 and index($a, ',') == -1) { |
| 139 |
return "The last name should go first, all other names must follow, after a comma in @{[as_position($pos)]} '$tag', as in 'Knuth, Donald E.'"; |
| 140 |
} |
| 141 |
my $npos = 0; |
| 142 |
for my $name (split(/[ ,]+/, $a)) { |
| 143 |
$npos += 1; |
| 144 |
if (index($name, '{') != -1) { |
| 145 |
next; |
| 146 |
} |
| 147 |
if ($name =~ /^[A-Z]\.$/) { |
| 148 |
next; |
| 149 |
} |
| 150 |
if ($name =~ /^[A-Z][^.]+$/) { |
| 151 |
next |
| 152 |
} |
| 153 |
if ($name =~ /^(van|de|der|dos|von|e)$/) { |
| 154 |
next |
| 155 |
} |
| 156 |
if ($name =~ /^[A-Z]$/) { |
| 157 |
return "A shortened name must have a tailing dot in @{[as_position($pos)]} '$tag', as in 'Knuth, Donald E.'"; |
| 158 |
} |
| 159 |
return "In @{[as_position($pos)]} '$tag' @{[as_position($npos)]} name looks suspicious ($name), use something like 'Knuth, Donald E. and Duane, Bibby'"; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
# Check that titles don't have shortened words with a tailing dot. |
| 166 |
sub check_shortenings { |
| 167 |
my (%entry) = @_; |
| 168 |
my %tags = map { $_ => 1 } qw/title booktitle journal/; |
| 169 |
foreach my $tag (keys %entry) { |
| 170 |
if (not exists $tags{$tag}) { |
| 171 |
next; |
| 172 |
} |
| 173 |
my $value = $entry{$tag}; |
| 174 |
my @words = split(/ /, clean_tex($value)); |
| 175 |
foreach my $word (@words) { |
| 176 |
if (not $word =~ /^[A-Za-z]/) { |
| 177 |
next; |
| 178 |
} |
| 179 |
if ($word eq 'vs.') { |
| 180 |
next; |
| 181 |
} |
| 182 |
if ($word =~ /\.\.\.$/) { |
| 183 |
next; |
| 184 |
} |
| 185 |
if ($word =~ /^.*\.$/) { |
| 186 |
return "Do not shorten the words in the '$tag', such as '$word'" |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
# Check the right format of the 'title' and 'booktitle.' |
| 193 |
sub check_wrapping { |
| 194 |
if (exists $args{'--no:wraps'}) { |
| 195 |
return; |
| 196 |
} |
| 197 |
my (%entry) = @_; |
| 198 |
my @tags = qw/title booktitle/; |
| 199 |
foreach my $tag (@tags) { |
| 200 |
if (not exists($entry{$tag})) { |
| 201 |
next; |
| 202 |
} |
| 203 |
my $title = $entry{$tag}; |
| 204 |
if (not $title =~ /^\{.+\}$/) { |
| 205 |
return "The '$tag' must be wrapped in double curled brackets" |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
# Check the right format of the tags for arXiv. |
| 211 |
# See https://arxiv.org/help/arxiv_identifier |
| 212 |
sub check_arXiv { |
| 213 |
my (%entry) = @_; |
| 214 |
if (exists($entry{'archiveprefix'})) { |
| 215 |
if (not exists $entry{'eprint'}) { |
| 216 |
return "The 'eprint' is mandatory when 'archiveprefix' is there" |
| 217 |
} |
| 218 |
if (not $entry{'eprint'} =~ /^[0-9]{4}\.[0-9]{4,5}(v[0-9]+)?$/) { |
| 219 |
return "The 'eprint' must have two integers separated by a dot" |
| 220 |
} |
| 221 |
my $eprint = $entry{'eprint'}; |
| 222 |
my ($head, $tail) = split(/\./, $eprint); |
| 223 |
my $year = substr($head, 0, 2); |
| 224 |
my $month = substr($head, 2); |
| 225 |
if ($month > 12) { |
| 226 |
return "The month '$month' of the 'eprint' is wrong, it can't be bigger than 12" |
| 227 |
} |
| 228 |
if (not exists $entry{'primaryclass'}) { |
| 229 |
return "The 'primaryclass' is mandatory when 'archiveprefix' is there" |
| 230 |
} |
| 231 |
if (not $entry{'primaryclass'} =~ /^[a-z]{2,}\.[A-Z]{2}$/) { |
| 232 |
return "The 'primaryclass' must have two parts, like 'cs.PL'" |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
# Check that organization is not mentioned in the booktitle. |
| 238 |
sub check_org_in_booktitle { |
| 239 |
if (exists $args{'--no:org'}) { |
| 240 |
return; |
| 241 |
} |
| 242 |
my (%entry) = @_; |
| 243 |
my @orgs = qw/ACM IEEE/; |
| 244 |
if (exists($entry{'booktitle'})) { |
| 245 |
my $title = $entry{'booktitle'}; |
| 246 |
foreach my $o (@orgs) { |
| 247 |
if ($title =~ /^.*\Q$o\E.*$/) { |
| 248 |
return "The '$o' organization must not be mentioned in the booktitle, use 'publisher' tag instead" |
| 249 |
} |
| 250 |
} |
| 251 |
if ($title =~ /^.*(ACM|IEEE).*$/) { |
| 252 |
return "Don't mention the organization in the booktitle, use 'publisher' tag instead" |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
# Check that no values have tailing dots. |
| 258 |
# Check that there are no spaces before commans. |
| 259 |
sub check_typography { |
| 260 |
my (%entry) = @_; |
| 261 |
my %symbols = ( |
| 262 |
'...' => 'ellipses', |
| 263 |
'.' => 'dot', |
| 264 |
',' => 'comma', |
| 265 |
';' => 'semi-colon', |
| 266 |
':' => 'colon', |
| 267 |
'!' => 'exclamation mark', |
| 268 |
'?' => 'question mark', |
| 269 |
'-' => 'dash', |
| 270 |
'--' => 'double dash', |
| 271 |
'---' => 'triple dash', |
| 272 |
'(' => 'opening bracket', |
| 273 |
')' => 'closing bracket', |
| 274 |
'[' => 'opening square bracket', |
| 275 |
']' => 'closing square bracket', |
| 276 |
); |
| 277 |
my @need_spaces_around = ( '---', '...' ); |
| 278 |
my @no_spaces_around = ( '--', '-' ); |
| 279 |
my @no_space_before = ( '.', ',', ';', ':', '?', '!', ')', ']' ); |
| 280 |
my @no_space_after = ( '(', '[' ); |
| 281 |
my @need_space_before = ( '(', '[' ); |
| 282 |
my @need_space_after = ( ')', ']', ',' ); |
| 283 |
my @good_tails = ( 'Inc.', 'Ltd.' ); |
| 284 |
my @bad_tails = ( '.', ',', ';', ':', '-' ); |
| 285 |
foreach my $tag (keys %entry) { |
| 286 |
if ($tag =~ /^:.*/) { |
| 287 |
next; |
| 288 |
} |
| 289 |
if ($tag eq 'doi') { |
| 290 |
next; |
| 291 |
} |
| 292 |
my $value = $entry{$tag}; |
| 293 |
foreach my $s (@bad_tails) { |
| 294 |
if ($s eq '.' and ($tag eq 'author' or $tag eq 'editor')) { |
| 295 |
next; |
| 296 |
} |
| 297 |
my $good = 0; |
| 298 |
foreach my $gt (@good_tails) { |
| 299 |
if ($value =~ /^.*\Q$gt\E$/) { |
| 300 |
$good = 1; |
| 301 |
} |
| 302 |
} |
| 303 |
if (not $good) { |
| 304 |
if ($value =~ /^.*\Q$s\E$/) { |
| 305 |
return "The '$tag' must not end with a $symbols{$s}" |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
foreach my $s (@no_space_before) { |
| 310 |
if ($value =~ /^.*\s\Q$s\E(?!\Q$s\E).*$/) { |
| 311 |
return "In the '$tag', do not put a space before the $symbols{$s}" |
| 312 |
} |
| 313 |
} |
| 314 |
foreach my $s (@no_space_after) { |
| 315 |
if ($value =~ /^.*\Q$s\E\s.*$/) { |
| 316 |
return "In the '$tag', do not put a space after the $symbols{$s}" |
| 317 |
} |
| 318 |
} |
| 319 |
foreach my $s (@need_space_before) { |
| 320 |
if ($value =~ /^.*[^\{\s\\]\Q$s\E.*$/) { |
| 321 |
return "In the '$tag', put a space before the $symbols{$s}" |
| 322 |
} |
| 323 |
} |
| 324 |
foreach my $s (@need_space_after) { |
| 325 |
my $p = join('', @no_space_before); |
| 326 |
if ($value =~ /^.*[^\\]\Q$s\E[^\}\s\Q$p\E].*$/) { |
| 327 |
return "In the '$tag', put a space after the $symbols{$s}" |
| 328 |
} |
| 329 |
} |
| 330 |
foreach my $s (@need_spaces_around) { |
| 331 |
if ($value =~ /^.*[^\s]\Q$s\E.*$/ or $value =~ /^.*\Q$s\E[^\s].*$/) { |
| 332 |
return "In the '$tag', put spaces around the $symbols{$s}" |
| 333 |
} |
| 334 |
} |
| 335 |
foreach my $s (@no_spaces_around) { |
| 336 |
if ($value =~ /^.*\s\Q$s\E\s.*$/) { |
| 337 |
return "In the '$tag', don't put spaces around the $symbols{$s}" |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
# Check that the type is small letters. |
| 344 |
sub check_type_capitalization { |
| 345 |
my (%entry) = @_; |
| 346 |
my $type = $entry{':type'}; |
| 347 |
if (not $type =~ /^[a-z]+$/) { |
| 348 |
return "The type '$type' must be lower-cased" |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
# Check that no values have non-ASCII symbols. |
| 353 |
sub check_ascii { |
| 354 |
my (%entry) = @_; |
| 355 |
foreach my $tag (keys %entry) { |
| 356 |
if ($tag =~ /^:.*/) { |
| 357 |
next; |
| 358 |
} |
| 359 |
my $value = $entry{$tag}; |
| 360 |
for my $pos (0..length($value)-1) { |
| 361 |
my $char = substr($value, $pos, 1); |
| 362 |
my $ord = ord($char); |
| 363 |
if ($ord == 9 || $ord == 10 || $ord == 13) { |
| 364 |
next; |
| 365 |
} |
| 366 |
if ($ord < 20) { |
| 367 |
return "In the '$tag', don't use control symbol '0x" . (sprintf '%04x', $ord) . "'" |
| 368 |
} |
| 369 |
if ($ord > 0x7f) { |
| 370 |
return "In the '$tag', don't use Unicode symbol '0x" . (sprintf '%04x', $ord) . "'" |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
# Check the year is not mentioned in titles. |
| 377 |
sub check_year_in_titles { |
| 378 |
my (%entry) = @_; |
| 379 |
my @tags = qw/title booktitle journal/; |
| 380 |
foreach my $tag (@tags) { |
| 381 |
if (not exists($entry{$tag})) { |
| 382 |
next; |
| 383 |
} |
| 384 |
my @words = only_words($entry{$tag}); |
| 385 |
foreach my $word (@words) { |
| 386 |
if ($word =~ /^[1-9][0-9]{3}$/) { |
| 387 |
return "The '$tag' must not contain the year $word, it is enough to have the 'year' tag" |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
# Check the right format of the 'booktitle' in the 'inproceedings' entry. |
| 394 |
sub check_booktile_of_inproceedings { |
| 395 |
if (exists $args{'--no:inproc'}) { |
| 396 |
return; |
| 397 |
} |
| 398 |
my (%entry) = @_; |
| 399 |
my $tag = 'inproceedings'; |
| 400 |
if ($entry{':type'} eq $tag) { |
| 401 |
if (exists $entry{'booktitle'}) { |
| 402 |
my @words = only_words($entry{'booktitle'}); |
| 403 |
if (lc($words[0]) ne 'proceedings' or lc($words[1]) ne 'of' or lc($words[2]) ne 'the') { |
| 404 |
return "The '$tag' must start with 'Proceedings of the ...'" |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
# Check the right format of the 'doi.' |
| 411 |
sub check_doi { |
| 412 |
if (exists $args{'--no:doi'}) { |
| 413 |
return; |
| 414 |
} |
| 415 |
my (%entry) = @_; |
| 416 |
if (exists $entry{'doi'}) { |
| 417 |
my $doi = $entry{'doi'}; |
| 418 |
if (not $doi =~ /^[0-9a-zA-Z.]+\/[0-9a-zA-Z._\-)(><:;\/]+$/) { |
| 419 |
return "The format of the 'doi' is wrong" |
| 420 |
} |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
# Check the right format of the 'howpublished.' |
| 425 |
sub check_howpublished { |
| 426 |
my (%entry) = @_; |
| 427 |
if (exists $entry{'howpublished'}) { |
| 428 |
my $how = $entry{'howpublished'}; |
| 429 |
if (not $how =~ /^\\url\{.+\}$/) { |
| 430 |
return "The format of the 'howpublished' is wrong, use \\url{} inside" |
| 431 |
} |
| 432 |
my $url = substr($how, 5, -1); |
| 433 |
if (not $url =~ /^https?:\/\/.+$/) { |
| 434 |
return "The format of the URL in 'howpublished' is wrong, doesn't start with https://: '$url'" |
| 435 |
} |
| 436 |
my $max = 64; |
| 437 |
my $len = length($url); |
| 438 |
if ($len > $max) { |
| 439 |
return "The length of the URL in 'howpublished' is too big ($len > $max), use URL shortener: '$url'" |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
# Check the right format of the 'year.' |
| 445 |
sub check_year { |
| 446 |
my (%entry) = @_; |
| 447 |
if (exists $entry{'year'}) { |
| 448 |
my $year = $entry{'year'}; |
| 449 |
if ($year =~ /^\{.+\}$/) { |
| 450 |
return; |
| 451 |
} |
| 452 |
if (not $year =~ /^[0-9]+$/) { |
| 453 |
return "The format of the 'year' is wrong, may only contain numbers or must be wrapped in curly braces" |
| 454 |
} |
| 455 |
if (not $year =~ /^[0-9]{4}$/) { |
| 456 |
return "Exactly four digits must be present in the 'year', or it must be wrapped in curly braces" |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
# Check the right format of the 'month.' |
| 462 |
sub check_month { |
| 463 |
my (%entry) = @_; |
| 464 |
if (exists $entry{'month'}) { |
| 465 |
my $month = $entry{'month'}; |
| 466 |
if (not $month =~ /^[1-9]|10|11|12$/) { |
| 467 |
return "The format of the 'month' is wrong" |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
# Check the right format of the 'volume.' |
| 473 |
sub check_volume { |
| 474 |
my (%entry) = @_; |
| 475 |
if (exists $entry{'volume'}) { |
| 476 |
my $volume = $entry{'volume'}; |
| 477 |
if (not $volume =~ /^[1-9][0-9]*$/) { |
| 478 |
return "The format of the 'volume' is wrong" |
| 479 |
} |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
# Check the right format of the 'number.' |
| 484 |
sub check_number { |
| 485 |
my (%entry) = @_; |
| 486 |
if (exists $entry{'number'}) { |
| 487 |
my $number = $entry{'number'}; |
| 488 |
if (not $number =~ /^[1-9][0-9]*$/) { |
| 489 |
return "The format of the 'number' is wrong" |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
# Check the right format of the 'pages.' |
| 495 |
sub check_pages { |
| 496 |
my (%entry) = @_; |
| 497 |
if (exists $entry{'pages'}) { |
| 498 |
my $pages = $entry{'pages'}; |
| 499 |
if (not $pages =~ /^([1-9][0-9]*--[1-9][0-9]*|[1-9][0-9]*)$/) { |
| 500 |
return "The format of the 'pages' is wrong" |
| 501 |
} |
| 502 |
my @parts = split(/--/, $pages); |
| 503 |
if (@parts+0 eq 2) { |
| 504 |
if ($parts[0] eq $parts[1]) { |
| 505 |
return "The 'pages' mentions the same page twice, just use it once" |
| 506 |
} |
| 507 |
if ($parts[0] > $parts[1]) { |
| 508 |
return "The 'pages' are in the wrong order, since $parts[0] is greater than $parts[1]" |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
# Check unescaped symbols. |
| 515 |
sub check_unescaped_symbols { |
| 516 |
my (%entry) = @_; |
| 517 |
my @symbols = ( '&' ); |
| 518 |
foreach my $tag (keys %entry) { |
| 519 |
if ($tag =~ /^:.*/) { |
| 520 |
next; |
| 521 |
} |
| 522 |
my $value = $entry{$tag}; |
| 523 |
foreach my $s (@symbols) { |
| 524 |
if ($value =~ /^(.*[^\\{]|)\Q$s\E.*$/) { |
| 525 |
return "The '$tag' contains a dangerous unescaped symbol (wrap it in curled brackets if you really need it)" |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
# Check one entry. |
| 532 |
sub process_entry { |
| 533 |
my (%entry) = @_; |
| 534 |
my @checks; |
| 535 |
foreach my $entry (keys %bibcop::) { |
| 536 |
if ($entry =~ /^check_/) { |
| 537 |
push(@checks, $entry); |
| 538 |
} |
| 539 |
} |
| 540 |
my @sorted = sort @checks; |
| 541 |
my @errors; |
| 542 |
foreach my $check (@sorted) { |
| 543 |
no strict 'refs'; |
| 544 |
my $err = $check->(%entry); |
| 545 |
if (defined $err and $err ne '') { |
| 546 |
push(@errors, $err); |
| 547 |
} |
| 548 |
} |
| 549 |
return @errors; |
| 550 |
} |
| 551 |
|
| 552 |
# Fix one entry. |
| 553 |
sub entry_fix { |
| 554 |
my (%entry) = @_; |
| 555 |
if (not exists $entry{':type'}) { |
| 556 |
error("I don't know what to do with an entry without a type"); |
| 557 |
} |
| 558 |
my $type = $entry{':type'}; |
| 559 |
if (not exists $blessed{$type}) { |
| 560 |
error("I don't know what to do with \@$type type of BibTeX entry"); |
| 561 |
} |
| 562 |
if (not exists $entry{':name'}) { |
| 563 |
error("I don't know what to do with an entry without a name"); |
| 564 |
} |
| 565 |
my $tags = $blessed{$type}; |
| 566 |
my %allowed = map { $_ => 1 } @$tags; |
| 567 |
my @lines; |
| 568 |
if (exists $entry{'booktitle'} and $entry{':type'} eq 'article') { |
| 569 |
$entry{'journal'} = $entry{'booktitle'}; |
| 570 |
} |
| 571 |
if (exists $entry{'journal'} and $entry{':type'} eq 'inproceedings') { |
| 572 |
$entry{'booktitle'} = $entry{'journal'}; |
| 573 |
} |
| 574 |
foreach my $tag (keys %entry) { |
| 575 |
if ($tag =~ /^:/) { |
| 576 |
next; |
| 577 |
} |
| 578 |
my $value = clean_tex($entry{$tag}); |
| 579 |
if ($tag eq 'url') { |
| 580 |
my $today = strftime('%d-%m-%Y', localtime(time)); |
| 581 |
push(@lines, " howpublished = {\\url{$value}},"); |
| 582 |
push(@lines, " note = {[Online; accessed $today]},"); |
| 583 |
next; |
| 584 |
} |
| 585 |
if (not exists $allowed{$tag} and not exists $allowed{$tag . '?'}) { |
| 586 |
next; |
| 587 |
} |
| 588 |
my $fixer = "fix_$tag"; |
| 589 |
my $fixed = $value; |
| 590 |
if (defined &{$fixer}) { |
| 591 |
no strict 'refs'; |
| 592 |
$value = $fixer->($value); |
| 593 |
} |
| 594 |
$value = fix_unicode($value); |
| 595 |
if ($tag =~ /title|booktitle|journal/) { |
| 596 |
$value = '{' . $value . '}'; |
| 597 |
} |
| 598 |
if (not $value eq '') { |
| 599 |
push(@lines, " $tag = {$value},"); |
| 600 |
} |
| 601 |
} |
| 602 |
my $fixed = "\@$type\{$entry{':name'},\n"; |
| 603 |
my @sorted = sort @lines; |
| 604 |
foreach my $line (@sorted) { |
| 605 |
$fixed = $fixed . $line . "\n"; |
| 606 |
} |
| 607 |
$fixed = $fixed . "}\n\n"; |
| 608 |
return $fixed; |
| 609 |
} |
| 610 |
|
| 611 |
sub fix_author { |
| 612 |
my ($value) = @_; |
| 613 |
$value =~ s/\s{2,}/ /g; |
| 614 |
my @authors = split(/\s+and\s+/, $value); |
| 615 |
foreach my $author (@authors) { |
| 616 |
$author =~ s/^\s+|\s+$//g; |
| 617 |
if (index($author, ' {') != -1 or index($author, '} ') != -1) { |
| 618 |
next; |
| 619 |
} |
| 620 |
$author =~ s/ ([A-Z])($| )/ $1.$2/g; |
| 621 |
if (index($author, ',') == -1) { |
| 622 |
my @words = split(/\s+/, $author); |
| 623 |
my $total = @words+0; |
| 624 |
if ($total > 1) { |
| 625 |
$author = $words[$total - 1] . ', ' . join(' ', @words[0 .. $total - 2]); |
| 626 |
} |
| 627 |
} |
| 628 |
$author =~ s/([A-Z])\.(?![ ,])/$1. /g; |
| 629 |
$author =~ s/^\s+|\s+$//g; |
| 630 |
} |
| 631 |
return join(' and ', @authors); |
| 632 |
} |
| 633 |
|
| 634 |
sub fix_number { |
| 635 |
my ($value) = @_; |
| 636 |
$value =~ s/^0+//g; |
| 637 |
return $value; |
| 638 |
} |
| 639 |
|
| 640 |
sub fix_month { |
| 641 |
my ($value) = @_; |
| 642 |
my %months = ( |
| 643 |
'1' => 'jan|january', |
| 644 |
'2' => 'feb|february', |
| 645 |
'3' => 'mar|march', |
| 646 |
'4' => 'apr|april', |
| 647 |
'5' => 'may', |
| 648 |
'6' => 'jun|june', |
| 649 |
'7' => 'jul|july', |
| 650 |
'8' => 'aug|august', |
| 651 |
'9' => 'sep|september', |
| 652 |
'10' => 'oct|october', |
| 653 |
'11' => 'nov|november', |
| 654 |
'12' => 'dec|december', |
| 655 |
); |
| 656 |
$value =~ s/^(0| )+//g; |
| 657 |
$value =~ s/(0| )+$//g; |
| 658 |
while(my($v, $re) = each %months) { |
| 659 |
if ($value =~ qr/$re/i) { |
| 660 |
$value = $v; |
| 661 |
last; |
| 662 |
} |
| 663 |
} |
| 664 |
return $value; |
| 665 |
} |
| 666 |
|
| 667 |
sub fix_capitalization { |
| 668 |
my ($value) = @_; |
| 669 |
my @words = split(/\s+/, $value); |
| 670 |
my $pos = 0; |
| 671 |
foreach my $word (@words) { |
| 672 |
$pos += 1; |
| 673 |
if (not $word =~ /^[A-Za-z]/) { |
| 674 |
next; |
| 675 |
} |
| 676 |
my $start = 1; |
| 677 |
if ($pos > 1) { |
| 678 |
my $before = $words[$pos - 2]; |
| 679 |
if (not $before =~ /(:|\?|!|;|-)$/) { |
| 680 |
$start = 0; |
| 681 |
} |
| 682 |
} |
| 683 |
my @parts = split(/-/, $word, -1); |
| 684 |
my $p = 0; |
| 685 |
foreach my $part (@parts) { |
| 686 |
$p += 1; |
| 687 |
my $lcp = lc($part); |
| 688 |
my $head = $lcp; |
| 689 |
$head =~ s/[,\.!\?;:]$//g; |
| 690 |
if (exists $minors{$head}) { |
| 691 |
if ($p > 1) { |
| 692 |
my $pre = $parts[$p - 2]; |
| 693 |
if (not $pre eq '') { |
| 694 |
$part = $lcp; |
| 695 |
next; |
| 696 |
} |
| 697 |
} elsif (@parts+0 == 1) { |
| 698 |
if (not $start) { |
| 699 |
$part = $lcp; |
| 700 |
next; |
| 701 |
} |
| 702 |
} |
| 703 |
} |
| 704 |
$part =~ s/^([a-z])/\U$1/g; |
| 705 |
} |
| 706 |
$word = join('-', @parts); |
| 707 |
} |
| 708 |
return join(' ', @words); |
| 709 |
} |
| 710 |
|
| 711 |
sub fix_title { |
| 712 |
my ($value) = @_; |
| 713 |
$value = fix_capitalization($value); |
| 714 |
$value =~ s/([^ ])---/$1 ---/g; |
| 715 |
$value =~ s/---([^ ])/--- $1/g; |
| 716 |
return $value; |
| 717 |
} |
| 718 |
|
| 719 |
sub fix_pages { |
| 720 |
my ($value) = @_; |
| 721 |
if ($value =~ /^[1-9][0-9]*$/ || $value eq '') { |
| 722 |
return $value; |
| 723 |
} |
| 724 |
my ($left, $right) = split(/---|--|-|–|—|\s/, $value); |
| 725 |
if (!defined $left || $left eq '') { |
| 726 |
$left = $right; |
| 727 |
} |
| 728 |
if (!defined $right || $right eq '') { |
| 729 |
$right = $left; |
| 730 |
} |
| 731 |
$left =~ s/^0+//g; |
| 732 |
$right =~ s/^0+//g; |
| 733 |
if ($left !~ /^[0-9]*$/ or $right !~ /^[0-9]*$/) { |
| 734 |
return $value; |
| 735 |
} |
| 736 |
$left = $left + 0; |
| 737 |
$right = $right + 0; |
| 738 |
if ($left > $right) { |
| 739 |
my $tmp = $left; |
| 740 |
$left = $right; |
| 741 |
$right = $tmp; |
| 742 |
} |
| 743 |
if ($left eq $right) { |
| 744 |
return $left; |
| 745 |
} |
| 746 |
return $left . '--' . $right; |
| 747 |
} |
| 748 |
|
| 749 |
sub fix_booktitle { |
| 750 |
my ($value) = @_; |
| 751 |
$value = fix_capitalization($value); |
| 752 |
if (index($value, 'Proceedings ') != 0) { |
| 753 |
$value = 'Proceedings of the ' . $value; |
| 754 |
} |
| 755 |
$value =~ s/ (19|20)[0-9]{2} / /g; |
| 756 |
my @orgs = qw/ACM IEEE ACM\/IEEE IEEE\/ACM/; |
| 757 |
foreach my $org (@orgs) { |
| 758 |
$value =~ s/ \Q$org\E / /g; |
| 759 |
} |
| 760 |
my %numbers = ( |
| 761 |
'First' => '1st', |
| 762 |
'Second' => '2nd', |
| 763 |
'Third' => '3rd', |
| 764 |
'Fourth' => '4th', |
| 765 |
'Fifth' => '5th', |
| 766 |
'Sixth' => '6th', |
| 767 |
'Seventh' => '7th', |
| 768 |
'Eighth' => '8th', |
| 769 |
'Ninth' => '9th', |
| 770 |
'Tenth' => '10th' |
| 771 |
); |
| 772 |
keys %numbers; |
| 773 |
while(my($left, $right) = each %numbers) { |
| 774 |
$value =~ s/^Proceedings of the \Q$left\E /Proceedings of the $right /g; |
| 775 |
} |
| 776 |
return $value; |
| 777 |
} |
| 778 |
|
| 779 |
sub fix_journal { |
| 780 |
my ($value) = @_; |
| 781 |
$value = fix_capitalization($value); |
| 782 |
return $value; |
| 783 |
} |
| 784 |
|
| 785 |
sub fix_org_name { |
| 786 |
my ($value) = @_; |
| 787 |
my @orgs = qw/ACM IEEE/; |
| 788 |
foreach my $org (@orgs) { |
| 789 |
$value =~ s/^\Q$org\E($|[^A-Z0-9a-z].*$)/$org/g; |
| 790 |
} |
| 791 |
return $value; |
| 792 |
} |
| 793 |
|
| 794 |
sub fix_publisher { |
| 795 |
my ($value) = @_; |
| 796 |
$value = fix_capitalization($value); |
| 797 |
$value = fix_org_name($value); |
| 798 |
return $value; |
| 799 |
} |
| 800 |
|
| 801 |
sub fix_organization { |
| 802 |
my ($value) = @_; |
| 803 |
$value = fix_capitalization($value); |
| 804 |
$value = fix_org_name($value); |
| 805 |
return $value; |
| 806 |
} |
| 807 |
|
| 808 |
sub fix_unicode { |
| 809 |
my ($value) = @_; |
| 810 |
my %literals = ( |
| 811 |
'ò' => '\`{o}', 'ó' => '\\\'{o}', 'ô' => '\^{o}', 'ö' => '\"{o}', 'ő' => '\H{o}', 'ǒ' => '\v{o}', 'õ' => '\~{o}', |
| 812 |
'à' => '\`{a}', 'á' => '\\\'{a}', 'â' => '\^{a}', 'ä' => '\"{a}', 'å' => '\r{a}', 'ą' => '\k{a}', 'ǎ' => '\v{a}', 'ã' => '\~{a}', |
| 813 |
'ù' => '\`{u}', 'ú' => '\\\'{u}', 'û' => '\^{u}', 'ü' => '\"{u}', 'ů' => '\r{u}', 'ǔ' => '\v{u}', 'ũ' => '\~{u}', |
| 814 |
'ì' => '\`{i}', 'í' => '\\\'{i}', 'î' => '\^{i}', 'ï' => '\"{i}', 'ǐ' => '\v{i}', 'ĩ' => '\~{i}', |
| 815 |
'ń' => '\\\'{n}', 'ň' => '\v{n}', 'ñ' => '\~{n}', |
| 816 |
'ç' => '\c{c}', |
| 817 |
'ł' => '\l{}', |
| 818 |
'ı' => '{\i}', |
| 819 |
'ø' => '\o{}', |
| 820 |
'–' => '--', '—' => '---', |
| 821 |
'’' => '\'' |
| 822 |
); |
| 823 |
keys %literals; |
| 824 |
while(my($k, $v) = each %literals) { |
| 825 |
$value =~ s/\Q$k\E/$v/g; |
| 826 |
} |
| 827 |
return $value; |
| 828 |
} |
| 829 |
|
| 830 |
# Parse the incoming .bib file and return an array |
| 831 |
# of hash-maps, where each one is a bibentry. |
| 832 |
sub entries { |
| 833 |
my ($bib) = @_; |
| 834 |
my @entries; |
| 835 |
my $s = 'top'; |
| 836 |
my %entry; |
| 837 |
my $acc = ''; |
| 838 |
my $tag = ''; |
| 839 |
my $interrupted = ''; # where the comment interrupted the proceeding (state name) |
| 840 |
my $lineno = 0; |
| 841 |
my $nest = 0; |
| 842 |
my $escape = 0; |
| 843 |
for my $pos (0..length($bib)-1) { |
| 844 |
my $char = substr($bib, $pos, 1); |
| 845 |
if ($char eq ' ') { |
| 846 |
# ignore the white space |
| 847 |
} elsif ($char eq '%' and not($s eq 'quote')) { |
| 848 |
if ($pos eq 0 or substr($bib, $pos - 1, 1) ne '\\') { |
| 849 |
$interrupted = $s; |
| 850 |
$s = 'comment'; |
| 851 |
} |
| 852 |
} elsif ($char eq "\n") { |
| 853 |
# ignore the EOL |
| 854 |
$lineno = $lineno + 1; |
| 855 |
if ($s eq 'comment') { |
| 856 |
$s = $interrupted; |
| 857 |
} |
| 858 |
} elsif ($s eq 'comment') { |
| 859 |
# ignore the comment |
| 860 |
} elsif ($s eq 'top') { |
| 861 |
if ($char eq '@') { |
| 862 |
%entry = (); |
| 863 |
$s = 'start'; |
| 864 |
$acc = ''; |
| 865 |
} else { |
| 866 |
warning("Each BibTeX entry must start with '\@', what is '$char' at line no.$lineno?"); |
| 867 |
last; |
| 868 |
} |
| 869 |
} elsif ($char =~ /[a-zA-Z]/ and $s eq 'start') { |
| 870 |
# @article |
| 871 |
} elsif ($char eq '{' and $s eq 'start') { |
| 872 |
$entry{':type'} = substr($acc, 1); |
| 873 |
$acc = ''; |
| 874 |
$s = 'body'; |
| 875 |
} elsif ($char =~ /[a-zA-Z0-9]/ and $s eq 'body') { |
| 876 |
$acc = ''; |
| 877 |
$s = 'tag'; |
| 878 |
} elsif ($char =~ /[a-zA-Z0-9_\.\-\/:]/ and $s eq 'tag') { |
| 879 |
# reading the tag |
| 880 |
} elsif ($char =~ /[a-zA-Z0-9]/ and $s eq 'value') { |
| 881 |
# reading the value without quotes or brackets |
| 882 |
} elsif ($char eq ',' and $s eq 'tag') { |
| 883 |
$entry{':name'} = $acc; |
| 884 |
$s = 'body'; |
| 885 |
} elsif ($char eq '=' and $s eq 'tag') { |
| 886 |
my $t = lc($acc); |
| 887 |
if (exists $entry{$t}) { |
| 888 |
warning("The tag '$t' is seen more than once at line no.$lineno"); |
| 889 |
} |
| 890 |
$tag = $t; |
| 891 |
$s = 'value'; |
| 892 |
$acc = ''; |
| 893 |
} elsif ($char eq ',' and $s eq 'value') { |
| 894 |
if (not exists $entry{$tag}) { |
| 895 |
my $tex = substr($acc, 1); |
| 896 |
$tex =~ s/\s//g; |
| 897 |
$entry{$tag} = $tex; |
| 898 |
} |
| 899 |
$s = 'body'; |
| 900 |
} elsif ($char eq '}' and $s eq 'body') { |
| 901 |
push(@entries, { %entry }); |
| 902 |
$s = 'top'; |
| 903 |
} elsif ($char eq '}' and $s eq 'value') { |
| 904 |
if (not exists $entry{$tag}) { |
| 905 |
my $tex = substr($acc, 1); |
| 906 |
$tex =~ s/\s//g; |
| 907 |
$entry{$tag} = $tex; |
| 908 |
} |
| 909 |
push(@entries, { %entry }); |
| 910 |
$s = 'top'; |
| 911 |
} elsif ($char eq '}' and $s eq 'tag') { |
| 912 |
$entry{':name'} = $acc; |
| 913 |
push(@entries, { %entry }); |
| 914 |
$s = 'top'; |
| 915 |
} elsif ($char eq '"' and $s eq 'value') { |
| 916 |
$s = 'quote'; |
| 917 |
$acc = ''; |
| 918 |
} elsif ($char eq '"' and $s eq 'quote') { |
| 919 |
$entry{$tag} = substr($acc, 1); |
| 920 |
$s = 'value'; |
| 921 |
} elsif ($s eq 'quote') { |
| 922 |
# nothing |
| 923 |
} elsif ($char eq '{' and $s eq 'value') { |
| 924 |
$nest = 1; |
| 925 |
$s = 'brackets'; |
| 926 |
$acc = ''; |
| 927 |
} elsif ($s eq 'brackets') { |
| 928 |
if ($char eq '\\') { |
| 929 |
$escape = 1; |
| 930 |
} elsif ($char eq '{' and $escape != 1) { |
| 931 |
$nest = $nest + 1; |
| 932 |
} elsif ($char eq '}' and $escape != 1) { |
| 933 |
$nest = $nest - 1; |
| 934 |
if ($nest == 0) { |
| 935 |
$entry{$tag} = substr($acc, 1); |
| 936 |
$s = 'value'; |
| 937 |
} |
| 938 |
} |
| 939 |
$escape = 0; |
| 940 |
} else { |
| 941 |
warning("It is impossible to parse the .bib file, because I do not know what to do with '$char' at line no.$lineno (s=$s)"); |
| 942 |
last; |
| 943 |
} |
| 944 |
if ($char eq ' ' and not($s =~ /quote|brackets/)) { |
| 945 |
next; |
| 946 |
} |
| 947 |
$acc = $acc . $char; |
| 948 |
} |
| 949 |
return @entries; |
| 950 |
} |
| 951 |
|
| 952 |
# Takes the text and returns only list of words seen there. |
| 953 |
sub only_words { |
| 954 |
my ($tex) = @_; |
| 955 |
my $t = clean_tex($tex); |
| 956 |
$t =~ s/([^a-zA-Z0-9\\'])/ $1 /g; |
| 957 |
$t =~ s/- +- +-/---/g; |
| 958 |
$t =~ s/{ /{/g; |
| 959 |
$t =~ s/ }/}/g; |
| 960 |
return split(/ +/, $t); |
| 961 |
} |
| 962 |
|
| 963 |
# Take a TeX string and return a cleaner one, without redundant spaces, brackets, etc. |
| 964 |
sub clean_tex { |
| 965 |
my ($tex) = @_; |
| 966 |
$tex =~ s/\s+/ /g; |
| 967 |
$tex =~ s/^\s+//g; |
| 968 |
$tex =~ s/\s+$//g; |
| 969 |
while ($tex =~ s/^\{(.+)\}$/$1/g) {}; |
| 970 |
return $tex; |
| 971 |
} |
| 972 |
|
| 973 |
# Turn a number into a position, like 1 -> 1st, 2 -> 2nd, 3 -> 3rd, 4 -> 4th, and so on. |
| 974 |
sub as_position { |
| 975 |
my ($i) = @_; |
| 976 |
my $txt; |
| 977 |
if ($i == 1) { |
| 978 |
$txt = '1st'; |
| 979 |
} elsif ($i == 2) { |
| 980 |
$txt = '2nd'; |
| 981 |
} elsif ($i == 3) { |
| 982 |
$txt = '3rd'; |
| 983 |
} else { |
| 984 |
$txt = "${i}th"; |
| 985 |
} |
| 986 |
return "the $txt"; |
| 987 |
} |
| 988 |
|
| 989 |
# Take a bibentry and print all its tags as a comma-separated string. |
| 990 |
sub listed_tags { |
| 991 |
my (%entry) = @_; |
| 992 |
my @list; |
| 993 |
foreach my $tag (keys %entry) { |
| 994 |
if ($tag =~ /^:.*/) { |
| 995 |
next; |
| 996 |
} |
| 997 |
push(@list, $tag); |
| 998 |
} |
| 999 |
my @sorted = sort @list; |
| 1000 |
return '(' . join(', ', @sorted) . ')'; |
| 1001 |
} |
| 1002 |
|
| 1003 |
# Make sure the text can safely be rendered in TeX. |
| 1004 |
sub escape_tex { |
| 1005 |
my ($tex) = @_; |
| 1006 |
$tex =~ s/[^a-zA-Z0-9-.,+)(:;@ '"]/?/g; |
| 1007 |
return $tex; |
| 1008 |
} |
| 1009 |
|
| 1010 |
# Print ERROR message to the console and die. |
| 1011 |
sub error { |
| 1012 |
my ($txt) = @_; |
| 1013 |
if (exists $args{'--latex'}) { |
| 1014 |
print "\\PackageError{bibcop}{" . escape_tex($txt). "}{}\n"; |
| 1015 |
} else { |
| 1016 |
print STDERR $txt . "\n"; |
| 1017 |
} |
| 1018 |
fail(); |
| 1019 |
} |
| 1020 |
|
| 1021 |
# Print DEBUG message to the console. |
| 1022 |
sub debug { |
| 1023 |
my ($txt) = @_; |
| 1024 |
if (exists $args{'--verbose'}) { |
| 1025 |
if (exists $args{'--latex'}) { |
| 1026 |
print "\\message{bibcop: " . escape_tex($txt) . "^^J}\n"; |
| 1027 |
} else { |
| 1028 |
print $txt . "\n"; |
| 1029 |
} |
| 1030 |
} |
| 1031 |
} |
| 1032 |
|
| 1033 |
# Print INFO message to the console. |
| 1034 |
sub info { |
| 1035 |
my ($txt) = @_; |
| 1036 |
if (exists $args{'--latex'}) { |
| 1037 |
print '% '; |
| 1038 |
} |
| 1039 |
print $txt . "\n"; |
| 1040 |
} |
| 1041 |
|
| 1042 |
# Print INFO message to the console. |
| 1043 |
sub warning { |
| 1044 |
my ($txt) = @_; |
| 1045 |
if (exists $args{'--latex'}) { |
| 1046 |
print "\\PackageWarningNoLine{bibcop}{" . escape_tex($txt) . "}\n"; |
| 1047 |
} else { |
| 1048 |
print $txt . "\n"; |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
sub fail { |
| 1053 |
if (exists $args{'--latex'}) { |
| 1054 |
exit(0); |
| 1055 |
} else { |
| 1056 |
exit(1); |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
my $script = basename($0); |
| 1061 |
$script =~ s/\.pl$//g; |
| 1062 |
if (not $script eq 'bibcop') { |
| 1063 |
goto END; |
| 1064 |
} |
| 1065 |
|
| 1066 |
if (@ARGV+0 eq 0 or exists $args{'--help'} or exists $args{'-?'}) { |
| 1067 |
info("Bibcop is a style checker of BibTeX files (.bib)\n\n" . |
| 1068 |
"Usage:\n" . |
| 1069 |
" bibcop [<options>] <.bib file path>\n\n" . |
| 1070 |
"Options:\n" . |
| 1071 |
" -v, --version Print the current version of Bibcop and exit\n" . |
| 1072 |
" -?, --help Print this help screen\n" . |
| 1073 |
" --fix Fix the errors and print a new version of the .bib file to the console\n" . |
| 1074 |
" -i, --in-place When used together with the --fix, modifies the file in place, doesn't print it to the console\n" . |
| 1075 |
" --verbose Print supplementary debugging information\n" . |
| 1076 |
" --no:XXX Disable one of the following checks (e.g. --no:wraps):\n" . |
| 1077 |
" tags Only some tags are allowed, while some of them are mandatory\n" . |
| 1078 |
" caps All major words in titles and booktitles must be capitalized\n" . |
| 1079 |
" wraps Double curly braces are required around titles and booktitles\n" . |
| 1080 |
" doi The presence of the 'doi' tag is mandatory in all entries\n" . |
| 1081 |
" inproc The booktitle of \@inproceedings must start with 'Proceedings of the'\n" . |
| 1082 |
" org The booktitle may not mention ACM or IEEE\n" . |
| 1083 |
" --latex Report errors in LaTeX format using the \\PackageWarningNoLine command\n\n" . |
| 1084 |
"If any issues, please, report to GitHub: https://github.com/yegor256/bibcop"); |
| 1085 |
} elsif (exists $args{'--version'} or exists $args{'-v'}) { |
| 1086 |
info('0.0.32 2025/04/28'); |
| 1087 |
} else { |
| 1088 |
my ($file) = grep { not($_ =~ /^-.*$/) } @ARGV; |
| 1089 |
if (not $file) { |
| 1090 |
error('File name must be specified'); |
| 1091 |
} |
| 1092 |
open(my $fh, '<', $file) or error('Cannot open file: ' . $file); |
| 1093 |
my $bib; { local $/; $bib = <$fh>; } |
| 1094 |
my @entries = entries($bib); |
| 1095 |
if (exists $args{'--fix'}) { |
| 1096 |
my $fixed = ''; |
| 1097 |
my %seen; |
| 1098 |
for my $i (0..(@entries+0 - 1)) { |
| 1099 |
my %entry = %{ $entries[$i] }; |
| 1100 |
my $name = $entry{':name'}; |
| 1101 |
if (exists $seen{$name}) { |
| 1102 |
next; |
| 1103 |
} |
| 1104 |
$fixed = $fixed . entry_fix(%entry); |
| 1105 |
$seen{$name} = 1; |
| 1106 |
} |
| 1107 |
my ($head) = $bib =~ /^((?:%.*\n)+\n*)/; |
| 1108 |
$fixed = $head . $fixed; |
| 1109 |
if (exists $args{'-i'} or exists $args{'--in-place'}) { |
| 1110 |
open(my $out, '>', $file) or error('Cannot open file for writing: ' . $file); |
| 1111 |
print $out $fixed; |
| 1112 |
close($out); |
| 1113 |
} else { |
| 1114 |
info($fixed); |
| 1115 |
} |
| 1116 |
} else { |
| 1117 |
debug((@entries+0) . ' entries found in ' . $file); |
| 1118 |
my $found = 0; |
| 1119 |
for my $i (0..(@entries+0 - 1)) { |
| 1120 |
my %entry = %{ $entries[$i] }; |
| 1121 |
debug("Checking $entry{':name'} (no.$i)..."); |
| 1122 |
foreach my $err (process_entry(%entry)) { |
| 1123 |
warning("$err, in the '$entry{':name'}' entry"); |
| 1124 |
$found += 1; |
| 1125 |
} |
| 1126 |
} |
| 1127 |
if ($found > 0) { |
| 1128 |
debug("$found problem(s) found"); |
| 1129 |
fail(); |
| 1130 |
} |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
END: |
| 1135 |
|
| 1136 |
# In order to finish it with success: |
| 1137 |
1; |