Jump to content

Module:Biblecontents

Permanently protected module
From Wikisource

require('strict')

local p = {}

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local TableTools = require('Module:TableTools')
local bibleData = require('Module:Biblecontents/data')

local function templatestyles_tag(src)
	return mw.getCurrentFrame():extensionTag('templatestyles', '', {['src'] = src})
end

local function book_id(name)
	return string.gsub(string.lower(name), ' ', '')
end

local function biblecontents(args)
	local testing = yesno(args.testing) or mw.title.getCurrentTitle().prefixedText == 'Template:Biblecontents/testcases'
	
	local version = args.version
	
	local versionData = TableTools.shallowClone(bibleData[version or 'no-version'] or bibleData['default'])
	local defaultData = TableTools.shallowClone(bibleData['default'])
	
	for k, v in pairs(defaultData) do
		versionData[k] = versionData[k] or defaultData[k]
	end
	
	if not testing and not mw.title.getCurrentTitle():inNamespaces(0, 114) then
		versionData['categories'] = {}
	end
	
	local stylesheets = table.concat({
		templatestyles_tag('Template:Rule/styles.css'),
		templatestyles_tag('Template:Flatlist/styles.css'),
		templatestyles_tag('Template:Biblecontents/styles.css')
	})
	
	local bar = ''
	if not yesno(args['nobar']) then
		bar = mw.html.create('hr'):addClass('"wst-rule wst-rule-center __uirule wst-biblecontents-rule')
	end
	
	local biblecontents_div = mw.html.create('div'):addClass('wst-biblecontents')
	
	for i, s in ipairs(versionData['sections']) do
		local section = (s['id'] and versionData['section-overrides'][s['id']]) or s
		
		if not section['id'] or versionData['inclusions'][section['id']] ~= false then
			if section['name'] then
				biblecontents_div:tag('p')
					:addClass('wst-biblecontents-section')
					:wikitext(section['name'])
			end
			
			local booklinks = {}
			for j, book in ipairs(section['books']) do
				local name = book['name']
				local id = book['id'] or book_id(name)
				local alias = versionData['aliases'][id] or name
				
				if alias and versionData['inclusions'][id] ~= false then
					table.insert(booklinks, '[[' .. versionData['link'](version, alias) .. '|' .. alias .. ']]')
				end
			end
			if #booklinks > 0 then
				local ul = biblecontents_div:tag('div'):addClass('wst-flatlist'):tag('ul')
				for j, booklink in ipairs(booklinks) do
					ul:tag('li'):wikitext(booklink)
				end
			end
		end
	end
	
	return stylesheets .. tostring(bar) .. tostring(biblecontents_div) .. table.concat(versionData['categories'])
end

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

return p