跳转到内容

模組:WHS criteria

本页使用了标题或全文手工转换
被永久保护的模块
维基百科,自由的百科全书
(重定向自Module:UNESCO WHS type

local p = {}

local cultural_criteria = {"(i)", "(ii)", "(iii)", "(iv)", "(v)", "(vi)"}
local natural_criteria = {"(vii)", "(viii)", "(ix)", "(x)"}

-- 处理文本
local function processText(text)
	local arabic_to_roman = {
        ["(1)"] = "(i)", ["(2)"] = "(ii)", ["(3)"] = "(iii)",
        ["(4)"] = "(iv)", ["(5)"] = "(v)", ["(6)"] = "(vi)",
        ["(7)"] = "(vii)", ["(8)"] = "(viii)", ["(9)"] = "(ix)",
        ["(10)"] = "(x)"
    }
    text = text:gsub(":", "("):gsub(":", "(")
                :gsub("、", ")(")
                :gsub(",", ")(")
                :gsub(",", ")(")
                :gsub("/", ")(")
                :gsub("%s+", ")(")
                :gsub("\n", ")(")
                :gsub("(", "("):gsub(")", ")")
                -- 罗马数字大写转小写
                :gsub("I", "i")
                :gsub("V", "v")
                :gsub("X", "x")
    -- 阿拉伯数字转罗马数字
    text = text:gsub("%(%d+%)", arabic_to_roman)
    text = "(" .. text .. ")"
    return text
end

-- 匹配标准
local function checkCriteria(text, criteria)
    local found = {}
    for _, criterion in ipairs(criteria) do
        if text:find(criterion, 1, true) then
            table.insert(found, criterion)
        end
    end
    return found
end

-- 主函数
function p.format(frame)
    local input = frame.args[1] or ""
	input = processText(input)

    -- 检查是否是文化、自然遗产
    local cultural_found = checkCriteria(input, cultural_criteria)
    local natural_found = checkCriteria(input, natural_criteria)

    local result = ""

    if #cultural_found > 0 and #natural_found > 0 then
        result = "[[文化遗产|文化]]:" .. table.concat(cultural_found) .. ",[[自然遗产|自然]]:" .. table.concat(natural_found)
    elseif #cultural_found > 0 then
        result = "[[文化遗产|文化]]:" .. table.concat(cultural_found)
    elseif #natural_found > 0 then
        result = "[[自然遗产|自然]]:" .. table.concat(natural_found)
    else
        if mw.title.getCurrentTitle().namespace == 0 then
		    result = "[[Category:世界遗产登录标准无法识别]]"
		end
    end

    return result
end

return p