跳转到内容

模組:China Expwy Name

被永久保护的模块
维基百科,自由的百科全书

这是本页的一个历史版本,由Siyuwj留言 | 贡献2013年4月28日 (日) 19:06编辑。这可能和当前版本存在着巨大的差异。

-----------------------------------------------------------------------
-- Module:China Expwy Name - Lua module for China Expwy Name template
-----------------------------------------------------------------------
-- For the {{China Expwy Name}} code-name convensions

local p = {}

-- Icon width for different code, according to GB5768-2009
local width_translation = {
    [1] = "22px",
    [2] = "27px",
    [3] = "32px",
    [4] = "37px"
}

local canonical_provinces = { CHINA = "China", BEIJING = "Beijing", 
    TIANJIN = "Tianjin", SHANGHAI = "Shanghai", CHONGQING = "Chongqing",
    HEBEI = "Hebei", SHANXI = "Shanxi", LIAONING = "Liaoning", JILIN = "Jilin",
    HEILONGJIANG = "Heilongjiang", JIANGSU = "Jiangsu", ZHEJIANG = "Zhejiang",
    ANHUI = "Anhui", FUJIAN = "Fujian", JIANGXI = "Jiangxi", 
    SHANDONG = "Shandong", HENAN = "Henan", HUBEI = "Hubei", HUNAN = "Hunan",
    GUANGDONG = "Guangdong", HAINAN = "Hainan", SICHUAN = "Sichuan",
    GUIZHOU = "Guizhou", YUNNAN = "Yunnan", SHAANXI = "Shaanxi", GANSU = "Gansu",
    QINGHAI = "Qinghai", TAIWAN = "Taiwan", INNERMONGOLIA = "InnerMongolia",
    GUANGXI = "Guangxi", TIBET = "Tibet", Ningxia = "Ningxia", 
    XINJIANG = "Xinjiang", HONGKONG = "Hongkong", MACAU = "Macau" }

-- Main expressway code-name translation library
local expwy_lib = mw.loadData("Module:China Expwy Name/NameFromCode")
-- Code translation against ambiguity, such as G1501s
local code_translation = mw.loadData("Module:China Expwy Name/TranslateCode")

function load_province_code(province)
    if expwy_lib[province] == nil then
        expwy_lib[province] = mw.loadData("Module:China Expwy Name/NameFromCode/" .. canonical_provinces[province])
    end
end

function trim(s)
  return s:match "^%s*(.-)%s*$"
end

function internal_translatecode(province, id)
    if code_translation[province] == nil or code_translation[province][id] == nil then
        return id
    else
        return code_translation[province][id]
    end
end

-- Functionality for code-name conversion
function p.fullname(frame)
    if frame.args[1] == nil then
        return "error"
    else
        id = trim(string.upper(frame.args[1]))
    end
    if frame.args[2] ~= nil and frame.args[2] ~= "" then
        province = trim(string.upper(frame.args[2]))
        load_province_code(province)
        if expwy_lib[province] == nil then
            return "error"
        elseif expwy_lib[province][id] == nil then
            return "error"
        else
            return expwy_lib[province][id]
        end
    else
        return "error"
    end
end

-- Functionality for raw code fetching (e.g., G1501SH -> G1501)
function p.translatecode(frame)
    if frame.args[1] == nil then
        return ""
    else
        id = trim(string.upper(frame.args[1]))
    end
    if frame.args[2] == nil then
        province = "CHINA"
    else
        province = trim(string.upper(frame.args[2]))
    end
    return internal_translatecode(province, id)
end

-- Functionality for sign width fetching
function p.image_no_name(frame)
    if frame.args[1] == nil then
        return ""
    else
        id = trim(string.upper(frame.args[1]))
    end
    if frame.args[2] == nil or trim(frame.args[2]) == '' then
        province = "CHINA"
    else
        province = trim(string.upper(frame.args[2]))
    end
    true_code = internal_translatecode(province, id)
    true_code_len = string.len(true_code) - 1
    if frame.args[3] ~= nil and string.upper(frame.args[3]) ~= "D" then
        image_width = frame.args[3]
    else
        if width_translation[true_code_len] == nil then
            return ""
        else
            image_width = width_translation[true_code_len]
        end
    end
    base_image_name = canonical_provinces[province] .. " Expwy " .. true_code .. " sign no name"
    svg_title = mw.title.makeTitle("File", base_image_name .. '.svg')
    if svg_title.fileExists then
        return '[[File:' .. base_image_name .. '.svg|' .. image_width .. '|' .. true_code .. ']]'
    else
        return ""
    end
end

-- Functionality for sign width fetching
function p.signwidth(frame)
    if frame.args[1] == nil then
        return width_translation[1]
    else
        id = trim(string.upper(frame.args[1]))
    end
    if frame.args[2] == nil then
        province = "CHINA"
    else
        province = trim(string.upper(frame.args[2]))
    end
    
    true_code_len = string.len(internal_translatecode(province, id)) - 1
    if width_translation[true_code_len] == nil then
        return width_translation[1]
    else
        return width_translation[true_code_len]
    end
end

return p