Jump to content

Module:CTA header

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jc86035 (talk | contribs) at 09:52, 6 July 2017 (ce). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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

function p.main(frame)
	local args = getArgs(frame, {parentOnly = true})
	local function getArgNums(prefix)
	-- Returns a table containing the numbers of the arguments that exist
	-- for the specified prefix. For example, if the prefix was 'data', and
	-- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
		local nums = {}
		for k, v in pairs(args) do
	 		local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
			if num then table.insert(nums, tonumber(num)) end
		end
		table.sort(nums)
		return nums
	end
	local colors, total = getArgNums('line'), 0
	for k, v in ipairs(colors) do
		colors[k] = frame:expandTemplate{ title = 'CTA color', args = {args['line'..v]} }
		total = total + 1
	end
	local gradient = 'linear-gradient(to bottom'
	for k, v in ipairs(colors) do
		gradient = gradient..', #'..colors[k]..' '..(k-1)*100/total..'%, #'..colors[k]..' '..k*100/total..'%'
	end
	gradient = gradient..') 0.1%'
	local name = args.name or ''
	local coord = args.coord or ''
	local size, length, br, size1, align = '100%', mw.ustring.len(name), mw.ustring.match(name, '< */? *[Bb][Rr] */? *>'), '200%', {'', ''}
	if br then
		size1 = '100%'
	elseif length > 6 then
		size = 100*((1/(0.35*(length-6)+1.7))+0.4) .. '%'
	end
	return '<div style="color:white;background:dimgray;height:38px;display:block;vertical-align:middle;line-height:38px;border-left:50px solid #'..(colors[1] or '000000')..';border-right:50px solid #'..(colors[1] or '000000')..';font-size:'..size..';border-image:'..gradient..'"><div style="display:inline-block;vertical-align:middle;line-height:1;font-size:'..size1..';text-align:left">'..name..'</div> <div style="display:inline-block;vertical-align:middle;line-height:0.9;text-align:right">'..coord..'</div></div>'
end

return p