Module:Graph:Chart political

From Wikipedia, the free encyclopedia

-- this module implements [[Template:Graph:Chart political]]
local p = {}

local function isnotempty(s)
	return s and s:match( '^%s*(.-)%s*$' ) ~= ''
end

function p._chart_args(args)
	local pfetch = require('Module:Political party')._fetch
	local colors = mw.text.split(args['colors'] or '', '%s*[,]%s*')
	local targs = {}
	for k,v in pairs(args) do
		if k:find('^y%d+Party$') then
			local digit = string.match(k, '%d+')
			local color = isnotempty(colors[tonumber(digit)]) and colors[tonumber(digit)] or pfetch({v, 'color'})
			local pname = isnotempty(args['y' .. digit .. 'Title']) and args['y' .. digit .. 'Title'] or pfetch({v, 'abbrev'})
			targs['y' .. digit .. 'Title'] = pname
			colors[tonumber(digit)] = color
		else
			targs[k] = v
		end
	end
	targs['colors'] = string.gsub(table.concat(colors, ', '), '#', '#')

	return targs
end

function p.debug(frame)
	local args = p._chart_args(frame:getParent().args)
	local res = '<pre>\n{{Graph:Chart\n'
	for k,v in pairs(args) do
		res = res .. '| ' .. k .. ' = ' .. v .. '\n'
	end
	res = res .. '}}\n</pre>'
	
	return res
end

function p.main(frame)
	return mw.getCurrentFrame():expandTemplate{
		title = 'Graph:Chart',
		args = p._chart_args(frame:getParent().args)
		}
end

function p.mainlua(frame)
	local graphchart = require('Module:Graph:Chart')
	frame.args = p._chart_args(frame:getParent().args)
	return graphchart[''](frame)
end

return p