Module:Process box

From Wikimania

Documentation for this module may be created at Module:Process box/doc

local getArgs = require('Module:Arguments').getArgs
local p = {}

function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end

function p._main(args)
	local data = parseArguments(args)
	return prepareTemplateBox(data)
end

function parseArguments(args)
	local data = {
		phases = {}, -- for each phase: name, hover text, content, colors, images
		currentPhase = 1,
		title = '',
		content = nil,
		imageSize = 32, -- default image size in template
		imageLeft = nil,
		imageLeftSize = nil, -- override size for left image
		imageRight = nil,
		imageRightSize = nil, -- override size for right image
	}
	
	for k, v in pairs(args) do
		if string.find(k, 'phase ', 0, true) == 1 then
			local phaseNum, subkey = string.match(k, '^phase (%d+) (.+)$')
			phaseNum = tonumber(phaseNum)
			if phaseNum ~= nil then
				if data.phases[phaseNum] == nil then
					data.phases[phaseNum] = {}
				end
				data.phases[phaseNum][subkey] = v
			end
		elseif k == 'current phase' then
			data.currentPhase = tonumber(v, 10) or data.currentPhase
		elseif k == 'title' then
			data.title = v
		elseif k == 'content' then
			data.content = v
		elseif k == 'image size' then
			data.imageSize = tonumber(v, 10) or data.imageSize
		elseif k == 'image left' then
			data.imageLeft = v
		elseif k == 'image left size' then
			data.imageLeftSize = tonumber(v, 10) or data.imageLeftSize
		elseif k == 'image right' then
			data.imageRight = v
		elseif k == 'image right size' then
			data.imageRightSize = tonumber(v, 10) or data.imageRightSize
		end
	end
	
	return data
end

function prepareTemplateBox(data)
	local output = {
		'<div class="tpl-process-box">',
		'<div class="tpl-process-box-header">',
		prepareTemplateHeader(data),
		'</div>',
		prepareTemplateContent(data),
		'</div>'
	}
	
	return table.concat(output)
end

function prepareTemplateHeader(data)
	local output = {}
	
	for i, phase in ipairs(data.phases) do
		local hoverText = phase.hover or ''
		local activeColor = phase.color or ''
		local phaseName = phase.name or '(unnamed phase)'
		if i ~= data.currentPhase then
			table.insert(output, '<span title="' .. hoverText .. '"></span>')
		else
			table.insert(output, '<span class="phase-active ' .. activeColor .. '">' .. phaseName .. '</span>')
		end
	end
	
	return table.concat(output)
end

function prepareTemplateContent(data)
	local phase = data.phases[data.currentPhase] or { content = '(undefined phase selected)'}
	local imageLeft = phase['image left'] or data.imageLeft
	local imageLeftSize = data.imageLeftSize or data.imageSize
	local imageRight = phase['image right'] or data.imageRight
	local imageRightSize = data.imageRightSize or data.imageSize
	local content = phase.content or data.content or '(no content specified)'
	
	local output = {}
	if imageLeft then
		table.insert(output, '<div class="tpl-process-box-image img-left" style="width:' .. imageLeftSize .. 'px">[[File:' .. imageLeft .. '|' .. imageLeftSize .. 'x' .. imageLeftSize .. 'px|link=]]</div>')
	end
	table.insert(output, '<div class="tpl-process-box-content"><p><b>' .. data.title .. '</b></p>\n\n' .. content .. '\n</div>')
	if imageRight then
		table.insert(output, '<div class="tpl-process-box-image img-right" style="width:' .. imageRightSize .. 'px">[[File:' .. imageRight .. '|' .. imageRightSize .. 'x' .. imageRightSize .. 'px|link=]]</div>')
	end
	
	return table.concat(output)
end

return p