跳转到内容

模組:CNBUS/sandbox

本页使用了标题或全文手工转换
维基百科,自由的百科全书

这是本页的一个历史版本,由TimWu007留言 | 贡献2021年12月15日 (三) 14:04编辑。这可能和当前版本存在着巨大的差异。

-- 声明模块本体
local p = {
	NO_DATA = '(暂缺)',
	DEFAULT_SPACE = ' ',
}

-- 导入城市数据
local function _loadSystemData(city)
	if city ~= nil and city ~= "" then
		local state
		state, ro_system_data = pcall(mw.loadData, "Module:CNBUS/"..city)
		if not state then
			error(string.format('[[Module:CNBUS]]不存在“%s”的公交系统数据', city))
		end
		system_data = setmetatable( {}, { __index = ro_system_data });	-- 转换为可读写的table
		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 _isEmpty(var)
	return not var or var:match( '^%s*(.-)%s*$' ) == ''
end

-- 导入路线数据
local function _loadLineData(sysloc, path)
	local city_data = nil
	local state
	state, city_data = pcall(mw.loadData, path[sysloc])
	if not state then
		error(string.format('该城市系统不存在“%s”的资料模块'), sysloc)
	end
	return city_data
end

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

-- 处理起讫点中出现的跨粤文维基百科的链接模板({{tsl|zh-yue|...}})
function p._internalTsllink(name)
	if _isEmpty(name) then
		str = p.DEFAULT_SPACE
	else
		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 = "ly", args = {arg2, arg1, 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 = "ly", args = {arg2, arg1, arg3}}..no2..mw.getCurrentFrame():expandTemplate{title = "ly", args = {arg5, arg4, arg6}}..no3
			end
		end
	end
	return str
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 path = sys.data
	local d = _loadLineData(sysloc, path)		-- 加载路线数据
	if _isEmpty(d[no]) then
		table_output = mw.ustring.format([["|width="1%"| ||align=center|'''%s'''||colspan=3 align=center|''(错误:%s中未包含这条[\[%s巴士路线列表|%s]\]的路线)'']], no, sys.data[sysloc], loc, loc)
	elseif mw.ustring.match(d[no].note, "已停[办辦]") then
		if _isEmpty(d[no].suspend) then 
			table_output = mw.ustring.format([["|width="1%"| ||align=center|'''%s'''||colspan=3 align=center|''(本线已停办,请移除)'']], d[no].code)
		else
			table_output = mw.ustring.format([["|width="1%"| ||align=center|'''%s'''||colspan=3 align=center|''(本线自%s起停办,请移除)'']], d[no].code, d[no].suspend)
		end
	elseif mw.ustring.match(d[no].note, "[暂暫]停服[务務]") then
		if _isEmpty(d[no].suspend) then 
			table_output = mw.ustring.format([["|width="1%"| ||align=center|'''%s'''||colspan=3 align=center|''(本线已暂停服务)'']], d[no].code)
		else
			table_output = mw.ustring.format([["|width="1%"| ||align=center|'''%s'''||colspan=3 align=center|''(本线自%s起暂停服务)'']], d[no].code, d[no].suspend)
		end
	else
		data_colorbox = p._internalColorbox(d[no].company, city)	-- 生成颜色方块
		data_ep1 = p._internalTsllink(d[no].endpoint1)			-- 处理起讫点1可能出现的{{tsl|zh-yue}}
        if _isEmpty(d[no].endpoint2) then
		    data_ep2 = p._internalTsllink(d[no].endpoint1) 		-- 处理起讫点2可能出现的{{tsl|zh-yue}}
        else
            data_ep2 = p._internalTsllink(d[no].endpoint2)
        end
		
		if _isEmpty(d[no].endpoint3) then					-- 部分双向高峰快线:去程终点和返程起点不同
			data_ep3 = p._internalTsllink(d[no].endpoint3) 		-- 处理起讫点3可能出现的{{tsl|zh-yue}}
		else
			data_ep3 = false
		end
		if _isEmpty(d[no].direction) then
            if _isEmpty(d[no].endpoint2) then
			    data_direction = ""
            else
			    data_direction = ""
            end
		else
			data_direction = d[no].direction
		end
		if _isEmpty(d[no].fare) then
			data_fare = ""
		else
			data_fare = d[no].fare
		end
		if _isEmpty(d[no].company) then
			data_company = p.NO_DATA
		else
			data_company = d[no].company
		end
		if d[no].note then
			data_note = d[no].note
		else
			data_note = ""
		end

		-- BRT样式(广州、中山)
		if mw.ustring.match(style, "[Bb][Rr][Tt]") and _isEmpty(d[no].brt_b) then			-- 不是BRT路线
			table_output = mw.ustring.format([[|width="1%"| ||align=center|'''%s'''||colspan=7 align=center|''(错误:本线并非[\[快速公交系统|BRT路线]\])'']], d[no].code)
		elseif mw.ustring.match(style, "[Bb][Rr][Tt]") and not _isEmpty(d[no].brt_b) then		-- BRT样式:单向路线(一列,广州)
			if time == "yes" then
				if not _isEmpty(d[no].time1) then data_ep1 = data_ep1..[[<br>]]..d[no].time1 end
				if not _isEmpty(d[no].time2) then data_ep2 = data_ep2..[[<br>]]..d[no].time2 end
				if not _isEmpty(d[no].time3) and data_ep3 then data_ep3 = data_ep3..[[<br>]]..d[no].time3 end
			end
			
			table_output = [[|]]..data_colorbox..[[| ||''']]..d[no].code..[['''||align=right|]]..data_ep1
			data_brt_b_in = p._internalTsllink(d[no].brt_b_in)
			data_brt_b_out = p._internalTsllink(d[no].brt_b_out)
			if not _isEmpty(d[no].brt_a) then
				data_brt_a_in = p._internalTsllink(d[no].brt_a_in)
				data_brt_a_out = p._internalTsllink(d[no].brt_a_out)
				data_brtin = data_brt_a_in..'\n----'..data_brt_b_in
				data_brtout = data_brt_a_out..'\n----'..data_brt_b_out
				data_brtno = d[no].brt_a..'\n----'..d[no].brt_b
				table_output = table_output..'||→\n----←\n|align=left|'
			else
				data_brtin = data_brt_b_in
				data_brtout = data_brt_b_out
				data_brtno = d[no].brt_b
				table_output = table_output..'||'..data_direction..'||align=left|'
			end
			table_output = table_output..data_ep2..'||'..data_brtin..'\n|'..data_brtout..'\n|'..data_brtno..'\n|'..data_company
			if not _isEmpty(d[no].brt_info) then
				if data_note ~= "" then
					data_note = data_note..[[;]]..d[no].brt_info
				else
					data_note = data_note..d[no].brt_info
				end
			end
		-- 非BRT样式
		else
			if time == "yes" then 			-- 发班时间
				if not _isEmpty(d[no].time1) then data_ep1 = data_ep1..[[<br>]]..d[no].time1 end
				if not _isEmpty(d[no].time2) then data_ep2 = data_ep2..[[<br>]]..d[no].time2 end
				if not _isEmpty(d[no].time3) and data_ep3 then data_ep3 = data_ep3..[[<br>]]..d[no].time3 end
			end

			table_output = [[|]]..data_colorbox..[[| ||''']]..d[no].code..[['''||align=right|]]..data_ep1..'||nowrap=true|'..data_direction
			if _isEmpty(d[no].direction1) then
				if data_ep3 then
					table_output = table_output..'\n----'..d[no].direction1
				else 
					table_output = table_output..'<br>'..d[no].direction1
				end
			end
			table_output = table_output..'\n|align=left|'..data_ep2
			-- 部分双向高峰快线:去程终点和返程起点不同
			if data_ep3 then table_output = table_output..'\n----'..data_ep3 end

			if style == "noco" then							-- 不显示公司栏(noco)
				table_output = table_output..'\n|'..data_fare
			elseif style == "nofa" then						-- 不显示票价栏(nofa)
				table_output = table_output..'\n|'..data_company
			elseif style == "nocofa" then					-- 不显示公司和票价栏(nocofa)
				table_output = table_output
			else											-- 显示公司和票价栏(默认)
				table_output = table_output..'\n|'..d[no].fare..'\n|'..data_company
			end

			if vehicle == "yes" then						-- 配车信息(惠州)
				if _isEmpty(d[no].vehicle) then
					data_vehicle = p.NO_DATA
				else
					data_vehicle = d[no].vehicle
				end
				table_output = table_output..'\n|'..data_vehicle
			end
		end
		if image == "yes" and not _isEmpty(d[no].image) then
			if not _isEmpty(d[no].note) then
				data_note = data_note..'<br>'..d[no].image
			else
				data_note = d[no].image
			end
		end
		table_output = table_output..'\n|align=left|'..data_note
	end
	return table_output
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 path = sys.data
	local d = _loadLineData(sysloc, path)		-- 加载路线数据
	if _isEmpty(d[no]) then
		table_output = mw.ustring.format([[|align=center|'''%s'''||colspan=3 align=center|''(错误:%s中未包含这条[\[%s巴士路线列表|%s]\]的路线)'']], no, sys.data[sysloc], loc, loc)
	elseif mw.ustring.match(d[no].note, "已停[办辦]") then
		if _isEmpty(d[no].suspend) then 
			table_output = mw.ustring.format([[|align=center|'''%s'''||colspan=3 align=center|''(本线已停办,请移除)'']], d[no].code)
		else
			table_output = mw.ustring.format([[|align=center|'''%s'''||colspan=3 align=center|''(本线自%s起停办,请移除)'']], d[no].code, d[no].suspend)
		end
	elseif mw.ustring.match(d[no].note, "[暂暫]停服[务務]") then
		if _isEmpty(d[no].suspend) then 
			table_output = mw.ustring.format([[|align=center|'''%s'''||colspan=3 align=center|''(本线已暂停服务)'']], d[no].code)
		else
			table_output = mw.ustring.format([[|align=center|'''%s'''||colspan=3 align=center|''(本线自%s起暂停服务)'']], d[no].code, d[no].suspend)
		end
	else
		data_ep1 = p._internalTsllink(d[no].endpoint1)						-- 处理起讫点1可能出现的{{tsl|zh-yue}}
		data_ep2 = p._internalTsllink(d[no].endpoint2) 					-- 处理起讫点2可能出现的{{tsl|zh-yue}}
		if _isEmpty(d[no].direction) then
			data_direction = ""
		else
			data_direction = d[no].direction
		end
		table_output = [[|align=right|''']]..d[no].code..[['''||align=right|]]..data_ep1..'||align=center nowrap=true"|'..data_direction
		if not _isEmpty(d[no].direction1) then
			if data_ep3 then
				table_output = table_output..'\n----'..d[no].direction1
			else 
				table_output = table_output..'<br>'..d[no].direction1
			end
		end
		table_output = table_output..'\n|align=left|'..data_ep2
		if not _isEmpty(d[no].endpoint3) then					-- 部分双向高峰快线:去程终点和返程起点不同
			data_ep3 = p._internalTsllink(d[no].endpoint3)				-- 处理起讫点3可能出现的{{tsl|zh-yue}}
			table_output = table_output..'\n----'..data_ep3
		end
		if d[no].note ~= nil then
			data_note = d[no].note
		else
			data_note = ""
		end
		table_output = table_output..'\n|align=left|'..data_note
	end
	return table_output
end

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

-- 输出线路单项资料(惠州)
function p._internalCode(no, city, loc)
	local sys = _loadSystemData(city)			-- 加载系统数据
	local sysloc = sys.locName[loc]				-- sysloc为拼音
	local loc = sys.titlename[sysloc]			-- loc为简体
	local path = sys.data
	local d = _loadLineData(sysloc, path)		-- 加载路线数据
	if _isEmpty(d[no]) then
		table_output = string.format("(错误:%s中无%s线)", sys.data[sysloc], no)
	elseif mw.ustring.match(d[no].note, "已停[办辦]") then
		table_output = string.format("(错误:%s已停办或暂停服务,请移除)", d[no].code)
	else
		table_output = d[no].code
	end
	return table_output
end

-- 极简列表模板(惠州)
function p.code(frame)
	local ss = frame.args
	local ret = p._internalCode(ss.code, ss.city, ss.loc)
	return ret
end

-- 生成表格表头
function p._internalTitle(style, city, loc, vehicle, time, image)
	local preloc = loc
	local sys = _loadSystemData(city)			-- 加载系统数据
	local sysloc = sys.locName[loc]				-- sysloc为拼音
	local loc = sys.titlename[sysloc]			-- loc为简体
	if loc == nil then loc = preloc end
	local link_list
	if city == "ZZ" or city == "SU" or city == "BJ" or city == "CW" or city == "LYG" then
		link_list = loc..'公交线路列表'
	elseif city == "CQ" then
		link_list = '重庆公交线路列表 ('..loc..')'
	elseif city == "XA" then
		link_list = loc..'市公交线路表'
	elseif city == "ST" or city == "CZ" or city == "JY" then
		link_list = loc..'公交路线列表'
	elseif loc == "惠州" then
		link_list = '惠州公共交通线路表'
	else
		link_list = loc..'巴士路线列表'
	end

	if time == "yes" then 
		title_route = "路线及运营时间"
	else
		title_route = "路线"
	end

	if image == "yes" then
		title_note = "图片及备注"
	else
		title_note = "备注"
	end

	if (style == "BRT" or style == "brt") then
		t = '!colspan="2" width="8.5%"|[['..link_list..'|编号]]!!width="25%" colspan="3"|'..title_route..'!!width="10.6%"|驶入BRT通道!!width="10.6%"|驶出BRT通道!!width="8.5%"|BRT通道停靠站数!!width="14.9%"|运营商!!width="21.3%"|'..title_note
	elseif vehicle == "yes" then								-- 显示运力(vehicle = yes)
		if style == "noco" then			-- 不显示公司栏(noco)
			t = '!colspan="2" width="12%"|[['..link_list..'|编号]]!!colspan="3"|'..title_route..'!!width="8%"|收费!!width="19%"|运力!!width="19%"|'..title_note..'\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%"|[['..link_list..'|编号]]!!colspan="3"|'..title_route..'!!width="8%"|运营商!!width="19%"|运力!!width="19%"|'..title_note..'\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%"|[['..link_list..'|编号]]!!colspan="3"|'..title_route..'!!width="21%"|运力!!width="20%"|'..title_note..'\n|-style="background:#EAECF0" height=0\n|colspan="2"| ||width="22%"| || ||width="22%"| ||colspan="2"| '
		else													-- 显示公司和票价栏(默认)
			t = '!colspan="2" width="10%"|[['..link_list..'|编号]]!!colspan="3"|'..title_route..'!!width="8%"|收费!!width="8%"|运营商!!width="18%"|运力!!width="18%"|'..title_note..'\n|-style="background:#EAECF0" height=0\n|colspan="2"| ||width="18%"| || ||width="18%"| ||colspan="4"| '
		end
	else														-- 不显示运力
		if style == "noco" then			-- 不显示公司栏(noco)
			t = '!colspan="2" width="14%"|[['..link_list..'|编号]]!!colspan="3"|'..title_route..'!!width="10%"|收费!!width="22%"|'..title_note..'\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%"|[['..link_list..'|编号]]!!colspan="3"|'..title_route..'!!width="11%"|运营商!!width="21%"|'..title_note..'\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%"|[['..link_list..'|编号]]!!colspan="3"|'..title_route..'!!width="24%"|'..title_note..'\n|-style="background:#EAECF0" height=0\n|colspan="2"| ||width="28%"| || ||width="28%"| || '
		else											-- 显示公司和票价栏(默认)
			t = '!colspan="2" width="12%"|[['..link_list..'|编号]]!!colspan="3"|'..title_route..'!!width="8%"|收费!!width="10%"|运营商!!width="20%"|'..title_note..'\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, tt.vehicle, tt.time, tt.image)
	return ret
end

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

return p