Die Dokumentation für dieses Modul kann unter Modul:Partei/Doku erstellt werden

local p = {}
local cfg = mw.loadData('Modul:Partei/Liste');
local cfx = mw.loadData('Modul:Partei/Akronyme');	
local getArgs = require('Modul:Arguments').getArgs

-- §1 DELINK
local function delink(partei)
	local a = partei
	local n1 = mw.ustring.find(a,'%[%[')
	if (n1) then
		local b = mw.ustring.sub(a,n1+2)
		local n2 = mw.ustring.find(b,'%|')
		local n3 = mw.ustring.find(b,'%]%]')
		local c = mw.ustring.sub(a,n1+2,n3+n1)
		local n4 = mw.ustring.find(c,'%|')
		if (n4) then
			a = mw.ustring.sub(a,n1+2,n2+n1)
			else
			a = c
		end
		else
		if (n3) then
			if (n2) then
				a = mw.ustring.sub(a,n1+2,n2+n1)
				else
				a = c
			end
		end
	end
	a = mw.text.decode(a)
	return a
end

-- §2 PARTEINAME/AKRONYM
function p.parteiname(partei)
	local a = partei
	local full = cfx.akronym[a] or ''
	return full[1]
end

function p.name(frame)
	local args = getArgs(frame)
	local partei = args[1]
	local land = args[2]
	if land then kom = p.parteiname(land..'-'..partei) end
	return kom or delink(partei)
end

-- §3 STANDARD FARBE
function p.farbe(frame)
	local partei = p.name(frame)
	partei = cfg.altname[partei] or partei
	local farbe = cfg.info[partei] or ''
	return farbe[1] or 'DABAD0'
end

-- §4 ALTERNATIVE FARBE
function p.alt(frame)
	local partei = p.name(frame)
	partei = cfg.altname[partei] or partei
	local farbe = cfg.info[partei] or ''
	return farbe[3] or 'DABAD0'
end

-- §5 DUNKEL FARBE: STANDARD/ALTERNATIVE
local function farben(frame)
	local args = getArgs(frame)
	local v = args['V']
	if v == 'A' then return p.alt(frame)
	elseif v == 'HA' then return p.alt(frame) 
	elseif mw.ustring.find(args[1],' ') then return p.alt(frame)
	else return p.farbe(frame) or 'DABAD0'
	end
end

-- §6: HELLERE FARBE: STANDARD/ALTERNATIVE
function p.hell(frame)
	local args = getArgs(frame)
	local opaz = tonumber(args['h']) or 0.2
	local farbe = args['col'] or farben(frame)
	local cls = {}
	if (farbe:len() == 6) then
		cls[1] = tonumber(farbe:sub(1,2),16) or 255
		cls[2] = tonumber(farbe:sub(3,4),16) or 255
		cls[3] = tonumber(farbe:sub(5,6),16) or 255
	end
	cls[1] = 255 - math.floor((255-cls[1])*opaz)
	cls[2] = 255 - math.floor((255-cls[2])*opaz)
	cls[3] = 255 - math.floor((255-cls[3])*opaz)
	return string.format('%02X%02X%02X',cls[1],cls[2],cls[3])
end

-- §7 PARTEINAMEFARBE
function p.parteinamefarbe(frame)
	local args = getArgs(frame)
	local farbe = '#' .. farben(frame)
	local spaltef = 'rowspan="%s" style="width:2px; background-color:%s;"|\n'
	local spaltek = '| scope="row" rowspan="%s" colspan="%s" style="text-align:left;"|%s\n'
	local spaltep = '| scope="row" rowspan="%s" colspan="%s" style="text-align:left;"|%s'
	local rowspan = tonumber(args.rowspan) or 1
	local colspan = tonumber(args.colspan) or 1
	spaltef = string.format(spaltef, rowspan, farbe)
	if args['k'] then spaltek = string.format(spaltek, rowspan, colspan, args['k']) end
	spaltep = string.format(spaltep, rowspan, colspan, args[1])
	if args['k'] then return(spaltef..spaltek..spaltep) else
	return (spaltef..spaltep) end
end

-- §
local function namelink(frame)
	local args = getArgs(frame)
	local part = args[1]
	local land = args[2]
	if land then return cfx.akronym[(land..'-'..part)] end
end

-- §8 PIPELINK (Akronym, Land > [[Name|Akronym]])
function p.pipelink(frame)
	local args = getArgs(frame)
	local link = namelink(frame)
	if link then
		if link['sequenz'] then return link['sequenz']
		elseif link['kurz'] then return '[['..link[1]..'|'..link['kurz']..']]'
		else return '[['..link[1]..'|'..args[1]..']]' end
	else return args[1] end
end

-- §9 FULL (Akronym, Land > [[Name]], oder [[Name (Begriffsklärung)|Name]])
function p.full(frame)
	local args = getArgs(frame)
	local link = namelink(frame)
	if link then
		if link['full'] then return '[['..link[1]..'|'..link['full']..']]'
		else return '[['..link[1]..']]' end
	end
end

