模組:沙盒/Zhengruiw02/test

文档图示 模块文档[创建]
-- 载入getArgs模块
local getArgs = require('Module:Arguments').getArgs
 
-- 导入数据
local d = mw.loadData('Module:SZM/data')
-- d.lineNames 线路名称解析列表
-- d.lineColors 线路色彩列表
local sd =  mw.loadData('Module:SZM/stationsData')
-- sd.stationlist 车站列表
-- sd.exitlist 出口列表
-- sd.stationsByLine 车站ID对应线路车站
local interchange = require('Module:SZM')._interchange


-- 声明模块本体
local p = {}

-- 格式化显示
p.displayFormat = {
    stationLink = [=[[[%link|%name]]]=],
}

-- 站台标志格式化
p.platformSignFileFormat = "[[File:SZM Line%line P%platform.svg|%size|link=|%platform-{zh-hans:站台; zh-hant:月台}-]]"

-- 出口列表表格绘制格格式化
local exitTableFormat = {
    open    = '{| class="wikitable"\n',
    header    = '! width:70px | 出口编号 !! width:70px |  <small>无障碍设施</small> !!  建议前往的目的地 \n|-\n',
    row        = '| align="center" | %s || %s || %s \n|-\n', --%Exit || %facility || %destinations
    note    = '| bgcolor="#EEEEEE" colspan=3 | 注:[[File:Tactile Paved.svg|alt=|link=]] 設有盲人引導徑 | [[File:Lift.svg|alt=|link=]] 設有[[升降機]] | [[File:Stairlift Sign.svg|alt=|link]] 設有輪椅升降台\n',
    close    = '|}\n',
}

-- 出口标志
local exitSignFormat = {
    single = "[[File:MTR Exit Filler.svg|4px|alt=|link=]][[File:MTR Exit %s.svg|16px|alt=%s|link=|%s]][[File:MTR Exit Filler.svg|4px|alt=|link=]]",
    combi = "[[File:MTR Exit %s.svg|16px|alt=%s|link=|%s]][[File:MTR Exit %s.svg|8px|alt=%s|link=|%s]]",
    sign = "[[File:MTR Exit Sign.svg|24px|alt=出口|link=|出口]]",
}

-- 出口编号模式匹配
-- 英文字母+数字,如A1 B2 E
local exitIDPattern = "(%a*)(%d*)" --都是0次或多次,最短匹配

-- 无障碍标志
local accessSign = {
    lift = '[[File:Lift.svg|alt=|link=]]',
    l = '[[File:Lift.svg|alt=|link=]]',   --instead of 'lift'
    pave = '[[File:Tactile Paved.svg|alt=|link=]]',
    p = '[[File:Tactile Paved.svg|alt=|link=]]',  --instead of 'pave'
    stairlift = '[[File:Stairlift Sign.svg|alt=|link=]]',
    s = '[[File:Stairlift Sign.svg|alt=|link=]]',  --instead of 'stair lift'
}

-- 构建出口标志
local function CallExitSign(exitMain, exitSub)
    if not exitMain or exitMain == "" then return "" end
    local tmp = ""
    if exitSub and exitSub ~= "" then
        local con = exitMain .. exitSub
        tmp = mw.ustring.format(exitSignFormat.combi, exitMain, con, con, exitSub, con, con)
    else
        tmp = mw.ustring.format(exitSignFormat.single, exitMain, exitMain, exitMain)
    end
    return exitSignFormat.sign .. tmp
end

-- 构建出口列表
local function BuildExitList(rows)
    local result = exitTableFormat.open .. exitTableFormat.header
    local cell = {}
    for i, row in ipairs(rows) do
        if (mw.text.trim(row) ~= '') then
            cell =  mw.text.split(row, '~~')
            if cell[1] and cell[1] ~="" then --至少要有exitID吧
                --首先得到出口标志
                local _, _, exitMain, exitSub = mw.ustring.find(cell[1], exitIDPattern)
                local exitSign = CallExitSign(exitMain, exitSub)
 
                --然后处理无障碍设施
                local accessibility = ""
                if cell[2] then --Lift, Path, etc
                    local tmp = mw.text.split(cell[2], '+')
                    for _, v in ipairs(tmp) do
                        --if v then
                            accessibility = accessibility .. (accessSign[mw.ustring.lower(v)] or "")
                        --end
                    end
                end
 
                --最后处理出口目的地
                local dest = cell[3]
 
                result = result .. mw.ustring.format(exitTableFormat.row, exitSign or "", accessibility or "", dest or "")
            end
        end
    end
    result = result .. exitTableFormat.note .. exitTableFormat.close
    return result
