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 10:34, 6 July 2017 (mobile site). 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 mw.ustring.gsub(mw.ustring.gsub(mw.title.getCurrentTitle().text, '%s+%b()$', '', 1), ' station', '', 1)
	local coord = (args.coord and '&nbsp;<div style="display:inline-block;vertical-align:middle;line-height:0.9;text-align:right">'..mw.ustring.gsub(args.coord, '%s+', '<br/>')..'</div>') or ''
	local tmp = mw.ustring.gsub(name, '< ?/? ?[Ss][Mm][Aa][Ll][Ll] ?>', '')
	local size, length, br, size1, align = '15.4px', mw.ustring.len(tmp), mw.ustring.match(tmp, '< */? *[Bb][Rr] */? *>'), '200%', {'', ''}
	if br then
		size1 = '100%'
		tmp = mw.text.split(tmp, '%s*< */? *[Bb][Rr] */? *>%s*')
		for k, v in ipairs(tmp) do
			tmp[k] = mw.ustring.len(v)
		end
		if tmp[2] > tmp[1] then length = tmp[2] else length = tmp[1] end
		if length > 15 then size = 15.4*((1/(0.1*(length-15)+1.7))+0.4)..'px' end
	elseif length > 6 then
		size = 15.4*((1/(0.35*(length-6)+1.7))+0.4)..'px'
	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 class="fn org" style="display:inline-block;vertical-align:middle;line-height:1;font-size:'..size1..';text-align:left">'..name..'</div>'..coord..'</div>'
end

return p