Jump to content

Module:Party name with color/sandbox

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by REEDriler (talk | contribs) at 16:56, 25 April 2023 (this was already clear :)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}
local fetch = nil -- lazy load

local function load_political_party_module()
	if fetch == nil then
		fetch = require('Module:Political party linked/sandbox')._tagCfetch
	end
end

local function simplify_rowspan(text)
	text = mw.ustring.gsub(text or '', 'rowspan="1"%s*', '')
	return text
end

local function getData(args)
	load_political_party_module()
	local yesno = require('Module:Yesno')
	local text, color

	if yesno(args.dab) then
		text, color = fetch({args[1], 'dab'})
	elseif yesno(args.full) then
		text, color = fetch({args[1], 'full'})
	elseif args.shortname then
		text, color = fetch({args[1], '_shortname', args.shortname})
	else
		text, color = fetch({args[1], 'shortname'})
	end

	local icolor = args.colour or args.color
	if icolor then color = icolor end

	if yesno(args.no_link) then
		text = require('Module:Delink')._delink{text}
	elseif args.link then
		text = string.format("[[%s|%s]]", args.link, require('Module:Delink')._delink{text})
	end

	if yesno(args.bold) then text = "'''" .. text .. "'''" end
	return text, color
end

function p._cell(args)
	if not args[1] then return '' end
	local text, color = getData(args)

	local line1 = 'rowspan="%s" style="width: 2px; background-color: %s;" data-sort-value="%s" |\n'
	local line2 = '| scope="row" rowspan="%s" style="text-align: left;" | %s'

	local rowspan = tonumber(args.rowspan) or 1
	line1 = string.format(line1, rowspan, color, args[1])
	line2 = string.format(line2, rowspan, text)

	return simplify_rowspan(line1 .. line2)
end

function p._box(args)
	if not args[1] then return '' end
	local text, color = getData(args)

	local seats
	if args.seats then seats = ' (' .. args.seats .. ')' end
	seats = seats or ''

	local outline = args.outline or 'black'

	if not args.inline then -- legend_inline
		return '<span class="legend nowrap" style="page-break-inside:avoid;break-inside:avoid-column"><span class="legend-color" style="display:inline-block;min-width:1.25em;height:1.25em;line-height:1.25;margin:1px 0;border:1px solid ' .. outline .. ';background-color:' .. color .. '"> </span> ' .. text .. seats .. '</span>'
	elseif not require('Module:Yesno')(args.inline) then -- legend
		return '<div class="legend" style="page-break-inside:avoid;break-inside:avoid-column"><span class="legend-color" style="display:inline-block;min-width:1.25em;height:1.25em;line-height:1.25;margin:1px 0;border:1px solid ' .. outline .. ';background-color:' .. color .. '"> </span> ' .. text .. seats .. '</div>'
	else
		return ''
	end
end

function p.cell(frame)
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame)
	local check = require('Module:Check for unknown parameters')._check
	local tracking = check({
		['unknown']='[[Category:Pages using Party name with color with unknown parameters|_VALUE_ ]]',
		['preview']='Page using [[Template:Party name with color]] with unknown parameter "_VALUE_"',
		['showblankpositional']='1',
		'1', 'color', 'colour', 'dab', 'full', 'no_link', 'rowspan', 'shortname', 'bold', 'link'
	}, frame:getParent().args)
	return p._cell(args) .. tracking
end

function p.box(frame)
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame)
	return p._box(args)
end

return p