Module:RQ:wam:Eliot 1663

p = {}

local gb_page_index = mw.loadData("Module:RQ:wam:Eliot 1663/Google Books") local data = mw.loadData("Module:RQ:wam:Eliot 1663/data")

function p.find_verse(list, book, chapter, verse) local ia = nil local gb = nil local val, ia_new, gb_new

-- this could be refactored to use a binary search, but that requires figuring out a better way of handling divergent IA/GB page numbers for index, page in ipairs(list[book]) do   	if type(page) == "string" then val = {string.match(page, "([0-9]+):([0-9]+)('?)")} if ia ~= nil then ia_new = ia + 1 else ia_new = nil end if gb ~= nil then gb_new = gb + 1 else gb_new = nil end else val = {string.match(page[1], "([0-9]+):([0-9]+)('?)")} ia_new = page[2] gb_new = page[3] end if val[1] + 0 > chapter then return {ia, nil, gb, nil} elseif val[1] + 0 == chapter and val[2] + 0 == verse + 1 and #val[3] > 0 then return {ia, nil, gb, nil} elseif val[1] + 0 == chapter and val[2] + 0 == verse + 1 then return {ia, ia_new, gb, gb_new} elseif val[1] + 0 == chapter and val[2] + 0 > verse then return {ia, nil, gb, nil} elseif val[1] + 0 == chapter and val[2] + 0 == verse then return {ia_new, nil, gb_new, nil} end

ia = ia_new gb = gb_new end return {ia, nil, gb, nil} end

local function ia_link(id, page) return "[https://archive.org/details/" .. id .. "/page/n" .. page .. "/mode/1up n" .. page .. "]" end

local function gb_link(id, page) return "[https://google.com/books?id=" .. id .. "&pg=" .. gb_page_index[id][page] .. " &#91;" .. page .. "&#93;]" end

function p.page_numbers(frame) local val = {string.match(frame.args[1], "^%s*(.+) ([0-9]+):([0-9]+)")} local book, chapter, verse, list, ia_id, gb_id

if #val == 0 then val = {string.match(frame.args[1], "^%s*(.+) ([0-9]+)")} book = val[1] chapter = 1 verse = val[2] + 0 else book = val[1] chapter = val[2] + 0 verse = val[3] + 0 end

if data["OT"][book] then list = data["OT"] ia_id = "mamussewunneetup00elio" gb_id = "T46WdWgVbl8C" elseif data["AP"][book] then list = data["AP"] ia_id = "mamussewunneetup00elio" gb_id = "H0axs6sE4-kC" else list = data["NT"] ia_id = "newtestamentofou00elio" end

val = p.find_verse(list, book, chapter, verse) local thru = string.match(frame.args[1], "[-–]([0-9]+)") local thru_val = {} if thru then thru_val = p.find_verse(list, book, chapter, thru + 0) end

local pages = {}

if val[1] or val[2] or thru_val[1] or thru_val[2] then table.insert(pages, {}) local start_val = val[1] or val[2] or thru_val[1] or thru_val[2] local end_val = thru_val[2] or thru_val[1] or val[2] or val[1] table.insert(pages[1], ia_link(ia_id, start_val)) if start_val ~= end_val and not frame.args["concise"] then table.insert(pages[1], ia_link(ia_id, end_val)) end pages[1] = table.concat(pages[1], "–") .. (frame.args["concise"] and " (IA) " or " (IA) ") end

if val[3] or val[4] or thru_val[3] or thru_val[4] then table.insert(pages, {}) local start_val = val[3] or val[4] or thru_val[3] or thru_val[4] local end_val = thru_val[4] or thru_val[3] or val[4] or val[3] table.insert(pages[#pages], gb_link(gb_id, start_val)) if start_val ~= end_val and not frame.args["concise"] then table.insert(pages[#pages], gb_link(gb_id, end_val)) end pages[#pages] = table.concat(pages[#pages], "–") .. (frame.args["concise"] and " (GB) " or " (GB) ") end

if frame.args["concise"] then return table.concat(pages, ", ") end

local full_verse = frame.args[1]:gsub(" %(1661%)", "") local text = frame.args[1]:gsub('^%s*(.-)%s*$', '%1'):gsub("%-","–")

if book ~= "Rules" then text = text .. ", cf. [https://www.biblegateway.com/passage/?search=" .. mw.uri.encode(full_verse) .. "&version=KJV KJV]" end

if #pages > 0 then text = text .. ", "		pages_text = table.concat(pages, ", ") if pages_text:find("–") then text = text .. "pages " else text = text .. "page " end text = text .. pages_text end

return text end

function p.generate_index_helper(x) local output = "" local books = data["index"][x] for _, book in pairs(books) do		local title = book[1] local last_chapter = book[2] output = output .. "=====" .. title .. "=====\n\n" for i = 1, last_chapter do local pn = p.page_numbers({args={title .. " " .. i .. ":1", concise=1}}) if #pn < 5 then pn = "missing (full or part) " end output = output .. "# " .. pn .. "\n" end output = output .. "\n" end return output end

function p.generate_index(frame) local output = "" output = output .. "===Old Testament===\n" .. p.generate_index_helper("OT") output = output .. "===New Testament===\n" .. p.generate_index_helper("NT") output = output .. "===Appendix===\n" .. p.generate_index_helper("AP") return frame:preprocess(output) end

function p.extract_quotes(frame) local args = frame:getParent.args local i = 1 local output = ""

while args[i] do		output = output .. p.page_numbers({args={args[i]:gsub("^%s*(.-)%s*$", "%1")}}) .. "" .. args[i+1]:gsub("^%s*(.-)%s*$", "%1") .. "" .. args[i+2]:gsub("^%s*(.-)%s*$", "%1") .. "" i = i + 3 end

return output end

return p