Module:Government: Difference between revisions

From NPOWiki
Jump to navigation Jump to search
create
 
use error module
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
local list = mw.loadData('Module:Government/list')
local list = mw.loadData('Module:Government/list')
local err = require('Module:Error').call
local p = {}
local p = {}


--keys must be lowercase with spaces removed, all input will be converted as such
--keys must be lowercase with spaces, dashes, etc, removed; all input will be converted as such
local shortcuts = {
local shortcuts = {
admin = 'Administrator',
admin = 'Administrator',
mod = 'Moderator',
io = 'Imperial Officer',
io = 'Imperial Officer',
mod = 'Moderator',
ioma = 'Imperial Officer of Military Affairs',
ioea = 'Imperial Officer of Economic Affairs',
ioia = 'Imperial Officer of Internal Affairs',
iofa = 'Imperial Officer of Foreign Affairs',
soma = 'Senator of Military Affairs',
soia = 'Senator of Internal Affairs',
soda = 'Senator of Diplomatic Affairs',
}
}


Line 14: Line 22:
end
end


local function link(name, game)
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 err('duties parse error: ' .. name)
else
person = name
end
if game == 'Community' then
if game == 'Community' then
return '[' .. '[' .. name .. ']]'
text = '[' .. '[' .. person .. ']]'
else
else
return mw.getCurrentFrame():expandTemplate({ title='gamepage', args={ name, game=game } })
text = mw.getCurrentFrame():expandTemplate({ title='gamepage', args={ person, game=game } })
end
end
if showDuties and duties ~= '' then
text = text .. ' (' .. duties .. ')'
end
return text
end
end


Line 32: Line 63:
local position = args[2]
local position = args[2]
local bullet = strip(args.bull)
local bullet = strip(args.bull)
local desc = strip(args.desc)
local text = ''
local text = ''
local incumbent
local incumbent
if not game or not position then
if not game or not position then
return '<span class="error">game and position must be specified</span>'
return err('game and position must be specified')
end
if strip(game) == 'community' then
game = 'Community'
else
game = mw.getCurrentFrame():expandTemplate({ title='abbrgame', args={ game, 'full' } })
end
end
if not list[strip(game)] then
if not list[strip(game)] then
return '<span class="error">invalid government category: ' .. game .. '</span>'
return err('invalid government category: ' .. args[1])
end
end
if shortcuts[strip(position)] then
if shortcuts[strip(position)] then
Line 46: Line 83:
incumbent = list[strip(game)][strip(position)]
incumbent = list[strip(game)][strip(position)]
if not incumbent then
if not incumbent then
return '<span class="error">invalid government position: ' .. position .. '</span>'
return err('invalid government position: ' .. args[2])
elseif incumbent == '' then
elseif incumbent == '' then
return "''vacant''"
return "''vacant''"
end
end
if bullet == 'yes' then bullet = true elseif bullet == 'no' then bullet = false else bullet = nil 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
if not mw.ustring.find(incumbent, sep, 1, true) then
--add bullet only if specified on
--add bullet only if specified on
if bullet then text = text .. '*' end
if bullet then text = text .. '*' end
text = text .. link(incumbent, game)
text = text .. link(incumbent, game, desc)
else
else
--add bullet if not specified off
--add bullet if not specified off
Line 61: Line 99:
for holder in mw.text.gsplit(incumbent, sep, true) do
for holder in mw.text.gsplit(incumbent, sep, true) do
if bullet then text = text .. '*' end
if bullet then text = text .. '*' end
text = text .. link(holder, game) .. '\n'
text = text .. link(holder, game, desc) .. '\n'
end
end
if not bullet then
if not bullet then

Latest revision as of 11:20, 21 October 2025

Documentation [edit]

This page is a stub - it only covers the very basics of its subject. More information should be added to make the page informative and useful. If no more information is available, the page should be considered for a merge, redirect, or deletion.

Details: documentation to be written

If you can correct the issue, please edit the page to do so, then remove this notice.
Please see Template:Gov for usage information. Use the template rather than invoking the module. Data for this module is imported from Module:Government/list.

The above documentation is transcluded from Module:Government/doc.

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

--keys must be lowercase with spaces, dashes, etc, removed; all input will be converted as such
local shortcuts = {
	admin = 'Administrator',
	mod = 'Moderator',
	io = 'Imperial Officer',
	ioma = 'Imperial Officer of Military Affairs',
	ioea = 'Imperial Officer of Economic Affairs',
	ioia = 'Imperial Officer of Internal Affairs',
	iofa = 'Imperial Officer of Foreign Affairs',
	soma = 'Senator of Military Affairs',
	soia = 'Senator of Internal Affairs',
	soda = 'Senator of Diplomatic Affairs',
}

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 err('duties parse error: ' .. name)
	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 err('game and position must be specified')
	end
	if strip(game) == 'community' then
		game = 'Community'
	else
		game = mw.getCurrentFrame():expandTemplate({ title='abbrgame', args={ game, 'full' } })
	end
	if not list[strip(game)] then
		return err('invalid government category: ' .. args[1])
	end
	if shortcuts[strip(position)] then
		position = shortcuts[strip(position)]
	end
	incumbent = list[strip(game)][strip(position)]
	if not incumbent then
		return err('invalid government position: ' .. args[2])
	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