Module:Sandbox/Sameboat/m1

This is an old revision of this page, as edited by Sameboat (talk | contribs) at 09:15, 4 January 2015 (Created page with 'local getArgs = require('Module:Arguments').getArgs local data = mw.loadData('Module:HK-MTR stations/data') local p = {} local function makeInvokeFunction(fu...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
local getArgs = require('Module:Arguments').getArgs
local data = mw.loadData('Module:HK-MTR stations/data')
 
local p = {}
 
local function makeInvokeFunction(funcName)
	-- makes a function that can be returned from #invoke, using
	-- [[Module:Arguments]].
	return function (frame)
		local args = getArgs(frame, {parentOnly = true})
		return p[funcName](args)
	end
end
 
p.station = makeInvokeFunction('_station')
 
local function errorText(message)
	return '<strong class="error">' .. message .. '</strong>'
end
 
local function getStation(code)
	local station = data.stations[code]
	if not station then
		local alias = data.aliases[code]
		if alias then
			code = alias
			station = data.stations[code]
		end
	end
	-- The returned code is what should be used (target of any alias).
	return station, code
end
 
function p._station(args)
	local code = args[1] or args.station  -- arguments have been trimmed
	if code == nil or code == '' then
		return errorText('Need station or stop name')
	end
	local station, code = getStation(code)
	local link, text, result
	if station then
		link = station.link or code
		text = station.text or code
	elseif not link then
		result = text
	else
		link = code .. ' Stop'
		text = code
	end
	result = '[[' .. link .. '|' .. text .. ']]'
	return result
end
 
p.id = makeInvokeFunction('_id')
 
function p._id(args)
	local code = args[1] or args.enname  -- arguments have been trimmed
	if code == nil or code == '' then
		return errorText('Need station or stop name')
	end
	local station, code = getStation(code)
	if station then
		return station.id
	else
		return ''
	end
end
 
p.style = makeInvokeFunction('_style')
 
function p._style(args)
	local code = args[1] or ''
	if code == '' then
		return errorText('Need station or stop name')
	end
	local mtr_data = mw.loadData('Module:MTR/data')
	local station, code = getStation(code)
	local base_style, style, thbgcolor, thcolor, thgradient, result
	if not station or station.lr then
		thbgcolor = mtr_data.colors.LR
		thcolor = '000'
	else
		style = station.style
		thbgcolor = station.thbgcolor or '909090'
		thcolor = station.thcolor or '000'
		thgradient = station.thgradient
	end
	base_style = 'font-size:120%;font-weight:bolder;'
	if style then
		result = base_style .. style .. ';'
	elseif thgradient == nil then
		result = base_style .. 'background:#' .. thbgcolor .. ';color:#' .. thcolor .. ';'
	else
		result = base_style .. 'background:#' .. thbgcolor .. ';' .. thgradient .. ';color:#' .. thcolor .. ';'
	end
	return result
end
 
p.style = makeInvokeFunction('_name_format')
 
function p._style(args)
	local code = args[1] or ''
	if code == '' then
		return errorText('Need station or stop name')
	end
	local mtr_data = mw.loadData('Module:MTR/data')
	local station, code = getStation(code)
	local base_style, style, thbgcolor, thcolor, thgradient, result
	if not station or station.lr then
		thbgcolor = mtr_data.colors.LR
		thcolor = '000'
	else
		style = station.style
		thbgcolor = station.thbgcolor or '909090'
		thcolor = station.thcolor or '000'
		thgradient = station.thgradient
	end
	base_style = 'font-size:120%;font-weight:bolder;'
	if style then
		result = base_style .. style .. ';'
	elseif thgradient == nil then
		result = base_style .. 'background:#' .. thbgcolor .. ';color:#' .. thcolor .. ';'
	else
		result = base_style .. 'background:#' .. thbgcolor .. ';' .. thgradient .. ';color:#' .. thcolor .. ';'
	end
	return result
end
 
p.style = makeInvokeFunction('_thbgcolor')
 
function p._style(args)
	local code = args[1] or ''
	if code == '' then
		return errorText('Need station or stop name')
	end
	local mtr_data = mw.loadData('Module:MTR/data') -- maybe unneeded?
	local station, code = getStation(code) -- maybe unneeded?
	local base_style, style, thbgcolor, thcolor, thgradient, result -- maybe some unneeded?
	if not station or station.lr then
		thbgcolor = mtr_data.colors.LR
	else
		thbgcolor = station.thbgcolor or '909090'
	end
	return thbgcolor
end
 
p.style = makeInvokeFunction('_thcolor')
 
function p._style(args)
	local code = args[1] or ''
	if code == '' then
		return errorText('Need station or stop name')
	end
	local mtr_data = mw.loadData('Module:MTR/data') -- maybe unneeded?
	local station, code = getStation(code) -- maybe unneeded?
	local base_style, style, thbgcolor, thcolor, thgradient, result -- maybe some unneeded?
	if not station or station.lr then
		thcolor = '000'
	else
		thcolor = station.thcolor or '000'
	end
	return thcolor
end
 
p.style = makeInvokeFunction('_thgradient')
 
function p._style(args)
	local code = args[1] or ''
	if code == '' then
		return errorText('Need station or stop name')
	end
	local mtr_data = mw.loadData('Module:MTR/data') -- maybe unneeded?
	local station, code = getStation(code) -- maybe unneeded?
	local base_style, style, thbgcolor, thcolor, thgradient, result -- maybe some unneeded?
	if not station or station.lr then
		thgradient = ''
	else
		thgradient = station.thgradient or ''
	end
	return thgradient
end
 
return p