Jump to content

Module:Bibleversions

From Wikisource

require('strict')

local p = {}

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local TableTools = require('Module:TableTools')

local function book_id(name)
	return string.gsub(string.gsub(string.gsub(string.lower(name), ' ', ''), 'þ', 'th'), 'ſ', 's')
end

local function year_comp(year1, year2)
	if tonumber(year1) and tonumber(year2) then
		return year1 < year2
	elseif tonumber(year1) then
		return false
	elseif tonumber(year2) then
		return true
	else
		return false
	end
end

local function sortedBibleData()
	local bibleData = require('Module:Biblecontents/data')
	
	local sortedData = {}
	local defaultData = TableTools.shallowClone(bibleData['default'])
	for key, value in pairs(bibleData) do
		local versionData = TableTools.shallowClone(value)
		versionData['version'] = key
		if versionData['version'] ~= 'default' and versionData['version'] ~= 'no-version' then
			for k, v in pairs(defaultData) do
				versionData[k] = versionData[k] or v
			end
			
			versionData['version-display'] = versionData['version-display'] or versionData['version']
			versionData['start-year'] = tonumber(versionData['start-year'])
			versionData['end-year'] = tonumber(versionData['end-year'])
			
			local sections = {}
			local included_books = {}
			for i, s in ipairs(versionData['sections']) do
				local section = (s['id'] and versionData['section-overrides'][s['id']]) or s
				table.insert(sections, section)
				for j, book in ipairs(section['books']) do
					included_books[book['id'] or book_id(book['name'])] = book['name']
				end
			end
			versionData['sections'] = sections
			versionData['included-books'] = included_books
			
			table.insert(sortedData, versionData)
		end
	end
	table.sort(sortedData, function (a, b)
		if a['start-year'] == b['start-year'] then
			return year_comp(a['end-year'], b['end-year'])
		else
			return year_comp(a['start-year'], b['start-year'])
		end
	end)
	
	return sortedData
end

local function version_text(versionData, id)
	-- get book
	local book = versionData['included-books'][id]
	
	if not book then
		return nil
	end
	
	local edition_info = ''
	if versionData['edition-info'] then
		edition_info = ' (' .. versionData['edition-info'] .. ')'
	elseif versionData['start-year'] or versionData['end-year'] then
		edition_info = ' (' .. table.concat(TableTools.compressSparseArray({versionData['start-year'], versionData['end-year']}), '–') .. ')'
	end
	
	return '\"[[' .. versionData['link'](versionData['version'], book) .. '|' .. book .. ']]\" in the \'\'[[' .. versionData['link'](versionData['version'], nil) .. '|' .. versionData['version-display'] .. ']]\'\' version' .. edition_info
end

--[=[
function p.checkBibleDataIDs()
	local bibleData = require('Module:Biblecontents/data')
	local sortedData = sortedBibleData()
	
	local idList = {}
	local knownExceptions = {
		esdrasprologue = true,
		laodiceans = true,
		ecclesiasticus = true,
		kingsprologue = true,
		catholicepistlesprologue = true,
		['4kings'] = true,
		['3kings'] = true,
		joshuajudgesruthprologue = true,
		zacharias = true,
		['3esdras'] = true,
		['4esdras'] = true,
		preierofjeremye = true,
		syrach = true,
		bel = true,
		prayerofazariasandhymnofthethreechildren = true,
		enoch = true,
		grcesther = true,
		grcdaniel = true,
		ljeremiah = true,
		psalm151 = true,
		pilate = true,
		philip = true,
		['1clement'] = true,
		['2clement'] = true,
		peter = true,
		['3maccabees'] = true,
		['4maccabees'] = true,
		['5maccabees'] = true,
		['6maccabees'] = true,
		['7maccabees'] = true,
		['8maccabees'] = true,
		['1meqabyan'] = true,
		['2meqabyan'] = true,
		['3meqabyan'] = true,
		['3enoch'] = true,
		['psolomon'] = true,
		['restofbaruch'] = true,
		['1enoch'] = true,
		['2enoch'] = true,
		['odes'] = true,
		['jubilees'] = true
	}
	for i, section in ipairs(bibleData['default']['sections']) do
		for j, book in ipairs(section['books']) do
			idList[book['id'] or book_id(book['name'])] = book['name']
		end
	end
	for i, versionData in ipairs(sortedData) do
		for k, v in pairs(versionData['included-books']) do
			if not idList[k] and not knownExceptions[k] then
				error('Unrecognized id \"' .. k .. '\" for ' .. versionData['version'])
			end
		end
	end
end
]=]

local function bibleversions(args)
	local bibleData = sortedBibleData()
	
	local id = book_id(args['id'] or args[1] or '')
	
	local ul = mw.html.create('ul')
	for i, versionData in ipairs(bibleData) do
		local text = version_text(versionData, id)
		if text then
			ul:tag('li'):wikitext(text)
		end
	end
	
	return ul
end

function p.bibleversions(frame)
	return bibleversions(getArgs(frame))
end

return p