模組:沙盒/TimWu007/CNBUStest

维基百科,自由的百科全书
文档图示 模块文档[创建]
-- 声明模块本体
local p = {}

-- 导入城市数据
local function _loadSystemData(city)
	if city ~= nil and city ~= "" then
		local state
		state, ro_system_data = pcall(mw.loadData, "Module:沙盒/TimWu007/CNBUStest/"..city)
		if not state then
			error(string.format('公交系统“%s”的数据模块不存在', city))
		end
		system_data = setmetatable( {}, { __index = ro_system_data });
		system_data.locName = {}
		for i1, t1 in pairs(system_data.location) do
			for _, k1 in pairs(t1) do
				system_data.locName[k1] = t1[1]
			end
		end
		system_data.compName = {}
    	for i2, t2 in pairs(system_data.company) do
    	    for _, k2 in pairs(t2) do
    	        system_data.compName[k2] = t2[1]
    	    end
    	end
	else
		error(string.format('city参数为空,请输入城市代码'))
	end
	return system_data
end

-- 导入路线数据
local function _loadLineData(sysloc, sys)
    local city_data = nil
    local state
    state, city_data = pcall(mw.loadData, sys.data[sysloc])
	if not state then
		state1, city_data = pcall(mw.loadData, sys.data["default"])
	end
	return city_data
end

-- {{广州巴士公司色块}}
function p._internalColorbox(compname, city)
	local sys = _loadSystemData(city)			-- 加载系统数据
	str = compname
	len = #str
	if sys.compName[compname] then
		return [[width="1%" style="background:]]..sys.colors[sys.compName[compname]]..[["]]
	else
		if len >18 or compname == "multi" then
			return [[width="1%" style="background:]]..sys.colors['multi']..[["]]	-- 公司名超过六个字为联营
		else
    		return [[width="1%" style="background:]]..sys.colors['other']..[["]]
    	end
    end
end

-- 处理起讫点中出现的跨粤文维基百科的链接模板({{tsl|zh-yue|...}})
function p.tsllink(name)
	str = name
	if string.find(name, "tsl") then
		start = string.find(name, "{")
		en3 = string.find(name, "}", start)
		en1 = string.find(name, "|", start+13)
		arg1 = string.sub(name, start+13, en1-1)
		en2 = string.find(name, "|", en1+1)
		arg2 = string.sub(name, en1+1, en2-1)
		arg3 = string.sub(name, en2+1, en3-1) 
		no1 = string.sub(name, 0, start-1)
		fi1 = string.find(name, "{", en2+1)
		if fi1 == nil then			-- 只引用一次tsl
			no2 = string.sub(name, en3+2, string.len(name))
			str = no1..mw.getCurrentFrame():expandTemplate{title = "tsl", args = {"zh-yue", arg1, arg2, arg3}}..no2
		else 						-- 引用两次tsl
			start1 = string.find(name, "{", en3)
			no2 = string.sub(name, en3+2, start1-1)
			en4 = string.find(name, "|", start1+13)
			arg4 = string.sub(name, start1+13, en4-1)
			en5 = string.find(name, "|", en4+1)
			arg5 = string.sub(name, en4+1, en5-1)
			en6 = string.find(name, "}", start1)
			arg6 = string.sub(name, en5+1, en6-1) 
			no3 = string.sub(name, en6+2, string.len(name))
			str = no1..mw.getCurrentFrame():expandTemplate{title = "tsl", args = {"zh-yue", arg1, arg2, arg3}}..no2..mw.getCurrentFrame():expandTemplate{title = "tsl", args = {"zh-yue", arg4, arg5, arg6}}..no3
		end
		return str
	else
		return str
	end
end

