| 1 |
#!/usr/bin/env texlua |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
local function exists(a, hit) |
| 30 |
for _, v in ipairs(a) do |
| 31 |
if v == hit then |
| 32 |
return true |
| 33 |
end |
| 34 |
end |
| 35 |
return false |
| 36 |
end |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
local function parse(map, args) |
| 44 |
local keys, key, unknown = {}, 'unpaired', {} |
| 45 |
local a, b |
| 46 |
for _, v in ipairs(args) do |
| 47 |
a, _, b = string.find(v, '^%-(%w)$') |
| 48 |
if a then |
| 49 |
for _, x in ipairs(map) do |
| 50 |
key = 'unpaired' |
| 51 |
if x['short'] == b then |
| 52 |
key = x['long'] |
| 53 |
break |
| 54 |
end |
| 55 |
end |
| 56 |
if key == 'unpaired' then |
| 57 |
table.insert(unknown, '-' .. b) |
| 58 |
end |
| 59 |
if not keys[key] then |
| 60 |
keys[key] = {} |
| 61 |
end |
| 62 |
else |
| 63 |
a, _, b = string.find(v, '^%-%-([%w-]+)$') |
| 64 |
if a then |
| 65 |
for _, x in ipairs(map) do |
| 66 |
key = 'unpaired' |
| 67 |
if x['long'] == b then |
| 68 |
key = b |
| 69 |
break |
| 70 |
end |
| 71 |
end |
| 72 |
if key == 'unpaired' then |
| 73 |
if not exists(unknown, '--' .. b) then |
| 74 |
table.insert(unknown, '--' .. b) |
| 75 |
end |
| 76 |
end |
| 77 |
if not keys[key] then |
| 78 |
keys[key] = {} |
| 79 |
end |
| 80 |
else |
| 81 |
if not keys[key] then |
| 82 |
keys[key] = {} |
| 83 |
end |
| 84 |
if key ~= 'unpaired' then |
| 85 |
for _, x in ipairs(map) do |
| 86 |
if x['long'] == key then |
| 87 |
if not (x['argument'] and |
| 88 |
#keys[key] == 0) then |
| 89 |
key = 'unpaired' |
| 90 |
end |
| 91 |
break |
| 92 |
end |
| 93 |
end |
| 94 |
if not keys[key] then |
| 95 |
keys[key] = {} |
| 96 |
end |
| 97 |
table.insert(keys[key], v) |
| 98 |
else |
| 99 |
if not keys[key] then |
| 100 |
keys[key] = {} |
| 101 |
end |
| 102 |
table.insert(keys[key], v) |
| 103 |
end |
| 104 |
end |
| 105 |
end |
| 106 |
end |
| 107 |
return keys, unknown |
| 108 |
end |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
local function difference(a, b) |
| 115 |
local result = {} |
| 116 |
for _, v in ipairs(a) do |
| 117 |
if not exists(b, v) then |
| 118 |
table.insert(result, v) |
| 119 |
end |
| 120 |
end |
| 121 |
return result |
| 122 |
end |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
local function split(str, pattern) |
| 128 |
local result = {} |
| 129 |
string.gsub(str, pattern, function(a) |
| 130 |
table.insert(result, a) end) |
| 131 |
return result |
| 132 |
end |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
local function read(file) |
| 138 |
local handler = io.open(file, 'r') |
| 139 |
local lines = {} |
| 140 |
if handler then |
| 141 |
for line in handler:lines() do |
| 142 |
table.insert(lines, line) |
| 143 |
end |
| 144 |
handler:close() |
| 145 |
end |
| 146 |
return lines |
| 147 |
end |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
local function plural(i, a, b) |
| 155 |
if i == 1 then |
| 156 |
return a |
| 157 |
else |
| 158 |
return b |
| 159 |
end |
| 160 |
end |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
local function normalize(str) |
| 166 |
local result, _ = string.gsub(str, '^%s', '') |
| 167 |
result, _ = string.gsub(result, '%s$', '') |
| 168 |
return result |
| 169 |
end |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
local function blacklist(a) |
| 175 |
local list = {} |
| 176 |
for _, v in ipairs(list) do |
| 177 |
if v == a then |
| 178 |
return true |
| 179 |
end |
| 180 |
end |
| 181 |
return false |
| 182 |
end |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
local function allowed(key) |
| 188 |
local keys = { 'string', 'comment' } |
| 189 |
for _, v in ipairs(keys) do |
| 190 |
if string.lower(key) == v then |
| 191 |
return false |
| 192 |
end |
| 193 |
end |
| 194 |
return true |
| 195 |
end |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
local function extract(lines) |
| 201 |
local result = {} |
| 202 |
for _, line in ipairs(lines) do |
| 203 |
local key, hit = string.match(line, |
| 204 |
'^%s*%@(%w+%s*){%s*(.+),') |
| 205 |
if key and allowed(key) then |
| 206 |
if not exists(result, hit) then |
| 207 |
hit = normalize(hit) |
| 208 |
table.insert(result, hit) |
| 209 |
end |
| 210 |
end |
| 211 |
end |
| 212 |
return result |
| 213 |
end |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
local function crossref(lines) |
| 220 |
local result, lookup, key, hit = {}, '' |
| 221 |
for _, line in ipairs(lines) do |
| 222 |
key, hit = string.match(line, |
| 223 |
'^%s*%@(%w+%s*){%s*(.+),') |
| 224 |
if key and allowed(key) then |
| 225 |
lookup = normalize(hit) |
| 226 |
else |
| 227 |
key, hit = string.match(line, |
| 228 |
'^%s*(%w+)%s*=%s*(.+)$') |
| 229 |
if key then |
| 230 |
key = string.lower(key) |
| 231 |
if key == 'crossref' then |
| 232 |
if string.sub(hit, -1) == ',' then |
| 233 |
hit = string.sub(hit, 2, -3) |
| 234 |
else |
| 235 |
hit = string.sub(hit, 2, -2) |
| 236 |
end |
| 237 |
result[lookup] = hit |
| 238 |
end |
| 239 |
end |
| 240 |
end |
| 241 |
end |
| 242 |
return result |
| 243 |
end |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
local function sanitize(file, extension) |
| 250 |
extension = '.' .. extension |
| 251 |
if string.sub(file, -#extension) ~= extension then |
| 252 |
file = file .. extension |
| 253 |
end |
| 254 |
return file |
| 255 |
end |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
local function valid(file) |
| 261 |
local handler = io.open(file, 'r') |
| 262 |
if handler then |
| 263 |
handler:close() |
| 264 |
return true |
| 265 |
else |
| 266 |
return false |
| 267 |
end |
| 268 |
end |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
local function wrap(str, size) |
| 275 |
local parts = split(str, '[^%s]+') |
| 276 |
local r, l = '', '' |
| 277 |
for _, v in ipairs(parts) do |
| 278 |
if (#l + #v) > size then |
| 279 |
r = r .. '\n' .. l |
| 280 |
l = v |
| 281 |
else |
| 282 |
l = normalize(l .. ' ' .. v) |
| 283 |
end |
| 284 |
end |
| 285 |
r = normalize(r .. '\n' .. l) |
| 286 |
return r |
| 287 |
end |
| 288 |
|
| 289 |
|
| 290 |
local backends = {} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
backends.bibtex = function(lines, rec) |
| 299 |
local citations, bibliography, invalid = {}, {}, {} |
| 300 |
local asterisk, parts, hit = false |
| 301 |
for _, line in ipairs(lines) do |
| 302 |
hit = string.match(line, '^%s*\\citation{(.+)}$') |
| 303 |
if hit then |
| 304 |
if hit ~= '*' then |
| 305 |
parts = split(hit, '[^,%s]+') |
| 306 |
for _, v in ipairs(parts) do |
| 307 |
v = normalize(v) |
| 308 |
if not exists(citations, v) then |
| 309 |
table.insert(citations, v) |
| 310 |
end |
| 311 |
end |
| 312 |
else |
| 313 |
asterisk = true |
| 314 |
end |
| 315 |
else |
| 316 |
hit = string.match(line, '^%s*\\bibdata{(.+)}$') |
| 317 |
if hit then |
| 318 |
parts = split(hit, '[^,%s]+') |
| 319 |
for _, v in ipairs(parts) do |
| 320 |
v = normalize(v) |
| 321 |
if not exists(bibliography, v) and |
| 322 |
not blacklist(v) then |
| 323 |
table.insert(bibliography, v) |
| 324 |
end |
| 325 |
end |
| 326 |
else |
| 327 |
hit = string.match(line, '^%s*\\@input{(.+)}$') |
| 328 |
if rec and hit then |
| 329 |
hit = sanitize(hit, 'aux') |
| 330 |
if not valid(hit) then |
| 331 |
table.insert(invalid, hit) |
| 332 |
else |
| 333 |
local a, b, c = backends.bibtex(read(hit), false) |
| 334 |
asterisk = asterisk or a |
| 335 |
for _, v in ipairs(b) do |
| 336 |
if not exists(citations, v) then |
| 337 |
table.insert(citations, v) |
| 338 |
end |
| 339 |
end |
| 340 |
for _, v in ipairs(c) do |
| 341 |
if not exists(bibliography, v) then |
| 342 |
table.insert(bibliography, v) |
| 343 |
end |
| 344 |
end |
| 345 |
end |
| 346 |
end |
| 347 |
end |
| 348 |
end |
| 349 |
end |
| 350 |
if #invalid ~= 0 then |
| 351 |
print() |
| 352 |
print(wrap('Warning: there ' .. plural(#invalid, |
| 353 |
'is an invalid reference ', 'are ' .. |
| 354 |
'invalid references ') .. 'to the ' .. |
| 355 |
'following auxiliary ' .. plural(#invalid, |
| 356 |
'file ', 'files ') .. 'that could not ' .. |
| 357 |
'be resolved at runtime:', 74)) |
| 358 |
for _, v in ipairs(invalid) do |
| 359 |
print('=> ' .. v) |
| 360 |
end |
| 361 |
end |
| 362 |
return asterisk, citations, bibliography |
| 363 |
end |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
backends.biber = function(lines, _) |
| 372 |
local citations, bibliography = {}, {} |
| 373 |
local asterisk, parts, hit = false |
| 374 |
for _, line in ipairs(lines) do |
| 375 |
hit = string.match(line, '^%s*<bcf:citekey order="%d+" ' .. |
| 376 |
'intorder="%d+">(.+)</bcf:citekey>$') |
| 377 |
if hit then |
| 378 |
if hit ~= '*' then |
| 379 |
parts = split(hit, '[^,%s]+') |
| 380 |
for _, v in ipairs(parts) do |
| 381 |
v = normalize(v) |
| 382 |
if not exists(citations, v) then |
| 383 |
table.insert(citations, v) |
| 384 |
end |
| 385 |
end |
| 386 |
else |
| 387 |
asterisk = true |
| 388 |
end |
| 389 |
else |
| 390 |
hit = string.match(line, '^%s*<bcf:datasource type="file" ' .. |
| 391 |
'datatype="%w+".*>(.+)</bcf:datasource>$') |
| 392 |
if hit then |
| 393 |
parts = split(hit, '[^,%s]+') |
| 394 |
for _, v in ipairs(parts) do |
| 395 |
v = normalize(v) |
| 396 |
if not exists(bibliography, v) and |
| 397 |
not blacklist(v) then |
| 398 |
table.insert(bibliography, v) |
| 399 |
end |
| 400 |
end |
| 401 |
end |
| 402 |
end |
| 403 |
end |
| 404 |
return asterisk, citations, bibliography |
| 405 |
end |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
local function count(t) |
| 411 |
local counter = 0 |
| 412 |
for _, _ in pairs(t) do |
| 413 |
counter = counter + 1 |
| 414 |
end |
| 415 |
return counter |
| 416 |
end |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
local function pad(c, size) |
| 423 |
local r = c |
| 424 |
while #r < size do |
| 425 |
r = r .. c |
| 426 |
end |
| 427 |
return r |
| 428 |
end |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
local function flatten(t) |
| 434 |
local result = {} |
| 435 |
for _, v in ipairs(t) do |
| 436 |
for _, k in ipairs(v) do |
| 437 |
if not exists(result, k) then |
| 438 |
table.insert(result, k) |
| 439 |
end |
| 440 |
end |
| 441 |
end |
| 442 |
return result |
| 443 |
end |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
local function organize(t) |
| 449 |
local result = {} |
| 450 |
for _, v in ipairs(t) do |
| 451 |
for j, k in pairs(v) do |
| 452 |
if not result[j] then |
| 453 |
result[j] = k |
| 454 |
end |
| 455 |
end |
| 456 |
end |
| 457 |
return result |
| 458 |
end |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
local function apply(c, f) |
| 465 |
local result = {} |
| 466 |
for _, v in ipairs(c) do |
| 467 |
table.insert(result, f(v)) |
| 468 |
end |
| 469 |
return result |
| 470 |
end |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
local function lookup(library, file, extension) |
| 478 |
return library.find_file(file, extension) |
| 479 |
end |
| 480 |
|
| 481 |
|
| 482 |
local function header() |
| 483 |
print(" _ _ _ _") |
| 484 |
print(" ___| |_ ___ ___| |_ ___|_| |_ ___ ___") |
| 485 |
print("| _| | -_| _| '_| _| | _| -_|_ -|") |
| 486 |
print("|___|_|_|___|___|_,_|___|_|_| |___|___|") |
| 487 |
print() |
| 488 |
print(wrap('checkcites.lua -- a reference ' .. |
| 489 |
'checker script (v2.8)', 74)) |
| 490 |
print(wrap('Copyright (c) 2012, 2019, Enrico Gregorio, Paulo Cereda', 74)) |
| 491 |
print(wrap('Copyright (c) 2024, Enrico Gregorio, Island of TeX', 74)) |
| 492 |
end |
| 493 |
|
| 494 |
|
| 495 |
local operations = {} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
operations.unused = function(citations, references, crossrefs) |
| 503 |
print() |
| 504 |
print(pad('-', 74)) |
| 505 |
print(wrap('Report of unused references in your TeX ' .. |
| 506 |
'document (that is, references present in ' .. |
| 507 |
'bibliography files, but not cited in ' .. |
| 508 |
'the TeX source file)', 74)) |
| 509 |
print(pad('-', 74)) |
| 510 |
|
| 511 |
local z = {} |
| 512 |
for _, citation in ipairs(citations) do |
| 513 |
if crossrefs[citation] then |
| 514 |
table.insert(z, crossrefs[citation]) |
| 515 |
end |
| 516 |
end |
| 517 |
|
| 518 |
for _, i in ipairs(z) do |
| 519 |
if not exists(i, citations) then |
| 520 |
table.insert(citations, i) |
| 521 |
end |
| 522 |
end |
| 523 |
|
| 524 |
local r = difference(references, citations) |
| 525 |
local forJson = {} |
| 526 |
print() |
| 527 |
print(wrap('Unused references in your TeX document: ' .. |
| 528 |
tostring(#r), 74)) |
| 529 |
if #r == 0 then |
| 530 |
return 0, forJson |
| 531 |
else |
| 532 |
for _, v in ipairs(r) do |
| 533 |
print('=> ' .. v) |
| 534 |
table.insert(forJson, v) |
| 535 |
end |
| 536 |
return 1, forJson |
| 537 |
end |
| 538 |
end |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
operations.undefined = function(citations, references, crossrefs) |
| 546 |
print() |
| 547 |
print(pad('-', 74)) |
| 548 |
print(wrap('Report of undefined references in your TeX ' .. |
| 549 |
'document (that is, references cited in the ' .. |
| 550 |
'TeX source file, but not present in the ' .. |
| 551 |
'bibliography files)', 74)) |
| 552 |
print(pad('-', 74)) |
| 553 |
|
| 554 |
local z = {} |
| 555 |
for _, citation in ipairs(citations) do |
| 556 |
if crossrefs[citation] then |
| 557 |
table.insert(z, crossrefs[citation]) |
| 558 |
end |
| 559 |
end |
| 560 |
|
| 561 |
for _, i in ipairs(z) do |
| 562 |
if not exists(i, citations) then |
| 563 |
table.insert(citations, i) |
| 564 |
end |
| 565 |
end |
| 566 |
|
| 567 |
local r = difference(citations, references) |
| 568 |
local forJson = {} |
| 569 |
print() |
| 570 |
print(wrap('Undefined references in your TeX document: ' .. |
| 571 |
tostring(#r), 74)) |
| 572 |
if #r == 0 then |
| 573 |
return 0, forJson |
| 574 |
else |
| 575 |
for _, v in ipairs(r) do |
| 576 |
print('=> ' .. v) |
| 577 |
table.insert(forJson, v) |
| 578 |
end |
| 579 |
return 1, forJson |
| 580 |
end |
| 581 |
end |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
operations.all = function(citations, references, crossrefs) |
| 589 |
local x, y |
| 590 |
local forJson = {} |
| 591 |
x, forJson['unused'] = operations.unused(citations, references, crossrefs) |
| 592 |
y, forJson['undefined'] = operations.undefined(citations, references, crossrefs) |
| 593 |
if x + y > 0 then |
| 594 |
return 1, forJson |
| 595 |
else |
| 596 |
return 0, forJson |
| 597 |
end |
| 598 |
end |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
local function validate(files, lib, enabled, extension) |
| 608 |
local bad, good = {}, {} |
| 609 |
for _, v in ipairs(files) do |
| 610 |
if not valid(v) then |
| 611 |
if enabled and lookup(lib, v, extension) then |
| 612 |
table.insert(good, lookup(lib, v, extension)) |
| 613 |
else |
| 614 |
table.insert(bad, v) |
| 615 |
end |
| 616 |
else |
| 617 |
table.insert(good, v) |
| 618 |
end |
| 619 |
end |
| 620 |
return bad, good |
| 621 |
end |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
local function toArray(elements) |
| 628 |
if #elements ~=0 then |
| 629 |
return '[ "' .. table.concat(elements, '", "') .. '" ]' |
| 630 |
else |
| 631 |
return '[]' |
| 632 |
end |
| 633 |
end |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
local function getOperation(check) |
| 639 |
if check == 'unused' then |
| 640 |
return 'list only unused references' |
| 641 |
elseif check == 'undefined' then |
| 642 |
return 'list only undefined references' |
| 643 |
else |
| 644 |
return 'list all unused and undefined references' |
| 645 |
end |
| 646 |
end |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
local function write(file, text) |
| 652 |
local handler = io.open(file, 'w') |
| 653 |
if handler then |
| 654 |
handler:write(text) |
| 655 |
handler:close() |
| 656 |
end |
| 657 |
end |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
local function toJson(file, schema) |
| 663 |
local string = '{\n' |
| 664 |
string = string .. ' "settings" : {\n' |
| 665 |
string = string .. ' "backend" : "' .. schema['backend'] .. '",\n' |
| 666 |
string = string .. ' "operation" : "' .. getOperation(schema['check']) .. '",\n' |
| 667 |
string = string .. ' "crossrefs" : ' .. ((schema['crossrefs'] and 'true') or 'false') .. '\n' |
| 668 |
string = string .. ' },\n' |
| 669 |
string = string .. ' "project" : {\n' |
| 670 |
string = string .. ' "forcibly_cite_all" : ' .. schema['asterisk'] .. ',\n' |
| 671 |
string = string .. ' "bibliographies" : ' .. toArray(schema['bibliographies']) .. ',\n' |
| 672 |
string = string .. ' "citations" : ' .. toArray(schema['citations']) .. ',\n' |
| 673 |
string = string .. ' "crossrefs" : ' .. toArray(schema['crossrefs'] or {}) .. '\n' |
| 674 |
string = string .. ' },\n' |
| 675 |
string = string .. ' "results" : {\n' |
| 676 |
string = string .. ' "unused" : {\n' |
| 677 |
string = string .. ' "active" : ' .. (exists({'unused', 'all'}, schema['check']) |
| 678 |
and 'true' or 'false') .. ',\n' |
| 679 |
string = string .. ' "occurrences" : ' .. toArray(schema['unused']) .. '\n' |
| 680 |
string = string .. ' },\n' |
| 681 |
string = string .. ' "undefined" : {\n' |
| 682 |
string = string .. ' "active" : ' .. (exists({'undefined', 'all'}, schema['check']) |
| 683 |
and 'true' or 'false') .. ',\n' |
| 684 |
string = string .. ' "occurrences" : ' .. toArray(schema['undefined']) .. '\n' |
| 685 |
string = string .. ' }\n' |
| 686 |
string = string .. ' }\n' |
| 687 |
string = string .. '}' |
| 688 |
write(file, string) |
| 689 |
end |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
local function checkcites(args) |
| 695 |
|
| 696 |
local kpse = require('kpse') |
| 697 |
kpse.set_program_name('texlua') |
| 698 |
|
| 699 |
header() |
| 700 |
|
| 701 |
local parameters = { |
| 702 |
{ short = 'a', long = 'all', argument = false }, |
| 703 |
{ short = 'u', long = 'unused', argument = false }, |
| 704 |
{ short = 'U', long = 'undefined', argument = false }, |
| 705 |
{ short = 'v', long = 'version', argument = false }, |
| 706 |
{ short = 'h', long = 'help', argument = false }, |
| 707 |
{ short = 'c', long = 'crossrefs', argument = false }, |
| 708 |
{ short = 'b', long = 'backend', argument = true }, |
| 709 |
{ short = 'j', long = 'json', argument = true } |
| 710 |
} |
| 711 |
|
| 712 |
local keys, err = parse(parameters, args) |
| 713 |
local check, backend = 'all', 'bibtex' |
| 714 |
|
| 715 |
local json = {} |
| 716 |
|
| 717 |
if #err ~= 0 then |
| 718 |
print() |
| 719 |
print(pad('-', 74)) |
| 720 |
print(wrap('I am sorry, but I do not recognize ' .. |
| 721 |
'the following ' .. plural(#err, 'option', |
| 722 |
'options') .. ':', 74)) |
| 723 |
for _, v in ipairs(err) do |
| 724 |
print('=> ' .. v) |
| 725 |
end |
| 726 |
|
| 727 |
print() |
| 728 |
print(wrap('Please make sure to use the correct ' .. |
| 729 |
'options when running this script. You ' .. |
| 730 |
'can also refer to the user documentation ' .. |
| 731 |
'for a list of valid options. The script ' .. |
| 732 |
'will end now.', 74)) |
| 733 |
return 1 |
| 734 |
end |
| 735 |
|
| 736 |
if count(keys) == 0 then |
| 737 |
print() |
| 738 |
print(pad('-', 74)) |
| 739 |
print(wrap('I am sorry, but you have not provided ' .. |
| 740 |
'any command line argument, including ' .. |
| 741 |
'files to check and options. Make ' .. |
| 742 |
'sure to invoke the script with the actual ' .. |
| 743 |
'arguments. Refer to the user documentation ' .. |
| 744 |
'if you are unsure of how this tool ' .. |
| 745 |
'works. The script will end now.', 74)) |
| 746 |
return 1 |
| 747 |
end |
| 748 |
|
| 749 |
if keys['version'] or keys['help'] then |
| 750 |
if keys['version'] then |
| 751 |
print() |
| 752 |
print(wrap('checkcites.lua, version 2.8 (dated December ' .. |
| 753 |
'14, 2024)', 74)) |
| 754 |
|
| 755 |
print(pad('-', 74)) |
| 756 |
print(wrap('You can find more details about this ' .. |
| 757 |
'script, as well as the user documentation, ' .. |
| 758 |
'in the official source code repository:', 74)) |
| 759 |
|
| 760 |
print() |
| 761 |
print('https://gitlab.com/islandoftex/checkcites') |
| 762 |
|
| 763 |
print() |
| 764 |
print(wrap('The checkcites.lua script is licensed ' .. |
| 765 |
'under the LaTeX Project Public License, ' .. |
| 766 |
'version 1.3.', 74)) |
| 767 |
else |
| 768 |
print() |
| 769 |
print(wrap('Usage: ' .. args[0] .. ' [ [ --all | --unused | ' .. |
| 770 |
'--undefined ] [ --backend <arg> ] <file> [ ' .. |
| 771 |
'<file 2> ... <file n> ] | --json <file> | ' .. |
| 772 |
'--help | --version ]', 74)) |
| 773 |
|
| 774 |
print() |
| 775 |
print('-a,--all list all unused and undefined references') |
| 776 |
print('-u,--unused list only unused references in your bibliography files') |
| 777 |
print('-U,--undefined list only undefined references in your TeX source file') |
| 778 |
print('-c,--crossrefs enable cross-reference checks (disabled by default)') |
| 779 |
print('-b,--backend <arg> set the backend-based file lookup policy') |
| 780 |
print('-j,--json <file> export the generated report as a JSON file') |
| 781 |
print('-h,--help print the help message') |
| 782 |
print('-v,--version print the script version') |
| 783 |
|
| 784 |
print() |
| 785 |
print(wrap('Unless specified, the script lists all unused and ' .. |
| 786 |
'undefined references by default. Also, the default ' .. |
| 787 |
'backend is set to "bibtex". Please refer to the user ' .. |
| 788 |
'documentation for more details.', 74)) |
| 789 |
end |
| 790 |
return 0 |
| 791 |
end |
| 792 |
|
| 793 |
if not keys['unpaired'] then |
| 794 |
print() |
| 795 |
print(pad('-', 74)) |
| 796 |
print(wrap('I am sorry, but you have not provided ' .. |
| 797 |
'files to process. The tool requires ' .. |
| 798 |
'least one file in order to properly ' .. |
| 799 |
'work. Make sure to invoke the script ' .. |
| 800 |
'with an actual file (or files). Refer ' .. |
| 801 |
'to the user documentation if you are ' .. |
| 802 |
'unsure of how this tool works. The ' .. |
| 803 |
'script will end now.', 74)) |
| 804 |
return 1 |
| 805 |
end |
| 806 |
|
| 807 |
if keys['backend'] then |
| 808 |
if not exists({ 'bibtex', 'biber' }, keys['backend'][1]) then |
| 809 |
print() |
| 810 |
print(pad('-', 74)) |
| 811 |
print(wrap('I am sorry, but you provided an ' .. |
| 812 |
'invalid backend. I know two: ' .. |
| 813 |
'"bibtex" (which is the default ' .. |
| 814 |
'one) and "biber". Please make ' .. |
| 815 |
'sure to select one of the two. ' .. |
| 816 |
'Also refer to the user documentation ' .. |
| 817 |
'for more information on how these ' .. |
| 818 |
'backends work. The script will end ' .. |
| 819 |
'now.', 74)) |
| 820 |
return 1 |
| 821 |
else |
| 822 |
backend = keys['backend'][1] |
| 823 |
end |
| 824 |
end |
| 825 |
|
| 826 |
if not keys['all'] then |
| 827 |
if keys['unused'] and keys['undefined'] then |
| 828 |
check = 'all' |
| 829 |
elseif keys['unused'] or keys['undefined'] then |
| 830 |
check = (keys['unused'] and 'unused') or |
| 831 |
(keys['undefined'] and 'undefined') |
| 832 |
end |
| 833 |
end |
| 834 |
|
| 835 |
json['backend'] = backend |
| 836 |
json['check'] = check |
| 837 |
|
| 838 |
local auxiliary = apply(keys['unpaired'], function(a) |
| 839 |
return sanitize(a, (backend == 'bibtex' |
| 840 |
and 'aux') or 'bcf') end) |
| 841 |
|
| 842 |
local invalid, _ = validate(auxiliary, kpse, false, 'aux') |
| 843 |
if #invalid ~= 0 then |
| 844 |
print() |
| 845 |
print(pad('-', 74)) |
| 846 |
print(wrap('I am sorry, but I was unable to ' .. |
| 847 |
'locate ' .. plural(#invalid, 'this file', |
| 848 |
'these files') .. ' (the extension ' .. |
| 849 |
'is automatically set based on the ' .. |
| 850 |
'"' .. backend .. '" backend):', 74)) |
| 851 |
for _, v in ipairs(invalid) do |
| 852 |
print('=> ' .. v) |
| 853 |
end |
| 854 |
|
| 855 |
print() |
| 856 |
print(wrap('Selected backend: ' .. backend, 74)) |
| 857 |
print(wrap('File lookup policy: add ".' .. |
| 858 |
((backend == 'bibtex' and 'aux') or 'bcf') .. |
| 859 |
'" to files if not provided.', 74)) |
| 860 |
|
| 861 |
print() |
| 862 |
print(wrap('Please make sure the ' .. plural(#invalid, |
| 863 |
'path is', 'paths are') .. ' ' .. |
| 864 |
'correct and the ' .. plural(#invalid, |
| 865 |
'file exists', 'files exist') .. '. ' .. |
| 866 |
'There is nothing I can do at the moment. ' .. |
| 867 |
'Refer to the user documentation for ' .. |
| 868 |
'details on the file lookup. If ' .. plural(#invalid, |
| 869 |
'this is not the file', 'these are not the ' .. |
| 870 |
'files') .. ' you were expecting, ' .. |
| 871 |
'double-check your source file or ' .. |
| 872 |
'change the backend option when running ' .. |
| 873 |
'this tool. The script will end now.', 74)) |
| 874 |
return 1 |
| 875 |
end |
| 876 |
|
| 877 |
local lines = flatten(apply(auxiliary, read)) |
| 878 |
local asterisk, citations, bibliography = backends[backend](lines, true) |
| 879 |
|
| 880 |
json['citations'] = citations |
| 881 |
json['bibliographies'] = bibliography |
| 882 |
json['asterisk'] = (asterisk and 'true') or 'false' |
| 883 |
|
| 884 |
print() |
| 885 |
print(wrap('Great, I found ' .. tostring(#citations) .. ' ' .. |
| 886 |
plural(#citations, 'citation', 'citations') .. ' in ' .. |
| 887 |
tostring(#auxiliary) .. ' ' .. plural(#auxiliary, 'file', |
| 888 |
'files') ..'. I also found ' .. tostring(#bibliography) .. |
| 889 |
' ' .. 'bibliography ' .. plural(#bibliography, 'file', |
| 890 |
'files') .. '. Let me check ' .. plural(#bibliography, |
| 891 |
'this file', 'these files') .. ' and extract the ' .. |
| 892 |
'references. Please wait a moment.', 74)) |
| 893 |
|
| 894 |
if asterisk then |
| 895 |
print() |
| 896 |
print(wrap('Also, it is worth noticing that I found a mention to ' .. |
| 897 |
'a special "*" when retrieving citations. That means ' .. |
| 898 |
'your TeX document contains "\\nocite{*}" somewhere in ' .. |
| 899 |
'the source code. I will continue with the check ' .. |
| 900 |
'nonetheless.', 74)) |
| 901 |
end |
| 902 |
|
| 903 |
bibliography = apply(bibliography, function(a) |
| 904 |
return sanitize(a, 'bib') end) |
| 905 |
|
| 906 |
invalid, bibliography = validate(bibliography, kpse, true, 'bib') |
| 907 |
if #invalid ~= 0 then |
| 908 |
print() |
| 909 |
print(pad('-', 74)) |
| 910 |
print(wrap('I am sorry, but I was unable to locate ' .. |
| 911 |
plural(#invalid, 'this file', 'these files') .. ' ' .. |
| 912 |
'(the extension is automatically set to ' .. |
| 913 |
'".bib", if not provided):', 74)) |
| 914 |
for _, v in ipairs(invalid) do |
| 915 |
print('=> ' .. v) |
| 916 |
end |
| 917 |
|
| 918 |
print() |
| 919 |
print(wrap('Please make sure the ' .. plural(#invalid, |
| 920 |
'path is', 'paths are') .. ' ' .. |
| 921 |
'correct and the ' .. plural(#invalid, |
| 922 |
'file exists', 'files exist') .. '. ' .. |
| 923 |
'There is nothing I can do at the moment. ' .. |
| 924 |
'Refer to to the user documentation ' .. |
| 925 |
'for details on bibliography lookup. If ' .. |
| 926 |
plural(#invalid, 'this is not the file', |
| 927 |
'these are not the files') .. ' you were ' .. |
| 928 |
'expecting (wrong bibliography), double-check ' .. |
| 929 |
'your source file. The script will end ' .. |
| 930 |
'now.', 74)) |
| 931 |
return 1 |
| 932 |
end |
| 933 |
|
| 934 |
local references = flatten(apply(bibliography, function(a) |
| 935 |
return extract(read(a)) end)) |
| 936 |
|
| 937 |
local crossrefs = (keys['crossrefs'] and organize(apply(bibliography, |
| 938 |
function(a) return crossref(read(a)) end))) or {} |
| 939 |
|
| 940 |
json['references'] = references |
| 941 |
|
| 942 |
if (keys['crossrefs']) then |
| 943 |
json['crossrefs'] = crossrefs |
| 944 |
end |
| 945 |
|
| 946 |
print() |
| 947 |
print(wrap('Fantastic, I found ' .. tostring(#references) .. |
| 948 |
' ' .. plural(#references, 'reference', |
| 949 |
'references') .. ' in ' .. tostring(#bibliography) .. |
| 950 |
' bibliography ' .. plural(#bibliography, 'file', |
| 951 |
'files') .. '. Please wait a moment while the ' .. |
| 952 |
plural(((check == 'all' and 2) or 1), 'report is', |
| 953 |
'reports are') .. ' generated.', 74)) |
| 954 |
|
| 955 |
local status, result = operations[check](citations, references, crossrefs) |
| 956 |
|
| 957 |
json['unused'] = result['unused'] or (check == 'unused') and result or {} |
| 958 |
json['undefined'] = result['undefined'] or (check == 'undefined') and result or {} |
| 959 |
|
| 960 |
if keys['json'] then |
| 961 |
toJson(keys['json'][1], json) |
| 962 |
end |
| 963 |
|
| 964 |
return status |
| 965 |
end |
| 966 |
|
| 967 |
|
| 968 |
os.exit(checkcites(arg)) |
| 969 |
|
| 970 |
|