模組解[]
-- This module implements {{HK-MTR icon}}, {{HK-MTR color}}, {{HK-MTR lines}} and {{HK-MTR box}}.
 
local getArgs = require('Module:Arguments').getArgs
local data = mw.loadData('Module:MTR/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
 
local function getColor(code)
	return data.colors[code] or '000'
end

local function getTextcolor(code)
	return data.textcolors[code] or '000'
end
 
p.colorbyname = makeInvokeFunction('_colorbyname')
 
function p._colorbyname(args)
	local code = args[1] or ''
	code = code:upper()
	return getColor(code)
end
 
local function getLink(code)
	local name = data.names[code]
	link = name
	if data.needs_dab[name] then
		link = link .. ' (MTR)'
	elseif data.lr[name] then
		link = 'MTR Light Rail Route ' .. link
	end
	return link
end
 
local function invalid(code)
	return '<strong class="error">Error: "' .. tostring(code) .. '" is not a valid code.</strong>'
end
 
p.link = makeInvokeFunction('_link')
 
function p._link(args)
	-- Retrieve arguments
	local code = args[1] or ''
	code = code:upper() -- Convert to upper case
	if not code then
		return invalid(code)
	end
	return getLink(code)
end
 
p.icon = makeInvokeFunction('_icon')
 
function p._icon(args)
	local code = args[1] or ''
	local link = args[2] or args.link
	local text = args[3] or args.text
	code = code:upper()
	local color = getColor(code)
	link = link or getLink(code)
	local name = data.names[code]
	if not name then
		return invalid(code)
	end
 
	-- Format the text
	if text == noint then
		text = name
	else
		text = text or 'Interchange station for the ' .. name
 	end
	-- Build the link
	local lr = data.lr[code]
	if lr then
		local textcolor = getTextcolor(code)
		return '<span style="background-color:#' .. color .. '; border:1px solid #' .. color .. ';">&nbsp;[['.. link ..'|<span style="color:#' .. textcolor .. '; font-weight:bold; font-size:inherit; white-space:nowrap;">' .. name .. '</span>]]&nbsp;</span>'
	else
		return '[[' .. link .. '|<span title="' .. text .. '" style="background-color:#' .. color .. '; border:1px solid #000; text-align:center;">&nbsp;&nbsp;&nbsp;&nbsp;</span>]]'
	end
end
 
p.line = makeInvokeFunction('_line')
 
function p._line(args)
	local code = args[1] or ''
	local link = args[2] or args.link
	local text = args[3] or args.text
	code = code:upper()
	link = link or getLink(code)
	local name = data.names[code]
	if not name then
		return invalid(code)
	end
	text = text or name
	return '[[' .. link .. '|' .. text .. ']]'
end
 
p.box = makeInvokeFunction('_box')
 
function p._box(args)
	local code = args[1] or ''
	local link = args[2] or args.link
	local text = args[3] or args.text
	code = code:upper()
	local color = getColor(code)
	link = link or getLink(code)
	local name = data.names[code]
	if not name then
		return invalid(code)
	end
	text = text or name
 
	return '<span title="' .. text .. '" style="background-color:#' .. color .. '; border:1px solid #000; text-align:center;">&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;[[' .. link .. '|' .. text .. ']]'
end


p.box1 = makeInvokeFunction('_box1')
 
function p._box1(args)
	local code = args[1] or ''
	local link = args[2] or args.link
	local text = args[3] or args.text
	code = code:upper()
	local color = getColor(code)
	link = link or getLink(code)
	local name = data.names[code]
	if not name then
		return invalid(code)
	end
	text = text or name

	return '<span style="display:inline-block; width:1.5em; height:1.5em; margin:1px 0; border:1px solid #000; background-color:#' .. color .. '; text-align:center;">&nbsp;</span>&nbsp;[[' .. link .. '|' .. text .. ']]'
end
 
return p