模組:KL bus

维基百科,自由的百科全书
文档图示 模块文档[创建]
-- placeholder for subpage, will be written later
-- rewrite {{九龍新界巴士簡單列表}} etc. into module to reduce load
local getArgs = require('Module:Arguments').getArgs
local alldata = {
	route = require('Module:KL bus/route')
}
local comp = {
	rapidkl = "吉隆坡快捷通"; gokl = "GO吉隆坡城市巴士";
	ss = "雪兰莪精明巴士"; pj = "八打灵再也市政厅";
	np = "布城脉搏"; so = "Selangor Omnibus";
	mara = "MARA Liner"; ws = "Wawasan Sutera";
}
local p = {}

local dir = {'↺','→','⇄','→<br/>←','↺<br/>←','→<br/>↺'}

function split(inputstr, sep)
        if sep == nil then
                sep = "%s"
        end
        local t={}
        for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
                table.insert(t, str)
        end
        return t
end

function p.main(frame)
	local args = getArgs(frame)
	return p._main(frame, args)
end

function p._main(frame, args)
	local grp = ''
	if args.l == "route" or args.l == "吉隆坡" or args.l == "巴生谷" then grp = "route"
	end
	local data = alldata[grp]
	local out = ""
	-- Main module code goes here.
	-- return args
	if args[1] == nil or args[1] == "" then
		return '\n|-\n| colspan=4 |  沒有任何巴士路線'
	end
	for i = 1, 100, 1 do
		if not(args[i]) then break end
		local special = string.find('' .. args[i], "_s")
		local route = split(args[i], "_s")[1]
		local spi = split(args[i], "_s")[2] or ''
		out = out .. '\n|-\n| style="text-align: right; padding-right: 10px" |'
		if data[route] then
			out = out .. frame:preprocess( data[route]['klbc'] )
			out = out .. '\n| style="text-align: right; white-space: nowrap" | '
			if special then
				local dirstr = ''
				if data[route]['special' .. spi .. '_direction'] then
					dirstr = dir[data[route]['special' .. spi .. '_direction']+1] end
				out = out .. frame:preprocess( data[route]['special' .. spi .. '_endpoint1'] )
						  .. '\n| style="text-align: center; padding: 0px 8px 0px 8px" |'
						  .. dirstr
						  .. '\n| style="white-space: nowrap" |'
						  .. frame:preprocess( data[route]['special' .. spi .. '_endpoint2'] )
						  .. '\n| style="padding-left: 10px" |'
						  .. frame:preprocess( data[route]['special' .. spi .. '_status'] )
			else
				local dirstr = ''
				if data[route]['direction'] then
					dirstr = dir[data[route]['direction']+1] end
				out = out .. frame:preprocess( data[route]['endpoint1'] )
						  .. '\n| style="text-align: center; padding: 0px 8px 0px 8px" |'
						  .. dirstr
						  .. '\n| style="white-space: nowrap" |'
						  .. frame:preprocess( data[route]['endpoint2'] )
						  .. '\n| style="padding-left: 10px" |'
						  .. frame:preprocess( data[route]['status'] )
			end
		else
			out = out .. '\n|' .. route .. '|| colspan="3" style="text-align: center" | (沒有資料,請加入至[[Module:KL bus/' .. grp .. ']]。)'
		end
	end
	return out
end

p.list = p.main

function p.tbl(frame)
	local args = getArgs(frame)
	local grp = ''
	if args.l == "route" or args.l == "吉隆坡" or args.l == "巴生谷" then grp = "route"
	end
	local data = alldata[grp]
	local out = ""
	-- Main module code goes here.
	-- return args
	if args[1] == nil or args[1] == "" then
		return '\n|-\n| colspan=3 | 沒有任何巴士路線'
	end
	for i = 1, 100, 1 do
		if not(args[i]) then break end
		local special = string.find('' .. args[i], "_s")
		local route = split(args[i], "_s")[1]
		local spi = split(args[i], "_s")[2] or ''
		out = out .. '\n|-\n| style="text-align: left" |'
		if data[route] then
			out = out .. frame:preprocess( data[route]['klbc'] )
			out = out .. '||'
			if special then
				out = out .. data[route]['special' .. spi .. '_normal'] .. "||"
						  .. frame:preprocess( data[route]['special' .. spi .. '_status'] )
			else
				out = out .. data[route]['normal'] .. "||"
						  .. frame:preprocess( data[route]['status'] )
			end
		else
			out = out .. '\n|' .. route .. '|| colspan="2" style="text-align: center" | (沒有資料,請加入至[[Module:KL bus/' .. grp .. ']]。)'
		end
	end
	return out
end

function p.data(frame)
	local args = getArgs(frame)
	local grp = ''
	if args.l == "route" or args.l == "吉隆坡" or args.l == "巴生谷" then grp = "route"
	end
	local data = alldata[grp]
	local out = ""
	
	local route = args[1]
	local prop = args[2]
	-- Main module code goes here.
	-- return args
	if route == nil or route == "" or prop == nil or prop == "" then
		return '沒有填寫巴士路線參數'
	end
	
	local out = ''
	if data[route] then
		if data[route][prop] then out = data[route][prop]
		else out = '未找到該路綫的' .. prop .. '資料,請複查[[Module:KL bus/' .. grp .. ']]。' end
	else out = '未有資料,請加入至[[Module:KL bus/' .. grp .. ']]。' end
	return out
end

return p