Module:Sandbox/Sameboat/m1

This is an old revision of this page, as edited by Sameboat (talk | contribs) at 09:23, 4 January 2015. 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 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
		if station.link==nil then
			return station.text
			else link = station.link or code
		end
		text = station.text or code
	else
		link = code .. ' Stop'
		text = code
	end
	result = '[[' .. link .. '|' .. text .. ']]'
	return result
end
 
return p