-- §10 PARTEIFARBEZEILE
function p.parteifarbezeile(frame)
	local args = getArgs(frame)
	local rowspan = tonumber(args.rowspan) or 1
	local colspan = tonumber(args.colspan) or 1
	local width = (tonumber(args.w) or 0) .. 'px'
	local def = args.k
	local farbe = '#' .. farben(frame)
	local zeile = 'rowspan="%s" colspan="%s" style="border-bottom:3px solid; border-bottom-color:%s; width:%s"|' .. (def or p.pipelink(frame) or args[1]) .. '\n'
	return string.format(zeile, rowspan, colspan, farbe, width)
end

-- S11 PARTEIFARBEZELLE
function p.parteifarbezelle(frame)
	local args = getArgs(frame)
	local rowspan = tonumber(args.rowspan) or 1
	local colspan = tonumber(args.colspan) or 1
	local v = args['V']
	if v == 'H' then farbe = '#' .. p.hell(frame)
	elseif v == 'HA' then farbe = '#' .. p.hell(frame)
	else farbe = '#' .. farben(frame) end
	local zelle = 'rowspan="%s" colspan="%s" style="background-color:%s;"'
	return string.format(zelle, rowspan, colspan, farbe)
end

-- §12 PARTEISITZE
function p.parteisitze(frame)
	local args = getArgs(frame)
	local x = args[1]
	local y = args[2]
	local pagename = mw.title.getCurrentTitle().prefixedText
	if args[4] then a = p.name({args[3], args[4]}) else
		if args[3] == 'P' then a = pagename
			else a = args[3]
		end
	end
	local v = args['V']
	if args['f'] then c = args['f'] else
		if v then c = p.alt({a}) else
		c = p.farbe({a})
		end
	end
	local q = tonumber(string.format('%.1f', x / y * 100))
	z = string.gsub(q, "%.", "%,")
	if args['w'] then w = '<span style="font-size:80%"> ('..z..' %)</span>' else w = '' end
	local k = (x / y * 100)..'%'
	local mandate = '<div style="width:100px">'..x..' / '..y..'%s</div><div style="width:100px;border:0.1px solid #aaa;background-color:#FFF;height:0.6em;position:relative;text-align:left;"><div style="background-color:#%s;width:%s;height:0.6em;"></div></div>'
	return string.format(mandate, w, c, k)
end

-- §13 FARBENLISTE
function p.farbenliste(frame)
	local root = mw.html.create('table'):addClass('wikitable sortable')
	root:tag('tr')
		:tag('th'):attr('rowspan', '2'):wikitext('Partei'):done()
		:tag('th'):attr('colspan', '2'):wikitext('Farbe'):done()
		:tag('th'):attr('rowspan', '2'):wikitext('Staat'):done()
		:tag('th'):attr('colspan', '2'):wikitext('Alt. Farbe'):done()
	root:tag('tr')
		:tag('th'):addClass('unsortable'):wikitext('Ausgabe'):done()
		:tag('th'):wikitext('Code'):done()
		:tag('th'):addClass('unsortable'):wikitext('Ausgabe'):done()
		:tag('th'):wikitext('Code'):done()
	local liste = {}
	for k, v in pairs(cfg.info) do
		liste[#liste+1] = k
	end
	table.sort(liste)
	for _, value in pairs(liste) do
	local info = cfg.info[value]
	if info[3] == nil then alt = '' else alt = 'background-color:#' .. info[3] end
	root:tag('tr')
		:tag('td'):wikitext('[[' .. value .. ']]'):done()
		:tag('td'):css('background-color', '#' .. info[1]):done()
		:tag('td'):wikitext(info[1]):done()
		:tag('td'):wikitext(info[2]):done()
		:tag('td'):cssText(alt):done()
		:tag('td'):wikitext(info[3]):done()
	end
	return root
end

-- §14 LISTE DER ALTERNATIVEN NAMEN
function p.altnamenliste(frame)
	local root = mw.html.create('table'):addClass('wikitable sortable')
	root:tag('tr')
		:tag('th'):wikitext('Alternativer name'):done()
		:tag('th'):wikitext('Partei'):done()
	local altn = {}
	for k, v in pairs(cfg.altname) do
		altn[#altn+1] = k
	end
	table.sort(altn)
	for _, name in ipairs(altn) do
	root:tag('tr')
		:tag('td'):wikitext('[[' .. name .. ']]'):done()
		:tag('td'):wikitext('[[' .. cfg.altname[name] ..']]'):done()
	end
	return root
end

-- §15 AKRONYME LISTE
function p.akronymeliste(frame)
	local root = mw.html.create('table'):addClass('wikitable sortable')
	root:tag('tr')
		:tag('th'):wikitext('Staat-Akronym'):done()
		:tag('th'):wikitext('Staat'):done()
		:tag('th'):wikitext('Akronym'):done()
		:tag('th'):wikitext('Parteiname'):done()
		:tag('th'):wikitext('Parteien'):done()
	local liste = {}
	for k, v in pairs(cfx.akronym) do
		liste[#liste+1] = k
	end
	table.sort(liste)
	for _, value in pairs(liste) do
	local akr = cfx.akronym[value]
	root:tag('tr')
		:tag('td'):wikitext('' .. value .. ''):done()
		:tag('td'):wikitext(string.sub('' .. value .. '', 0, 2)):done()
		:tag('td'):wikitext(string.sub('' .. value .. '', 4)):done()
		:tag('td'):wikitext('[['..akr[1]..']]'):done()
		:tag('td'):wikitext(akr[2]):done()
	end
	return root
end

return p