| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
VERSION = "3.1" |
| 23 |
BIBLEDEFAULT = "bible.can" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
function fileopen(fnam) |
| 29 |
local handle |
| 30 |
if fnam ~= "-" then |
| 31 |
local msg,errno |
| 32 |
handle,msg,errno = io.open(fnam, "r") |
| 33 |
if not handle then |
| 34 |
io.stderr:write("songidx: Unable to open ",fnam," for reading.\n", |
| 35 |
"Error ",errno,": ",msg,"\n") |
| 36 |
return nil |
| 37 |
end |
| 38 |
else |
| 39 |
handle = io.stdin |
| 40 |
fnam = "stdin" |
| 41 |
end |
| 42 |
return { f=handle, filename=fnam, lineno=0 } |
| 43 |
end |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
function filereadln(fs) |
| 48 |
fs["lineno"] = fs["lineno"] + 1 |
| 49 |
local s = fs["f"]:read() |
| 50 |
if not s then return nil end |
| 51 |
return s |
| 52 |
end |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
function closeout(handle) |
| 57 |
if handle == io.stdout then handle:flush() |
| 58 |
else handle:close() end |
| 59 |
end |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
function errorout(handle) |
| 64 |
closeout(handle) |
| 65 |
io.stderr:write("songidx: Error writing to output file. Aborting.\n") |
| 66 |
return 2 |
| 67 |
end |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
function fileclose(fs) |
| 72 |
if (fs["f"] ~= io.stdin) then fs["f"]:close() end |
| 73 |
fs["f"] = nil |
| 74 |
fs["filename"] = nil |
| 75 |
fs["lineno"] = 0 |
| 76 |
end |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
function numprefix(s) |
| 84 |
local _, len = unicode.utf8.find(s,"^%S*") |
| 85 |
for i=len, 1, -1 do |
| 86 |
local n = tonumber(s:sub(1,i)) |
| 87 |
if n then return n, s:sub(i+1) end |
| 88 |
end |
| 89 |
return false, s |
| 90 |
end |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
function cleantitle(s) |
| 97 |
if not s["clean"] then |
| 98 |
local t = s["title"]:gsub("\\[^%a%s]","") |
| 99 |
:gsub("\\(%s)","%1") |
| 100 |
:gsub("\\%a+%s*","") |
| 101 |
:gsub("{%s*","") |
| 102 |
:gsub("}","") |
| 103 |
s["clean"] = unicode.utf8.upper(t) |
| 104 |
end |
| 105 |
return s["clean"] |
| 106 |
end |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
function songcmp(s1,s2) |
| 113 |
local t1 = cleantitle(s1) |
| 114 |
local t2 = cleantitle(s2) |
| 115 |
|
| 116 |
while true do |
| 117 |
|
| 118 |
t1, t2 = unicode.utf8.match(t1,"%w.*"), unicode.utf8.match(t2,"%w.*") |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
if not t1 then |
| 124 |
if not t2 then break end |
| 125 |
return true |
| 126 |
elseif not t2 then return false end |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
local n1, n2 |
| 131 |
n1, t1 = numprefix(t1) |
| 132 |
n2, t2 = numprefix(t2) |
| 133 |
if n1 then |
| 134 |
if not n2 or n1 < n2 then return true end |
| 135 |
if n1 > n2 then return false end |
| 136 |
elseif n2 then return false |
| 137 |
else |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
local w1, w2 = unicode.utf8.match(t1,"^[%a'`]*"), |
| 143 |
unicode.utf8.match(t2,"^[%a'`]*") |
| 144 |
local diff = w1 < w2 |
| 145 |
if diff or not (w1 <= w2) then return diff end |
| 146 |
t1, t2 = t1:sub(#w1+1), t2:sub(#w2+1) |
| 147 |
end |
| 148 |
end |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
local alt1, alt2 = s1["title"]:sub(1,1), s2["title"]:sub(1,1) |
| 153 |
if alt1 == "*" and alt2 ~= "*" then return false end |
| 154 |
if alt1 ~= "*" and alt2 == "*" then return true end |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
return s1["idx"] < s2["idx"] |
| 159 |
end |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
function setstartchars(songs) |
| 174 |
local start = 1 |
| 175 |
local best |
| 176 |
|
| 177 |
for i=1, #songs do |
| 178 |
local c = unicode.utf8.match(cleantitle(songs[i]),"%w") |
| 179 |
if c then |
| 180 |
c = unicode.utf8.upper(c) |
| 181 |
if not best then |
| 182 |
songs[start]["newblk"], start, best = "\\#", i, c |
| 183 |
elseif best < c then |
| 184 |
songs[start]["newblk"], start, best = best, i, c |
| 185 |
elseif #c < #best then best = c |
| 186 |
elseif #c == #best then |
| 187 |
for j=1,#c do |
| 188 |
if c:byte(j) < best:byte(j) then best = c; break |
| 189 |
elseif c:byte(j) > best:byte(j) then break end |
| 190 |
end |
| 191 |
end |
| 192 |
elseif best then |
| 193 |
songs[start]["newblk"], start, best = best, i, nil |
| 194 |
end |
| 195 |
end |
| 196 |
|
| 197 |
if start <= #songs then |
| 198 |
if best then |
| 199 |
songs[start]["newblk"] = best |
| 200 |
else |
| 201 |
songs[start]["newblk"] = "\\#" |
| 202 |
end |
| 203 |
end |
| 204 |
end |
| 205 |
|
| 206 |
wt_prefix = { A=true, THE=true } |
| 207 |
wt_and = { AND=true } |
| 208 |
wt_by = { BY=true } |
| 209 |
wt_unknown = { UNKNOWN=true } |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
function rotate(s) |
| 219 |
local t = unicode.utf8.upper(s) |
| 220 |
local n = 0 |
| 221 |
if s:sub(1,1) == "*" then n = 1 end |
| 222 |
for pre in pairs(wt_prefix) do |
| 223 |
if t:sub(1+n,n+#pre) == pre and |
| 224 |
unicode.utf8.find(t, "^%s+%S", n+#pre+1) then |
| 225 |
local len = unicode.utf8.len(pre) |
| 226 |
local x, y, z = unicode.utf8.match(unicode.utf8.sub(s,n+len+1),"^%s+(%W*)(%w?)(.*)$") |
| 227 |
return s:sub(1,n) .. x .. unicode.utf8.upper(y) .. z:gsub("\\%s","%0\1"):match("^(.-)%s*$"):gsub("\1","") .. ",~" .. unicode.utf8.sub(s,1+n,n+len) |
| 228 |
end |
| 229 |
end |
| 230 |
return s |
| 231 |
end |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
function matchany(s,init,wt) |
| 238 |
local t = s:sub(init) |
| 239 |
local u = unicode.utf8.upper(t) |
| 240 |
for w,_ in pairs(wt) do |
| 241 |
if u:sub(1,#w) == w and |
| 242 |
(#w == #u or unicode.utf8.find(u, "^%s", #w+1)) then |
| 243 |
return init + #(unicode.utf8.sub(t,1,unicode.utf8.len(w))) |
| 244 |
end |
| 245 |
end |
| 246 |
return init |
| 247 |
end |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
function issuffix(s,i) |
| 254 |
return unicode.utf8.find(s, "^Jr$", i) or |
| 255 |
unicode.utf8.find(s, "^Jr[%s,;%.]", i) or |
| 256 |
unicode.utf8.find(s, "^[IVX]+$", i) or |
| 257 |
unicode.utf8.find(s, "^[IVX]+[%s,;%.]", i) |
| 258 |
end |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
function grabauthor(authline,i) |
| 298 |
i = unicode.utf8.find(authline, "[^%s,;]", i) |
| 299 |
if not i then return nil end |
| 300 |
i = unicode.utf8.find(authline, "%S", matchany(authline,i,wt_and)) |
| 301 |
if not i then return nil end |
| 302 |
i = unicode.utf8.find(authline, "%S", matchany(authline,i,wt_by)) |
| 303 |
if not i then return nil end |
| 304 |
local skip = (matchany(authline,i,wt_unknown) > i) |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
local first,last,suffix = i,nil,nil |
| 311 |
while i <= #authline do |
| 312 |
while true do |
| 313 |
local j = select(2,authline:find("^\\%A", i)) or |
| 314 |
select(2,authline:find("^\\%a+%s*", i)) or |
| 315 |
select(2,authline:find("^%b{}", i)) |
| 316 |
if not j then break end |
| 317 |
i = j + 1 |
| 318 |
end |
| 319 |
if i > #authline then break |
| 320 |
elseif authline:sub(i,i) == "," then |
| 321 |
local j = unicode.utf8.find(authline, "%S", i+1) |
| 322 |
if j and issuffix(authline,j) then i = i + 1 |
| 323 |
else break end |
| 324 |
elseif authline:sub(i,i) == ";" then break |
| 325 |
elseif unicode.utf8.find(authline, "^%s", i) then |
| 326 |
i = unicode.utf8.find(authline, "%S", i) |
| 327 |
if not i then i = #authline + 1; break end |
| 328 |
if matchany(authline, i, wt_and) > i then break end |
| 329 |
skip = skip or (matchany(authline, i, wt_unknown) > i) |
| 330 |
local j = matchany(authline, i, wt_by) |
| 331 |
if j > i then |
| 332 |
j = unicode.utf8.find(authline, "%S", j) |
| 333 |
if not j then last = i; break end |
| 334 |
i,first,last,suffix = j,j,nil,nil |
| 335 |
elseif issuffix(authline,i) then |
| 336 |
suffix = i |
| 337 |
elseif unicode.utf8.find(authline:sub(i):gsub("\\%A",""):gsub("\\%a+%s*",""),"^[%s{}'`\"]*%u") then |
| 338 |
last,suffix = i,nil |
| 339 |
end |
| 340 |
else |
| 341 |
i = select(2, unicode.utf8.find(authline, ".", i)) + 1 |
| 342 |
end |
| 343 |
end |
| 344 |
|
| 345 |
|
| 346 |
if skip then return grabauthor(authline,i) end |
| 347 |
|
| 348 |
|
| 349 |
local sirname, fullname |
| 350 |
if last then |
| 351 |
sirname = unicode.utf8.gsub(authline:sub(last,(suffix or i)-1), |
| 352 |
"([^%s,;\\])[%s,;]+$", "%1") |
| 353 |
end |
| 354 |
if not sirname or unicode.utf8.find(sirname, "%a%.$") then |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
local _,_,r = grabauthor(authline,i) |
| 361 |
if r or not sirname then |
| 362 |
fullname = unicode.utf8.gsub(authline:sub(first,i-1), |
| 363 |
"([^%s,;\\])[%s,;]+$", "%1") |
| 364 |
if r then return (r .. ", " .. fullname), i, r |
| 365 |
else return fullname, i, nil end |
| 366 |
end |
| 367 |
end |
| 368 |
|
| 369 |
|
| 370 |
fullname = sirname .. ", " .. |
| 371 |
unicode.utf8.gsub(authline:sub(first,(last or suffix or i)-1), |
| 372 |
"([^%s,;\\])[%s,;]+$", "%1") |
| 373 |
|
| 374 |
|
| 375 |
if suffix then |
| 376 |
fullname = fullname .. " " .. |
| 377 |
unicode.utf8.gsub(authline:sub(suffix,i-1), "([^%s,;\\])[%s,;]+$", "%1") |
| 378 |
end |
| 379 |
|
| 380 |
return fullname, i, sirname |
| 381 |
end |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
function genindex(fs,outname,authorindex) |
| 389 |
local songs = {} |
| 390 |
local seen = {} |
| 391 |
local wt = { wt_sep, wt_after, wt_prefix, wt_ignore } |
| 392 |
local typ = authorindex and "author" or "title" |
| 393 |
|
| 394 |
io.stderr:write("songidx: Parsing ",typ," index data file ",fs["filename"],"...\n") |
| 395 |
|
| 396 |
while true do |
| 397 |
local buf = filereadln(fs) |
| 398 |
if not buf then break end |
| 399 |
if buf:sub(1,1) == "%" then |
| 400 |
local j = buf:match("^()%%sep ") or |
| 401 |
buf:match("^%%()after ") or |
| 402 |
buf:match("^%%p()refix ") or |
| 403 |
buf:match("^%%ig()nore ") |
| 404 |
if j then |
| 405 |
if not seen[j] then |
| 406 |
for w in pairs(wt[j]) do wt[j][w] = nil end |
| 407 |
seen[j] = true |
| 408 |
end |
| 409 |
wt[j][unicode.utf8.upper(buf:sub(buf:find(" ")+1))] = true |
| 410 |
end |
| 411 |
else |
| 412 |
local snum = filereadln(fs) |
| 413 |
if not snum then |
| 414 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": incomplete song entry (orphan ",typ,")\n") |
| 415 |
return 2 |
| 416 |
end |
| 417 |
local link = filereadln(fs) |
| 418 |
if not link then |
| 419 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": incomplete song entry (missing hyperlink)\n") |
| 420 |
return 2 |
| 421 |
end |
| 422 |
if authorindex then |
| 423 |
local i,a = 1 |
| 424 |
buf = buf:gsub("[\1\2\3]",""):gsub("\\\\","\\\1") |
| 425 |
:gsub("\\{","\\\2"):gsub("\\}","\\\3") |
| 426 |
while true do |
| 427 |
a,i = grabauthor(buf, i) |
| 428 |
if not a then break end |
| 429 |
a = a:gsub("\1","\\"):gsub("\2","{"):gsub("\3","}") |
| 430 |
table.insert(songs, {title=a, num=snum, linkname=link, idx=#songs}) |
| 431 |
end |
| 432 |
else |
| 433 |
buf = rotate(unicode.utf8.gsub(unicode.utf8.gsub(buf,"([^%s\\])%s+$","%1"),"^(%*?)%s+","%1")) |
| 434 |
table.insert(songs, {title=buf, num=snum, linkname=link, idx=#songs}) |
| 435 |
end |
| 436 |
end |
| 437 |
end |
| 438 |
fileclose(fs) |
| 439 |
|
| 440 |
|
| 441 |
table.sort(songs, songcmp) |
| 442 |
|
| 443 |
setstartchars(songs) |
| 444 |
|
| 445 |
|
| 446 |
io.stderr:write("songidx: Generating ",typ," index TeX file ",outname,"...\n") |
| 447 |
local f,msg,errno |
| 448 |
if outname == "-" then |
| 449 |
f, outname = io.stdout, "stdout" |
| 450 |
else |
| 451 |
f,msg,errno = io.open(outname, "w") |
| 452 |
if not f then |
| 453 |
io.stderr:write("songidx: Unable to open ",outname," for writing.\n", |
| 454 |
"Error ",errno,": ",msg,"\n") |
| 455 |
return 2 |
| 456 |
end |
| 457 |
end |
| 458 |
for i=1, #songs do |
| 459 |
if i>1 and songs[i]["title"] == songs[i-1]["title"] then |
| 460 |
if not f:write("\\\\\\songlink{",songs[i]["linkname"],"}{",songs[i]["num"],"}") then return errorout(f) end |
| 461 |
else |
| 462 |
if songs[i]["newblk"] then |
| 463 |
if i>1 then |
| 464 |
if not f:write("}\n\\end{idxblock}\n") then return errorout(f) end |
| 465 |
end |
| 466 |
if not f:write("\\begin{idxblock}{",songs[i]["newblk"]) then return errorout(f) end |
| 467 |
end |
| 468 |
if songs[i]["title"]:find("^%*") then |
| 469 |
if not f:write("}\n\\idxaltentry{",songs[i]["title"]:sub(2)) then return errorout(f) end |
| 470 |
else |
| 471 |
if not f:write("}\n\\idxentry{",songs[i]["title"]) then return errorout(f) end |
| 472 |
end |
| 473 |
if not f:write("}{\\songlink{",songs[i]["linkname"],"}{",songs[i]["num"],"}") then return errorout(f) end |
| 474 |
end |
| 475 |
end |
| 476 |
if #songs > 0 then |
| 477 |
if not f:write("}\n\\end{idxblock}\n") then return errorout(f) end |
| 478 |
end |
| 479 |
|
| 480 |
return 0 |
| 481 |
end |
| 482 |
|
| 483 |
bible = {} |
| 484 |
chapX = 0 |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
function readbible(filename) |
| 490 |
local fs = fileopen(filename) |
| 491 |
if not fs then return nil end |
| 492 |
bible = {} |
| 493 |
|
| 494 |
while true do |
| 495 |
local buf = filereadln(fs) |
| 496 |
if not buf then break end |
| 497 |
if buf:sub(1,1) ~= "#" and buf:find("%S") then |
| 498 |
local t, vbuf = { name = buf:match("^[^|]*"), |
| 499 |
aliases = "|" .. unicode.utf8.upper(buf) .. "|" } |
| 500 |
repeat |
| 501 |
vbuf = filereadln(fs) |
| 502 |
if not vbuf then |
| 503 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": incomplete bible book entry (book title with no verses)\n") |
| 504 |
fileclose(fs) |
| 505 |
return nil |
| 506 |
end |
| 507 |
until vbuf:sub(1,1) ~= "#" and buf:find("%S") |
| 508 |
if vbuf:find("[^%d%s]") then |
| 509 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": verse count includes a non-digit\n") |
| 510 |
fileclose(fs) |
| 511 |
return nil |
| 512 |
end |
| 513 |
for n in vbuf:gmatch("%d+") do |
| 514 |
local i = tonumber(n) |
| 515 |
if not i then |
| 516 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": invalid number ",n,"\n") |
| 517 |
fileclose(fs) |
| 518 |
return nil |
| 519 |
end |
| 520 |
i = math.floor(i) |
| 521 |
if chapX < i+1 then chapX = i+1 end |
| 522 |
table.insert(t, i) |
| 523 |
end |
| 524 |
table.insert(bible, t) |
| 525 |
end |
| 526 |
end |
| 527 |
|
| 528 |
fileclose(fs) |
| 529 |
return true |
| 530 |
end |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
function parseref(s,i,book,ch) |
| 548 |
local v = -1 |
| 549 |
i = unicode.utf8.find(s,"%S",i) |
| 550 |
if not i then return nil end |
| 551 |
local j = unicode.utf8.find(s,"[%d:]*%s*[,;%-]",i) or |
| 552 |
unicode.utf8.find(s,"[%d:]*%s*$",i) |
| 553 |
local bookname = "|" .. unicode.utf8.upper(unicode.utf8.match(s:sub(i,j-1), "(.-)%s*$")) .. "|" |
| 554 |
i = j |
| 555 |
if bookname ~= "||" then |
| 556 |
book, ch = #bible+1, -1 |
| 557 |
for b,t in pairs(bible) do |
| 558 |
if t["aliases"]:find(bookname, 1, true) then book = b; break end |
| 559 |
end |
| 560 |
end |
| 561 |
j = unicode.utf8.find(s,"%D",i) or (#s+1) |
| 562 |
if s:sub(j,j) == ":" then |
| 563 |
ch, i = (tonumber(s:sub(i,j-1)) or -1), j+1 |
| 564 |
j = unicode.utf8.find(s,"%D",i) or (#s+1) |
| 565 |
end |
| 566 |
if ch<=0 and book>0 and #bible[book] == 1 then |
| 567 |
|
| 568 |
ch = 1 |
| 569 |
end |
| 570 |
if ch <= 0 then |
| 571 |
ch = tonumber(s:sub(i,j-1)) or -1 |
| 572 |
else |
| 573 |
v = tonumber(s:sub(i,j-1)) or -1 |
| 574 |
end |
| 575 |
i = unicode.utf8.find(s,"%S",j) |
| 576 |
if not i then i = #s+1 |
| 577 |
elseif not s:find("^[,;%-]",i) then return nil end |
| 578 |
return i, book, ch, v |
| 579 |
end |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
function vlt(ch1,v1,ch2,v2) |
| 585 |
return ch1 < ch2 or (ch1 == ch2 and v1 < v2) |
| 586 |
end |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
function vinc(book,ch,v) |
| 593 |
if v < bible[book][ch] then return ch, v+1 end |
| 594 |
return ch+1, 1 |
| 595 |
end |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
function vdec(book,ch,v) |
| 602 |
if v > 1 then return ch, v-1 end |
| 603 |
return ch-1, bible[book][ch-1] |
| 604 |
end |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
function unpack_cv(cv) |
| 610 |
local v = cv % chapX |
| 611 |
return (cv-v)/chapX, v |
| 612 |
end |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
function eqdom(t1,t2) |
| 617 |
for k,_ in pairs(t1) do if not t2[k] then return false end end |
| 618 |
for k,_ in pairs(t2) do if not t1[k] then return false end end |
| 619 |
return true |
| 620 |
end |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
function insertref(is_add,set,ch,v,n,l,k) |
| 636 |
if not set then set = {} end |
| 637 |
local cv = ch*chapX+v |
| 638 |
if not set[cv] then set[cv] = { adds={}, drops={} } end |
| 639 |
set[cv][is_add and "adds" or "drops"][k] = { num=n, link=l } |
| 640 |
return set |
| 641 |
end |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
function print_vrange(f,b,ch1,v1,ch2,v2,lch) |
| 652 |
local r = f:write(lch == 0 and "\\idxentry{" or ",") |
| 653 |
|
| 654 |
if v1 <= 0 then |
| 655 |
if lch ~= 0 then r = r and f:write("\\thinspace ") end |
| 656 |
r = r and f:write(ch1) |
| 657 |
elseif 0 <= b and b < #bible and #bible[b] == 1 then |
| 658 |
|
| 659 |
if lch ~= 0 then r = r and f:write("\\thinspace ") end |
| 660 |
r = r and f:write(v1) |
| 661 |
elseif lch <= 0 or lch ~= ch1 or ch1 ~= ch2 then |
| 662 |
if lch ~= 0 then r = r and f:write(" ") end |
| 663 |
r = r and f:write(ch1,":",v1) |
| 664 |
else |
| 665 |
if lch ~= 0 then r = r and f:write("\\thinspace ") end |
| 666 |
r = r and f:write(v1) |
| 667 |
end |
| 668 |
|
| 669 |
if vlt(ch1,v1,ch2,v2) then |
| 670 |
if v2 <= 0 then |
| 671 |
r = r and f:write("--",ch2) |
| 672 |
elseif ch1 ~= ch2 then |
| 673 |
r = r and f:write("--",ch2,":",v2) |
| 674 |
else |
| 675 |
r = r and f:write("--",v2) |
| 676 |
end |
| 677 |
end |
| 678 |
|
| 679 |
return r |
| 680 |
end |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
function print_reflist(f,t) |
| 686 |
local r = true |
| 687 |
local s = {} |
| 688 |
for k,_ in pairs(t) do table.insert(s,k) end |
| 689 |
table.sort(s) |
| 690 |
|
| 691 |
local first = true |
| 692 |
for _,k in ipairs(s) do |
| 693 |
if first then first = false else r = r and f:write("\\\\") end |
| 694 |
r = r and f:write("\\songlink{",t[k]["link"],"}{",t[k]["num"],"}") |
| 695 |
end |
| 696 |
return r |
| 697 |
end |
| 698 |
|
| 699 |
function debug_print_reflist(r) |
| 700 |
local first = true |
| 701 |
io.stderr:write("{") |
| 702 |
for k,_ in pairs(r) do |
| 703 |
if first then first=false else io.stderr:write(",") end |
| 704 |
io.stderr:write(k) |
| 705 |
end |
| 706 |
io.stderr:write("}") |
| 707 |
end |
| 708 |
|
| 709 |
function debug_print_changeset(x) |
| 710 |
if not x then io.stderr:write("nil") else |
| 711 |
for cv,r in pairs(x) do |
| 712 |
local ch,v = unpack_cv(cv) |
| 713 |
io.stderr:write("{",ch,":",v," --> adds=") |
| 714 |
debug_print_reflist(r["adds"]) |
| 715 |
io.stderr:write(", drops=") |
| 716 |
debug_print_reflist(r["drops"]) |
| 717 |
io.stderr:write("}") |
| 718 |
end |
| 719 |
end |
| 720 |
end |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
function genscriptureindex(fs,outname,biblename) |
| 728 |
local hadwarnings = 0 |
| 729 |
local idx = {} |
| 730 |
|
| 731 |
io.stderr:write("songidx: Parsing scripture index data file ",fs["filename"],"...\n") |
| 732 |
|
| 733 |
|
| 734 |
if not readbible(biblename) then return 2 end |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
local key = 0 |
| 740 |
while true do |
| 741 |
local ref = filereadln(fs) |
| 742 |
if not ref then break end |
| 743 |
local n = filereadln(fs) |
| 744 |
if not n then |
| 745 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": incomplete song entry (orphan reference line)\n") |
| 746 |
fileclose(fs) |
| 747 |
return 2 |
| 748 |
end |
| 749 |
local l = filereadln(fs) |
| 750 |
if not l then |
| 751 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": incomplete song entry (missing hyperlink)\n") |
| 752 |
fileclose(fs) |
| 753 |
return 2 |
| 754 |
end |
| 755 |
key = key + 1 |
| 756 |
|
| 757 |
local i = 1 |
| 758 |
local book, ch1, v1, ch2, v2 = -1, -1, -1, -1, -1 |
| 759 |
while i <= #ref do |
| 760 |
i,book,ch1,v1 = parseref(ref,i,book,ch1) |
| 761 |
ch2,v2 = ch1,v1 |
| 762 |
if not i then |
| 763 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": WARNING: Malformed scripture reference for song ",n,". Ignoring it.\n") |
| 764 |
hadwarnings = 1 |
| 765 |
break |
| 766 |
end |
| 767 |
if book < 1 then |
| 768 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": WARNING: Scripture reference for song ",n," doesn't include a book name. Ignoring it.\n") |
| 769 |
hadwarnings = 1 |
| 770 |
break |
| 771 |
end |
| 772 |
if book > #bible then |
| 773 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": WARNING: Scripture reference for song ",n," references unknown book. Ignoring it.\n") |
| 774 |
hadwarnings = 1 |
| 775 |
break |
| 776 |
end |
| 777 |
if ch1 < 1 then ch1 = 1 end |
| 778 |
if ch1 > #bible[book] then |
| 779 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": WARNING: Scripture reference for song ",n," refers to ",bible[book]["name"]," ",ch1,", which doesn't exist. Ignoring it.\n") |
| 780 |
hadwarnings = 1 |
| 781 |
break |
| 782 |
end |
| 783 |
if v1 < 1 then v1 = 1 end |
| 784 |
if v1 > bible[book][ch1] then |
| 785 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": WARNING: Scripture reference for song ",n," refers to ",bible[book]["name"]," ",ch1,":",v1,", which doesn't exist. Ignoring it.\n") |
| 786 |
hadwarnings = 1 |
| 787 |
break |
| 788 |
end |
| 789 |
|
| 790 |
if ref:sub(i,i) == "-" then |
| 791 |
|
| 792 |
|
| 793 |
i = unicode.utf8.find(ref, "[^%s%-]", i) |
| 794 |
if not i then |
| 795 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": WARNING: Scripture reference for song ",n," has range with no limit. Ignoring it.\n") |
| 796 |
hadwarnings = 1 |
| 797 |
break |
| 798 |
end |
| 799 |
local book2 |
| 800 |
i,book2,ch2,v2 = parseref(ref,i,book,ch1) |
| 801 |
if not i then |
| 802 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": WARNING: Malformed scripture reference for song ",n,". Ignoring it.\n") |
| 803 |
hadwarnings = 1 |
| 804 |
break |
| 805 |
end |
| 806 |
if book2 ~= book then |
| 807 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": WARNING: Scripture reference for song ",n," appears to span books! Ignoring it.\n") |
| 808 |
hadwarnings = 1 |
| 809 |
break |
| 810 |
end |
| 811 |
end |
| 812 |
if ch2 < 1 then ch2 = #bible[book] end |
| 813 |
if ch2 > #bible[book] then |
| 814 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": WARNING: Scripture reference for song ",n," refers implicitly to ",bible[book]["name"]," ",ch2,", which doesn't exist. Ignoring it.\n") |
| 815 |
hadwarnings = 1 |
| 816 |
break |
| 817 |
end |
| 818 |
if v2 < 1 then v2 = bible[book][ch2] |
| 819 |
elseif v2 > bible[book][ch2] then |
| 820 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": WARNING: Scripture reference for song ",n," refers implicitly to chapter ",ch2," of ",bible[book]["name"],", which doesn't exist. Ignoring it.\n") |
| 821 |
hadwarnings = 1 |
| 822 |
break |
| 823 |
end |
| 824 |
if vlt(ch2,v2,ch1,v1) then |
| 825 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": WARNING: Scripture reference for song ",n," contains backwards range ",bible[book]["name"]," ",ch1,":",v1,"-",ch2,":",v2,". Ignoring it.\n") |
| 826 |
hadwarnings = 1 |
| 827 |
break |
| 828 |
end |
| 829 |
if i < #ref then i = i + 1 end |
| 830 |
|
| 831 |
idx[book] = insertref(true, idx[book], ch1, v1, n, l, key) |
| 832 |
idx[book] = insertref(false, idx[book], ch2, v2, n, l, key) |
| 833 |
end |
| 834 |
end |
| 835 |
fileclose(fs) |
| 836 |
|
| 837 |
|
| 838 |
io.stderr:write("songidx: Generating scripture index TeX file ",outname,"...\n") |
| 839 |
local f,msg,errno |
| 840 |
if outname == "-" then |
| 841 |
f, outname = io.stdout, "stdout" |
| 842 |
else |
| 843 |
f,msg,errno = io.open(outname, "w") |
| 844 |
if not f then |
| 845 |
io.stderr:write("songidx: Unable to open ",outname," for writing.\n", |
| 846 |
"Error ",errno,": ",msg,"\n") |
| 847 |
return 2 |
| 848 |
end |
| 849 |
end |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
for b=1,#bible do |
| 856 |
local x = idx[b] |
| 857 |
if x then |
| 858 |
|
| 859 |
|
| 860 |
local s, t = {}, {} |
| 861 |
for cv,_ in pairs(x) do table.insert(s,cv) end |
| 862 |
table.sort(s) |
| 863 |
local lch = 0 |
| 864 |
local cv1 = s[1] |
| 865 |
local ch1,v1 = unpack_cv(cv1) |
| 866 |
if not f:write("\\begin{idxblock}{",bible[b]["name"],"}\n") then return errorout(f) end |
| 867 |
for i,cv in ipairs(s) do |
| 868 |
local this = x[cv] |
| 869 |
local ch,v = unpack_cv(cv) |
| 870 |
local ncv,nxt,nch,nv = s[i+1] |
| 871 |
if ncv then |
| 872 |
nxt,nch,nv = x[ncv], unpack_cv(ncv) |
| 873 |
end |
| 874 |
for k,r in pairs(this["adds"]) do t[k] = r end |
| 875 |
local skip = false |
| 876 |
if ncv and eqdom(this["drops"], nxt["adds"]) then |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
local ch2, v2 = vinc(b,ch,v) |
| 881 |
if not vlt(ch2,v2, nch,nv) then |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
skip = true |
| 886 |
elseif eqdom(t, this["drops"]) then |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
if not print_vrange(f,b,ch1,v1,ch,v,lch) then return errorout(f) end |
| 891 |
lch = (ch1 == ch) and ch or -1 |
| 892 |
ch1, v1 = nch, nv |
| 893 |
skip = true |
| 894 |
end |
| 895 |
end |
| 896 |
if not skip then |
| 897 |
if next(this["drops"]) then |
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
if not (print_vrange(f,b,ch1,v1,ch,v,lch) and |
| 904 |
f:write("}{") and |
| 905 |
print_reflist(f,t) and |
| 906 |
f:write("}\n")) then return errorout(f) end |
| 907 |
for k,_ in pairs(this["drops"]) do t[k] = nil end |
| 908 |
lch = 0 |
| 909 |
if not next(t) and ncv then |
| 910 |
ch1, v1 = nch, nv |
| 911 |
else |
| 912 |
ch1, v1 = vinc(b,ch,v) |
| 913 |
end |
| 914 |
end |
| 915 |
if next(t) and ncv and next(nxt["adds"]) and vlt(ch1,v1,nch,nv) then |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
local ch2, v2 = vdec(b,nch,nv) |
| 920 |
if not (print_vrange(f,b,ch1,v1,ch2,v2,lch) and |
| 921 |
f:write("}{") and |
| 922 |
print_reflist(f,t) and |
| 923 |
f:write("}\n")) then return errorout(f) end |
| 924 |
lch, ch1, v1 = 0, nch, nv |
| 925 |
end |
| 926 |
end |
| 927 |
end |
| 928 |
if not f:write("\\end{idxblock}\n") then return errorout(f) end |
| 929 |
end |
| 930 |
end |
| 931 |
|
| 932 |
closeout(f) |
| 933 |
return hadwarnings |
| 934 |
end |
| 935 |
|
| 936 |
|
| 937 |
function main() |
| 938 |
local fs, biblename, inname, outname, locale |
| 939 |
|
| 940 |
local i = 1 |
| 941 |
while arg[i] do |
| 942 |
if arg[i] == "-v" or arg[i] == "--version" then |
| 943 |
io.write("songidx ", VERSION, "\n", |
| 944 |
"Copyright (C) 2018 Kevin W. Hamlen\n", |
| 945 |
"License GPLv2: GNU GPL version 2 or later", |
| 946 |
" <http://gnu.org/licenses/gpl.html>\n", |
| 947 |
"This is free software: you are free to change and redistribute it.\n", |
| 948 |
"There is NO WARRANTY, to the extent permitted by law.\n") |
| 949 |
return 0 |
| 950 |
elseif arg[i] == "-h" or arg[i] == "--help" then |
| 951 |
io.write("Syntax: ",arg[-1]," ",arg[0]," [options] input.sxd [output.sbx]\n", |
| 952 |
"Available options:\n", |
| 953 |
" -b FILE Set the bible format when generating a scripture index\n", |
| 954 |
" --bible FILE (default: ", BIBLEDEFAULT, ")\n", |
| 955 |
"\n", |
| 956 |
" -l LOCALE Override the default system locale (affecting how non-\n", |
| 957 |
" --locale LOCALE English characters are sorted). See your system help\n", |
| 958 |
" for valid LOCALEs.\n", |
| 959 |
"\n", |
| 960 |
" -h Display this help file and stop.\n", |
| 961 |
" --help\n", |
| 962 |
"\n", |
| 963 |
" -v Print version information and stop.\n", |
| 964 |
" --version\n", |
| 965 |
"\n", |
| 966 |
"If omitted, [output.sbx] defaults to the input filename but with the file\n", |
| 967 |
"extension renamed to '.sbx'. To read or write to stdin or stdout, use '-'\n", |
| 968 |
"in place of input.sxd or output.sbx.\n", |
| 969 |
"\n", |
| 970 |
"See http://songs.sourceforge.net for support.\n") |
| 971 |
return 0 |
| 972 |
elseif arg[i] == "-b" or arg[i] == "--bible" then |
| 973 |
if biblename then |
| 974 |
io.stderr:write("songidx: multiple bible files specified\n") |
| 975 |
return 2 |
| 976 |
end |
| 977 |
i = i + 1 |
| 978 |
if arg[i] then |
| 979 |
biblename = arg[i] |
| 980 |
else |
| 981 |
io.stderr:write("songidx: ",arg[i-1]," option requires an argument\n") |
| 982 |
return 2 |
| 983 |
end |
| 984 |
elseif arg[i] == "-l" or arg[i] == "--locale" then |
| 985 |
if locale then |
| 986 |
io.stderr:write("songidx: multiple locales specified\n") |
| 987 |
return 2 |
| 988 |
end |
| 989 |
i = i + 1 |
| 990 |
if arg[i] then |
| 991 |
locale = arg[i] |
| 992 |
else |
| 993 |
io.stderr:write("songidx: ",arg[i-1]," requires an argument\n") |
| 994 |
return 2 |
| 995 |
end |
| 996 |
elseif arg[i] == "-o" or arg[i] == "--output" then |
| 997 |
if outname then |
| 998 |
io.stderr:write("songidx: multiple output files specified\n") |
| 999 |
return 2 |
| 1000 |
end |
| 1001 |
i = i + 1 |
| 1002 |
if arg[i] then |
| 1003 |
outname = arg[i] |
| 1004 |
else |
| 1005 |
io.stderr:write("songidx: ",arg[i-1]," option requires an argument\n") |
| 1006 |
return 2 |
| 1007 |
end |
| 1008 |
elseif arg[i]:sub(1,1) == "-" and arg[i] ~= "-" then |
| 1009 |
io.stderr:write("songidx: unknown option ",arg[i],"\n") |
| 1010 |
return 2 |
| 1011 |
elseif not inname then inname = arg[i] |
| 1012 |
elseif not outname then outname = arg[i] |
| 1013 |
else |
| 1014 |
io.stderr:write("songidx: too many command line arguments\n") |
| 1015 |
return 2 |
| 1016 |
end |
| 1017 |
i = i + 1 |
| 1018 |
end |
| 1019 |
|
| 1020 |
if not locale then |
| 1021 |
os.setlocale("") |
| 1022 |
elseif not os.setlocale(locale) then |
| 1023 |
io.stderr:write("songidx: invalid locale: ",locale,"\n") |
| 1024 |
return 2 |
| 1025 |
end |
| 1026 |
|
| 1027 |
if not inname then |
| 1028 |
io.stderr:write("songidx: no input file specified\n") |
| 1029 |
return 2 |
| 1030 |
end |
| 1031 |
if not outname then |
| 1032 |
if inname == "-" then |
| 1033 |
outname = "-" |
| 1034 |
else |
| 1035 |
local n |
| 1036 |
outname,n = inname:gsub("%.[^%./\\]*$", ".sbx") |
| 1037 |
if n == 0 then outname = inname .. ".sbx" end |
| 1038 |
end |
| 1039 |
end |
| 1040 |
if not biblename then biblename = BIBLEDEFAULT end |
| 1041 |
|
| 1042 |
fs = fileopen(inname) |
| 1043 |
if not fs then return 2 end |
| 1044 |
|
| 1045 |
local retval = 2 |
| 1046 |
local buf = filereadln(fs) |
| 1047 |
if not buf then |
| 1048 |
io.stderr:write("songidx:",fs["filename"],": file is empty\n") |
| 1049 |
fileclose(fs) |
| 1050 |
elseif buf == "TITLE INDEX DATA FILE" then |
| 1051 |
retval = genindex(fs,outname,false) |
| 1052 |
elseif buf == "SCRIPTURE INDEX DATA FILE" then |
| 1053 |
retval = genscriptureindex(fs,outname,biblename) |
| 1054 |
elseif buf == "AUTHOR INDEX DATA FILE" then |
| 1055 |
retval = genindex(fs,outname,true) |
| 1056 |
else |
| 1057 |
io.stderr:write("songidx:",fs["filename"],":",fs["lineno"],": file has unrecognized format\n") |
| 1058 |
fileclose(fs) |
| 1059 |
end |
| 1060 |
|
| 1061 |
if retval == 0 then |
| 1062 |
io.stderr:write("songidx: Done!\n") |
| 1063 |
elseif retval == 1 then |
| 1064 |
io.stderr:write("songidx: COMPLETED WITH ERRORS. SEE ABOVE.\n") |
| 1065 |
else |
| 1066 |
io.stderr:write("songidx: FAILED. SEE ERROR MESSAGES ABOVE.\n") |
| 1067 |
end |
| 1068 |
|
| 1069 |
return retval |
| 1070 |
end |
| 1071 |
|
| 1072 |
os.exit(main()) |
| 1073 |
|