end

-- 构建站台标志
local function getPlatformSign(line, platform, size)
    if not line or not platform then return end
    local img = p.platformSignFileFormat:gsub("%%line", line)
    img = img:gsub("%%platform", platform)
    img = img:gsub("%%size", size or "20px")
    return img
end

-- 构建车站名查询表
do
    p.fromStaNameToStaID = {}
    for i, t in ipairs(sd.stationlist) do
        p.fromStaNameToStaID[t.name] = i
    end
end

-- 构建出口ID查询表,基于车站ID
do
    p.fromStationIDToExitID = {}
    for i, t in ipairs(sd.exitlist) do
        if not p.fromStationIDToExitID[tonumber(t.stationid)] then
            p.fromStationIDToExitID[tonumber(t.stationid)] = {}
        end
        table.insert(p.fromStationIDToExitID[tonumber(t.stationid)], i)
    end
end

-- 请求函数
local function makeInvokeFunction(funcName)
    -- makes a function that can be returned from #invoke, using
    -- [[Module:Arguments]].
    return function (frame)
        local args = getArgs(frame)
        return p[funcName](args)
    end
end

-- 本地函数:返回线路车站文字,参数1=线路数字,参数2=需要显示换乘站,返回=线路车站字符串
local function getLineStations(lineNumber, withTransfer)
    if not sd.stationsByLine[lineNumber] then return end
    local result = ""
    for i, stationID in ipairs(sd.stationsByLine[lineNumber]) do
        local link = p.displayFormat.stationLink:gsub("%%name", sd.stationlist[stationID].name)
        link = link:gsub("%%link", sd.stationlist[stationID].link)
        if i == 1 then
            result = link
        else
            result = result .. " - " .. link
        end
        if withTransfer then
            if type(sd.stationlist[stationID].line) == "table" then
                local lines = {}
                for k, line in pairs(sd.stationlist[stationID].line) do
                    if tonumber(line) ~= lineNumber then
                        table.insert(lines, tonumber(line))
                    end
                end
                result = result .. interchange(lines)
            end
        end
    end
    return result
end

-- 本地函数:从车站ID获取出口信息,参数1=车站ID,返回=车站出口信息
local function getExitfromStationID(stationID)
    if not p.fromStationIDToExitID[stationID] then return end
    local result = sd.stationlist[stationID].name .. '站出口详情\r\n\r\n'
    for k, v in ipairs(p.fromStationIDToExitID[stationID]) do
        result = result .. sd.exitlist[v]['exitname'] .. '出口 : ' .. sd.exitlist[v]['buildinglist'] .. "\r\n\r\n"
        if sd.exitlist[v]['buslist'] then
            result = result .. '公交车 : ' .. sd.exitlist[v]['buslist'] .. "\r\n\r\n"
        end
        --result = result .. "\r\n"
    end
    return result
end

-- 本地函数:从车站名称获取出口信息,参数1=车站名称,返回=车站出口信息
local function getExitfromStationName(stationName)
    if not p.fromStaNameToStaID[stationName] then return end
    return getExitfromStationID(p.fromStaNameToStaID[stationName])
end

-- 本地函数:从车站名称获取出口列表,参数1=车站名称,返回=车站出口信息列表
local function getExitListfromStationName(stationName)
    local stationID = p.fromStaNameToStaID[stationName]
    if not stationID then return end
    if not p.fromStationIDToExitID[stationID] then return end
    --local result = sd.stationlist[stationID].name .. '站出口详情\r\n\r\n'
    local rows = {}
    for k, v in ipairs(p.fromStationIDToExitID[stationID]) do
        table.insert(rows, sd.exitlist[v]['exitname'] .. '~~ ~~' .. sd.exitlist[v]['buildinglist'])
    end
    return BuildExitList(rows)
end
 
