Module:Government

From NPOWiki
Revision as of 02:01, 5 July 2021 by Bobogoobo (talk | contribs) (added support for special duties)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Government/doc

local list = mw.loadData('Module:Government/list')
local p = {}

--keys must be lowercase with spaces removed, all input will be converted as such
local shortcuts = {
	admin = 'Administrator',
	io = 'Imperial Officer',
	mod = 'Moderator',
}

local function strip(s)
	--trim, lowercase, and remove non-alphanumeric characters - same as in /list
	return mw.text.trim(mw.ustring.gsub(mw.ustring.lower(s or ''), '%W', ''))
end

local function link(name, game, showDuties)
	--delimiters for duty descriptions
	local dutiesOpen = '['
	local dutiesClose = ']'
	local person = ''
	local duties = ''
	local dutiesStart = mw.ustring.find(name, dutiesOpen, 1, true)
	local dutiesEnd = mw.ustring.find(name, dutiesClose, 1, true)
	local text
	
	if dutiesStart and dutiesEnd and dutiesStart < dutiesEnd then
		person = mw.text.trim(mw.ustring.sub(name, 1, dutiesStart - 1))
		duties = mw.text.trim(mw.ustring.sub(name, dutiesStart + 1, dutiesEnd - 1))
	elseif dutiesStart or dutiesEnd then
		return '<span class="error">duties parse error: ' .. name .. '</span>'
	else
		person = name
	end
	
	if game == 'Community' then
		text = '[' .. '[' .. person .. ']]'
	else
		text = mw.getCurrentFrame():expandTemplate({ title='gamepage', args={ person, game=game } })
	end
	if showDuties and duties ~= '' then
		text = text .. ' (' .. duties .. ')'
	end
	
	return text
end

function p.main(frame)
	return p.call(frame:getParent().args)
end

function p.call(args)
	--separator between officeholders in /list
	local sep = ', '
	local game = args[1]
	local position = args[2]
	local bullet = strip(args.bull)
	local desc = strip(args.desc)
	local text = ''
	local incumbent
	
	if not game or not position then
		return '<span class="error">game and position must be specified</span>'
	end
	if not list[strip(game)] then
		return '<span class="error">invalid government category: ' .. game .. '</span>'
	end
	if shortcuts[strip(position)] then
		position = shortcuts[strip(position)]
	end
	incumbent = list[strip(game)][strip(position)]
	if not incumbent then
		return '<span class="error">invalid government position: ' .. position .. '</span>'
	elseif incumbent == '' then
		return "''vacant''"
	end
	if bullet == 'yes' then bullet = true elseif bullet == 'no' then bullet = false else bullet = nil end
	desc = desc ~= ''
	
	if not mw.ustring.find(incumbent, sep, 1, true) then
		--add bullet only if specified on
		if bullet then text = text .. '*' end
		text = text .. link(incumbent, game, desc)
	else
		--add bullet if not specified off
		if bullet ~= false then bullet = true end
		for holder in mw.text.gsplit(incumbent, sep, true) do
			if bullet then text = text .. '*' end
			text = text .. link(holder, game, desc) .. '\n'
		end
		if not bullet then
			text = mw.ustring.gsub(mw.text.trim(text), '\n', ', ')
		end
	end
	
	return mw.text.trim(text)
end

return p