Module:Alternating colored text

From Uncyclopedia, the content-free encyclopedia
Jump to navigation Jump to search

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

local function isNotEmpty(v)
	return v ~= nil and v ~= ''
end

function p.main(frame)
	
	local args = getArgs(frame)
	
	local color1 = args.color1 or ''
	local color2 = args.color2 or ''
	local color3 = args.color3 or args.c or ''
	local style = args.style or ''
	
	local out = {}
	
	if not isNotEmpty(args.color1) then
		color1 = args[1]
	end
	if not isNotEmpty(args.color2) then
		color2 = args[2]
	end
	
	table.insert(out,'<span class="alternating-colored-text" style="' .. style .. '">') -- text container

	local startIndex = 3
	
	if isNotEmpty(args.color1) and isNotEmpty(args.color2) then
		startIndex = 1
	elseif not isNotEmpty(args.color1) and not isNotEmpty(args.color2) then
		startIndex = 3
	else
		startIndex = 2
		mw.addWarning('<code>color1</code> and <code>color2</code> must be used together!')
	end
	
	local startOffset = startIndex - 1
	
	local colorVal = ''
	for i = startIndex, math.huge do
		local letter = args[i]
		if not letter or letter == '' then
			break
		end
		
		if isNotEmpty(color3) and (i - startOffset) % 3 == 0 then
			colorVal = color3
		elseif (i - startOffset) % 2 == 0 then
			colorVal = color2
		else
			colorVal = color1
		end
		
		table.insert(out, '<span style="color:' .. colorVal .. '">' .. letter .. '</span>')
	end
	
	table.insert(out, '</span>') -- text container end
	
	return table.concat(out)
	
end

return p