p.getcolor =  makeInvokeFunction('_getcolor')
p.getLineStations =  makeInvokeFunction('_getLineStations')
p.getExitfromStationID =  makeInvokeFunction('_getExitfromStationID')
p.getExitfromStationName =  makeInvokeFunction('_getExitfromStationName')
p.getExitListfromStationName =  makeInvokeFunction('_getExitListfromStationName')
p.getPlatformSign =  makeInvokeFunction('_getPlatformSign')
p.exitList =  makeInvokeFunction('_exitList')
 
function p._getLineStations(args)
    return getLineStations(tonumber(args[1]), args[2])
end
 
function p._getExitfromStationID(args)
    return getExitfromStationID(tonumber(args[1]))
end
 
function p._getExitfromStationName(args)
    return getExitfromStationName(args[1])
end

function p._getExitListfromStationName(args)
    return getExitListfromStationName(args[1])
end
 
function p._getPlatformSign(args)
    return getPlatformSign(args.line, args.platform, args.size)
end
 
function p._exitList(args)
    local list = args.list
    --mw.text.trim( list )
    local rows = mw.text.split(list, '\n')
    return BuildExitList(rows)
end
 
function p._templateLineStations(frame)
end
-- t = { {}, {},}
local function returnTable(t)
    local result = ""
    --local i = 0
    for k, v in ipairs(t) do
        result = result .. "[" .. k .. "] = "
        if type(v) == 'table' then
            result = result .. "{\r\n\r\n" .. returnTable(v) .. "},\r\n\r\n"
        else
            result = result .. "'" .. v .. "',\r\n\r\n";
        end
    end
    return result
end
 
p.displayLineNameFormat = {
    l = [=[[[深圳地铁%linenumber号线|%linenumber号线]]]=], --仅链接
    lc = [=[[[深圳地铁%linenumber号线|<span style="color:#%linecolor;">%linenumber号线</span>]]]=], --带色彩链接
    tc = [=[<span style="color:#%linecolor;">%linenumber号线</span>]=], --带色彩文本
    c = [=[<span style="color:#%linecolor;">■ </span>[[深圳地铁%linenumber号线|%linenumber号线]]]=], --带色块链接
    t = [=[%linenumber号线]=], --纯文本
    i = [=[[[深圳地铁%linenumber号线|<span title="可-{zh-hans:换乘; zh-hant:轉乘}-%linenumber号线" style="color:#%linecolor">■</span>]]]=], --换乘站
    box = [=[<span style="white-space: nowrap; font-family: sans-serif; background: #%linecolor; color: #%fgcolor; padding: 0 5px;">%linenumber号线</span>]=], --盒子
    rect      = [=[<span style="white-space:nowrap; background:#%linecolor; color:#%fgcolor; padding:0 5px;">%linenumber号线</span>]=], --矩形
    roundRect = [=[<span style="white-space:nowrap; background:#%linecolor; color:#%fgcolor; padding:2px 5px; font-weight:bold;border-radius:3px;">%linenumber号线</span>]=], --圆角矩形
}


-- 2016/04/15
-- 本地函数:解析线路色彩,参数=线路编号,返回=线路色彩RGB代码值
local function getLineColor(lineNumber)
    if d.lineColors[lineNumber] then
        return d.lineColors[lineNumber]
    else
        --error(errorlist.nocolor:gsub("%%d", lineNumber or "NULL"))
        return
    end
end
p.szmTemplate = makeInvokeFunction('_szmTemplate')
 -- 替换原版{{深圳地铁}}模板函数,参数1=线路名称,参数2=控制符,返回=输出结果
function p._szmTemplate(args)
    local lineName = args[1]
    local control = args[2]
    --local lineNumberStr = tostring(lineName)--fromLineNameToLineNumber(lineName)
    local lineNumber = tonumber(lineName)
    local lineColor = getLineColor(lineNumber)
    local fgColor = "FFFFFF"--getLineColor(lineNumber)
    if not control then control = "l" end
    local result = p.displayLineNameFormat[control]:gsub("%%linenumber", lineNumber)--:gsub("%%linecolor", lineColor):gsub("%%fgcolor", fgColor)
    result = result:gsub("%%linecolor", lineColor)
    result = result:gsub("%%fgcolor", fgColor)
    return result
end

function p.printTable(frame)
    local t =""
    if frame.args[1] then
        t = frame.args[1]
        if p[t] then
            return mw.text.nowiki(returnTable(p[t]))
        end
    end
end

return p