Module:Sandbox/Pizza1016/Victoria infobox station name formatting

Revision as of 21:46, 29 April 2022 by imported>Pizza1016
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Sandbox/Pizza1016/Victoria infobox station name formatting/doc

local p = {}

function p.format( frame )
	local args = frame.args  -- expects a sequence of strings
	local lines = require("Module:Adjacent stations/Melbourne").lines
	local vlines = require("Module:Adjacent stations/V/Line").lines
	local result = {}
	local colours = {}  -- colours to display
	local keyset = {}  -- to avoid duplicating colours
	
	-- Obtain all unique colours first, in order of argument input
	local i = 1
	local j = 1
	while args[i] do
		if lines[args[i]] then
			local current = lines[args[i]].color
			if current and not keyset[current] then
				colours[j] = current
				keyset[current] = true
				j = j + 1
			end
		elseif args[i] == "V/Line" or vlines[args[i]] then
			if not keyset["7f0d82"] then
				colours[j] = "7f0d82"
				keyset["7f0d82"] = true
				j = j + 1
			end
		else
			error("Line \"" .. args[i] .. "\" does not exist")
		end
		i = i + 1
	end
	
	-- Now generate the format string
	for j = 1, ( #colours - 1 ) do
		table.insert( result, 1, table.concat( {
				"<div style=\"width:",	(100 * j) / ( j + 1 ),
				"%; height:8px; background:#", colours[j], ";\">"
			} ) )
		table.insert( result, "</div>" )
	end
	table.insert( result, 1, table.concat( {
			"<div style=\"width:100%; height:8px; background:#",
			colours[#colours], ";\">"
		} ) )
	table.insert( result, "</div>" )

	return table.concat(result)
end

return p