-- 生成一条路线的一列表格内容
function p._internalList(no, style, city, loc, time, vehicle, image)
	local sys = _loadSystemData(city)			-- 加载系统数据
	local sysloc = sys.locName[loc]				-- sysloc为拼音
	local loc = sys.titlename[sysloc]			-- loc为简体
	local d = _loadLineData(sysloc, sys)		-- 加载路线数据
	if d[no] == nil then
		a = [[|width="1%"| ||align=center|''']]..no.."'''||colspan=3 align=center|''(错误:[["..sys.data[sysloc]..']]中未包含这条[['..loc.."巴士路线列表|"..loc.."]]的路线)''"
	elseif d[no].note == "已停办" or d[no].note == "已停辦" then
		if d[no].suspend == nil then 
			a = [[|width="1%"| ||align=center|''']]..d[no].code..[['''||colspan=3 align=center|''(本线已停办或暂停服务)'']]
		else
			a = [[|width="1%"| ||align=center|''']]..d[no].code..[['''||colspan=3 align=center|''(本线自]]..d[no].suspend..[[起停办或暂停服务)'']]
		end
	else
		b = p._internalColorbox(d[no].company, city)	-- 生成颜色方块
		e1 = p.tsllink(d[no].endpoint1)			-- 处理起讫点1可能出现的{{tsl|zh-yue}}
		e2 = p.tsllink(d[no].endpoint2) 		-- 处理起讫点2可能出现的{{tsl|zh-yue}}
		note = d[no].note
		-- BRT样式(广州、中山)
		if ((style == "BRT" or style == "brt") and (d[no].brt_west == nil)) then			-- 不是BRT路线
			a = [[|width="1%"| ||align=center|''']]..no.."'''||colspan=7 align=center|''(错误:本线并非[[广州快速公交运输系统#路线|广州BRT路线]])''"
		elseif ((style == "BRT" or style == "brt") and (d[no].brt_west ~= nil)) then		-- BRT样式:早高峰单向路线(一列,广州)
			a = [[|]]..b..[[| ||align=center|''']]..d[no].code..[['''||align=right|]]..e1
			if d[no].brt_east ~= nil then
				brtin = d[no].brt_east_in..'\n----'..d[no].brt_west_in
				brtout = d[no].brt_east_out..'\n----'..d[no].brt_west_out
				brtno = d[no].brt_east..'\n----'..d[no].brt_west
				a = a..'||align=center|→\n----←\n|align=left|'
			else
				brtin = d[no].brt_west_in
				brtout = d[no].brt_west_out
				brtno = d[no].brt_west
				a = a..'||align=center|早<br />←||align=left|'
			end
			a = a..e2..'\n|align=center|'..brtin..'\n|align=center|'..brtout..'\n|align=center|'..brtno..'\n|align=center|'..d[no].company
			if d[no].brt_info ~= nil then note = note..';'..d[no].brt_info end
		-- 非BRT样式
		else
			if d[no].endpoint3 == nil and time == "yes" then -- 发班时间(中山、珠海)
				if d[no].time1 == "" or d[no].time1 == nil then
					t1 = "(暂时未知)"
				else
					t1 = d[no].time1
				end
				if d[no].time2 == "" or d[no].time2 == nil then
					t2 = "(暂时未知)"
				else
					t2 = d[no].time2
				end
				e1 = e1..[[<br />]]..t1
				e2 = e2..[[<br />]]..t2
			end

			a = [[|]]..b..[[| ||align=center|''']]..d[no].code..[['''||align=right|]]..e1..'||align=center|'..d[no].direction
			if d[no].direction1 ~= nil then a = a..'\n----'..d[no].direction1 end
			a = a..'\n|align=left|'..e2
			if d[no].endpoint3 ~= nil then					-- 部分双向高峰快线:去程终点和返程起点不同
				e3 = p.tsllink(d[no].endpoint3)				-- 处理起讫点3可能出现的{{tsl|zh-yue}}
				a = a..'\n----'..e3
			end

			if (style == "noco" or style == "nocotr") then	-- 不显示公司栏(noco、nocotr)
				a = a..'\n|align=center|'..d[no].fare
			elseif style == "nofa" then						-- 不显示票价栏(nofa)
				a = a..'\n|align=center|'..d[no].company
			elseif style == "nocofa" then					-- 不显示公司和票价栏(nocofa)
				a = a
			else											-- 显示公司和票价栏(notr、nodo、默认)
				a = a..'\n|align=center|'..d[no].fare..'\n|align=center|'..d[no].company
			end

			if vehicle == "yes" then						-- 配车信息(惠州)
				if d[no].vehicle == "" then
					ve = "(暂时未知)"
				else
					ve = d[no].vehicle
				end
				a = a..'\n|align=center|'..ve
			end
															-- 无轨、双层巴士信息(广州)
			if (style == "notr" or style == "nocotr") then 	-- 不显示无轨电车信息(notr、nocotr)
				if d[no].doubledecker==1 then 
					if note ~= "" then 
						note = note..[[;双层巴士路线]] 
					else 
						note = note..[[双层巴士路线]]
					end
				end
			elseif style == "nodo" then						-- 不显示双层巴士信息(nodo)
				if d[no].trolleybus==1 then
					if note ~= "" then
						note = note..[[;无轨电车路线]] 
					else
						note = note..[[无轨电车路线]]
					end
				end
			else 											-- 都显示(noco、nofa、nocofa、无)
				if d[no].doubledecker==1 then 
					if note ~= "" then
						note = note..[[;双层巴士路线]] 
					else
						note = note..[[双层巴士路线]]
					end
					if d[no].trolleybus==1 then note = note..[[;无轨电车路线]] end
				elseif d[no].trolleybus==1 then
					if note ~= "" then
						note = note..[[;无轨电车路线]] 
					else
						note = note..[[无轨电车路线]]
					end
				end
			end
		end
		if image ==  "yes" and d[no].image then note = note..d[no].image end
		a = a..'\n|'..d[no].note
	end
	return a
end

-- 列表模板
function p.list(frame)
	local bb = frame.args
	local ret = p._internalList(bb.code, bb.format, bb.city, bb.loc, bb.time, bb.vehicle, bb.image)
	return ret
end

-- 生成一条路线的一列简单表格内容
function p._internalSimpList(no, city, loc)
	local sys = _loadSystemData(city)			-- 加载系统数据
	local sysloc = sys.locName[loc]				-- sysloc为拼音
	local loc = sys.titlename[sysloc]			-- loc为简体
	local d = _loadLineData(sysloc)				-- 加载路线数据
	if d[no] == nil then
		a = [[|align=center|''']]..no.."'''||colspan=3 align=center|''(错误:[["..sys.data[sysloc]..']]中未包含这条[['..loc.."巴士路线列表|"..loc.."]]的路线)''"
	elseif d[no].note == "已停办" or d[no].note == "已停辦" then
		if d[no].suspend == nil then 
			a = [[|align=center|''']]..d[no].code..[['''||colspan=3 align=center|''(错误:本线已停办或暂停服务)'']]
		else
			a = [[|align=center|''']]..d[no].code..[['''||colspan=3 align=center|''(错误:本线自]]..d[no].suspend..[[起停办或暂停服务,请移除)'']]
		end
	else
		e1 = p.tsllink(d[no].endpoint1)						-- 处理起讫点1可能出现的{{tsl|zh-yue}}
		e2 = p.tsllink(d[no].endpoint2) 					-- 处理起讫点2可能出现的{{tsl|zh-yue}}
		a = [[|align=center|''']]..d[no].code..[['''||align=right|]]..e1..'||align=center|'..d[no].direction
		if d[no].direction1 ~= nil then a = a..'\n----\n'..d[no].direction1 end
		a = a..'\n|align=left|'..e2
		if d[no].endpoint3 ~= nil then					-- 部分双向高峰快线:去程终点和返程起点不同
			e3 = p.tsllink(d[no].endpoint3)				-- 处理起讫点3可能出现的{{tsl|zh-yue}}
			a = a..'\n----\n'..e3
		end
		a = a..'\n|'
	end
	return a
end

-- 简单列表模板
function p.simplelist(frame)
	local ss = frame.args
	local ret = p._internalSimpList(ss.code, ss.city, ss.loc)
	return ret
end

-- 生成表格表头
function p._internalTitle(style, city, loc)
	local sys = _loadSystemData(city)			-- 加载系统数据
	local sysloc = sys.locName[loc]				-- sysloc为拼音
	local loc = sys.titlename[sysloc]			-- loc为简体

	if (style == "BRT" or style == "brt") then
		t = '!colspan="2" width="8.5%"|[[广州巴士路线列表|编号]]!!width="25%" colspan="3"|路线!!width="10.6%"|驶入BRT通道!!width="10.6%"|驶出BRT通道!!width="8.5%"|BRT通道停靠站数!!width="14.9%"|运营商!!width="21.3%"|备注'
	elseif vehicle == "yes" then								-- 显示运力(vehicle = yes)
		if (style == "noco" or style == "nocotr") then			-- 不显示公司栏(noco、nocotr)
			t = '!colspan="2" width="12%"|[['..loc..'巴士路线列表|编号]]!!colspan="3"|路线!!width="8%"|收费!!width="19%"|运力!!width="19%"|备注\n|-style="background:#EAECF0" height=0\n|colspan="2"| ||width="20%"| || ||width="20%"| ||colspan="3"| '
		elseif style == "nofa" then								-- 不显示票价栏(nofa)
			t = '!colspan="2" width="12%"|[['..loc..'巴士路线列表|编号]]!!colspan="3"|路线!!width="8%"|运营商!!width="19%"|运力!!width="19%"|备注\n|-style="background:#EAECF0" height=0\n|colspan="2"| ||width="20%"| || ||width="20%"| ||colspan="3"| '
		elseif style == "nocofa" then							-- 不显示公司和票价栏(nocofa)
			t = '!colspan="2" width="13%"|[['..loc..'巴士路线列表|编号]]!!colspan="3"|路线!!width="21%"|运力!!width="20%"|备注\n|-style="background:#EAECF0" height=0\n|colspan="2"| ||width="22%"| || ||width="22%"| ||colspan="2"| '
		else													-- 显示公司和票价栏(notr、nodo、默认)
			t = '!colspan="2" width="10%"|[['..loc..'巴士路线列表|编号]]!!colspan="3"|路线!!width="8%"|收费!!width="8%"|运营商!!width="18%"|运力!!width="18%"|备注\n|-style="background:#EAECF0" height=0\n|colspan="2"| ||width="18%"| || ||width="18%"| ||colspan="4"| '
		end
	else														-- 不显示运力
		if (style == "noco" or style == "nocotr") then	-- 不显示公司栏(noco、nocotr)
			t = '!colspan="2" width="14%"|[['..loc..'巴士路线列表|编号]]!!colspan="3"|路线!!width="10%"|收费!!width="22%"|备注\n|-style="background:#EAECF0" height=0\n|colspan="2"| ||width="25%"| || ||width="25%"| ||colspan="2"| '
		elseif style == "nofa" then						-- 不显示票价栏(nofa)
			t = '!colspan="2" width="14%"|[['..loc..'巴士路线列表|编号]]!!colspan="3"|路线!!width="11%"|运营商!!width="21%"|备注\n|-style="background:#EAECF0" height=0\n|colspan="2"| ||width="25%"| || ||width="25%"| ||colspan="2"| '
		elseif style == "nocofa" then					-- 不显示公司和票价栏(nocofa)
			t = '!colspan="2" width="16%"|[['..loc..'巴士路线列表|编号]]!!colspan="3"|路线!!width="24%"|备注\n|-style="background:#EAECF0" height=0\n|colspan="2"| ||width="28%"| || ||width="28%"| || '
		else											-- 显示公司和票价栏(notr、nodo、默认)
			t = '!colspan="2" width="12%"|[['..loc..'巴士路线列表|编号]]!!colspan="3"|路线!!width="8%"|收费!!width="10%"|运营商!!width="20%"|备注\n|-style="background:#EAECF0" height=0\n|colspan="2"| ||width="23%"| || ||width="23%"| ||colspan="3"| '
		end
	end
	return t
end

-- Title模板
function p.title(frame)
	local tt = frame.args
	local ret = p._internalTitle(tt.format, tt.city, tt.loc)
	return ret
end

-- 色块模板
function p.colorbox(frame)
	local cc = frame.args
	local ret = p._internalColorbox(cc.company, cc.city)
	return ret
end

return p