Jump to content

Module:Designation

Permanently protected module
From Wikipedia, the free encyclopedia

require('strict')
local p = {}

local get_data = function(frame)
	local args = frame:getParent().args
	local former = false
	if args.delisted=='yes' then
		former = true
	end
	local par
	if args and args[1] and args[1]~='' then
		par = args[1]:lower()
		if par:sub(1, 6)=='former' then
			former = true
			par = par:sub(7) -- strip off "former" from start
		end
		local list = mw.loadJsonData('Module:Designation/list')
		if list[par] then -- lookup not needed
			return list[par], former
		else -- attempt lookup
			local lookup = mw.loadJsonData('Module:Designation/lookup')
			if lookup[par] then -- successful lookup
				par = lookup[par]
				return list[par], former
			else
				local category = mw.title.getCurrentTitle().namespace==0 and '[[Category:Articles using Template:Designation with invalid designation]]' or ''
				return 'Invalid designation' .. category
			end
		end
	else
		return 'No parameter'
	end
end

local text = function(frame, data, former)
	local text = data.text
	if data.lang then
		text = frame:expandTemplate{title = 'lang', args = {
			code = data.lang,
			text = text
		}}
	end
	return text and ((former and 'Former ' or '') .. text)
end

p.text = function(frame)
	local data, former = get_data(frame)
	if type(data)=='string' then
		return data
	end
	return text(frame, data, former)
end

local colour = function(data, former, index)
	local color
	if former then
		color = 'DDDDDD'
	elseif data[index] then
		color = data[index]
	else
		color = 'A8EDEF' -- default colour
	end
	return color and ('#' .. color)
end

p.color = function(frame)
	local data, former = get_data(frame)
	if type(data)=='string' then
		return data
	end
	return mw.text.nowiki(colour(data, former, 'col'))
end

p.color2 = function(frame)
	local data, former = get_data(frame)
	if type(data)=='string' then
		return data
	end
	return mw.text.nowiki(colour(data, former, 'col2'))
end

p.abbr = function(frame)
	local data, former = get_data(frame)
	if type(data)=='string' then
		return data
	end
	return former and '???' or data.abbr or '???'
end

p.divbox = function(frame)
	local data, former = get_data(frame)
	local color = colour(data, former, 'col2')
	local text = text(frame, data, former)
	local div = mw.html.create('div')
		:css('line-height', '1.5')
		:css('text-align', 'center')
	if type(data)=='string' then
		local invalid = mw.html.create('span')
			:css('color', 'red')
			:wikitext('Error: ' .. data)
		div:node(invalid)
	else
		div:css('border-style', 'solid')
			:css('border-width', '4px')
			:css('border-color', color)
			:wikitext(text)
	end
	return tostring(div)
end

return p