跳转到内容

模組:Infobox NRHP

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

这是Module:Infobox NRHP当前版本,由Kcx36留言 | 贡献编辑于2025年7月11日 (五) 10:49 (// InPageEdit)。这个网址是本页该版本的固定链接。

(差异) ←上一修订 | 最后版本 (差异) | 下一修订→ (差异)
local getArgs = require('Module:Arguments').getArgs
local p = {}

-- 检查字符串是否为空
local function isNotEmpty(s)
    return s and s ~= '' and true or false
end

-- 处理ID
local function makeNRHPFocusLink(refnum)
    if not refnum then
        return ''
    end
    -- 提取数字部分
    local num = refnum:match('^%s*(%d+)')
    if not num then
        return refnum
    end
    return '[' .. 'https://npgallery.nps.gov/AssetDetail/NRIS/' .. num .. ' ' .. num .. ']' .. refnum:sub(#num + 1)
end

-- 处理FORMER前缀
local function processFormerPrefix(s)
    if not s then
        return s
    end
    if s:upper():sub(1, 6) == 'FORMER' then
        return s:sub(7)
    end
    return s
end

-- 主函数
function p.generateDesignationList(frame)
    local args = getArgs(frame)
    local result = {}

    local needsCleanup = false

    -- designation1
    local refnum = args.refnum or ''
    local hasValidRefnum = refnum:match('^%s*%d%d%d%d%d%d%d%d') ~= nil

    if hasValidRefnum then
        if not (args.nrhp_type and args.nrhp_type:lower() == "nrhp") then
            table.insert(result, '| designation1 = NRHP')
        end
    else
        -- 当designation1为空时,检查是否需要清理
        needsCleanup = true
        local nrhpTypes = {args.nrhp_type, args.nrhp_type2, args.nrhp_type3, args.nrhp_type4}

        for _, typ in ipairs(nrhpTypes) do
            if typ and (typ:lower() == 'cp' or typ:lower() == 'nhldcp') then
                needsCleanup = false
                break
            end
        end
    end

    -- 计算suffix
    local suffix = '1' -- 默认放在designation1

    -- 找出哪些designation有自己的信息
    local hasOwnInfo = {}
    for i = 1, 4 do
        local nrhpType = args['nrhp_type' .. (i == 1 and '' or i)]
        local designatedType = args['designated_nrhp_type' .. (i == 1 and '' or i)]
        hasOwnInfo[i + 1] = isNotEmpty(nrhpType) and isNotEmpty(designatedType)
    end

    -- 找出最后一个没有自己信息的designation
    local lastWithoutOwnInfo = nil
    for i = 2, 5 do -- designation2到designation5
        if isNotEmpty(args['nrhp_type' .. (i == 2 and '' or tostring(i - 1))]) then
            if not hasOwnInfo[i] then
                lastWithoutOwnInfo = tostring(i)
            end
        end
    end

    -- 如果有这样的designation,就使用它的编号
    if lastWithoutOwnInfo then
        suffix = lastWithoutOwnInfo
    end

    -- 日期
    if isNotEmpty(args.added) then
        table.insert(result, '| designation' .. suffix .. '_date = ' .. args.added)
    end

    if isNotEmpty(args.delisted) then
        table.insert(result, '| delisted' .. suffix .. '_date = ' .. args.delisted)
    end

    -- 编号
    local refnumValue = ''
    if isNotEmpty(args.refnum) then
        refnumValue = makeNRHPFocusLink(args.refnum)

        -- increase_refnum
        local increaseRefnums = {args.increase_refnum or args.increase1_refnum, args.increase2_refnum,
                                 args.increase3_refnum, args.increase4_refnum}
        local IncreaseCount = 0
        if isNotEmpty(increaseRefnums[2]) then
            IncreaseCount = 1
        end

        for i, incRef in ipairs(increaseRefnums) do
            if isNotEmpty(incRef) then
                local partText = IncreaseCount == 1 and "(扩展部分" .. tostring(i) .. ")" or
                                     "(扩展部分)"
                refnumValue = refnumValue .. '<br />' .. makeNRHPFocusLink(incRef) .. partText
            end
        end

        -- decrease_refnum
        local decreaseRefnums = {args.decrease_refnum or args.decrease1_refnum, args.decrease2_refnum,
                                 args.decrease3_refnum, args.decrease4_refnum}
        local DecreaseCount = 0
        if isNotEmpty(increaseRefnums[2]) then
            DecreaseCount = 1
        end

        for i, decRef in ipairs(decreaseRefnums) do
            if isNotEmpty(decRef) then
                local partText = DecreaseCount == 1 and "(缩减部分" .. tostring(i) .. ")" or
                                     "(缩减部分)"
                refnumValue = refnumValue .. '<br />' .. makeNRHPFocusLink(decRef) .. partText
            end
        end
    end

    if refnumValue ~= '' then
        table.insert(result, '| designation' .. suffix .. '_number = ' .. refnumValue)
    end

    -- MPS
    if isNotEmpty(args.mpsub) then
        table.insert(result, '| designation' .. suffix .. '_free1name = [[國家史蹟名錄#多重物件遞交|MPS]]')
        table.insert(result, '| designation' .. suffix .. '_free1value = ' .. args.mpsub)
    end

    -- 范围扩展
    local increaseValues = {args.increase or args.increase1, args.increase2, args.increase3, args.increase4}

    local increaseValue = ''
    for i, inc in ipairs(increaseValues) do
        if isNotEmpty(inc) then
            if i > 1 then
                increaseValue = increaseValue .. '<br />'
            end
            increaseValue = increaseValue .. inc
        end
    end

    if increaseValue ~= '' then
        table.insert(result, '| designation' .. suffix .. '_free2name = 范围扩展')
        table.insert(result, '| designation' .. suffix .. '_free2value = ' .. increaseValue)
    end

    -- 范围缩减
    local decreaseValues = {args.decrease or args.decrease1, args.decrease2, args.decrease3, args.decrease4}

    local decreaseValue = ''
    for i, dec in ipairs(decreaseValues) do
        if isNotEmpty(dec) then
            if i > 1 then
                decreaseValue = decreaseValue .. '<br />'
            end
            decreaseValue = decreaseValue .. dec
        end
    end

    if decreaseValue ~= '' then
        table.insert(result, '| designation' .. suffix .. '_free3name = 范围缩减')
        table.insert(result, '| designation' .. suffix .. '_free3value = ' .. decreaseValue)
    end

    -- designation2-5
    for i = 1, 4 do
        local nrhpType = args['nrhp_type' .. (i == 1 and '' or i)]
        local delistedType = args['delisted_nrhp_type' .. (i == 1 and '' or i)]
        local designatedType = args['designated_nrhp_type' .. (i == 1 and '' or i)]
        local delistedDate = args['delisted_nrhp_type' .. (i == 1 and '' or i)]

        if isNotEmpty(nrhpType) then
            -- designation
            local designationValue = processFormerPrefix(nrhpType)
            if isNotEmpty(delistedType) then
                designationValue = processFormerPrefix(nrhpType)
            end
            table.insert(result, '| designation' .. (i + 1) .. ' = ' .. (designationValue or ''))

            -- 评定时间
            if isNotEmpty(designatedType) then
                table.insert(result, '| designation' .. (i + 1) .. '_date = ' .. designatedType)
            end

            -- 除名时间
            if isNotEmpty(delistedDate) then
                table.insert(result, '| delisted' .. (i + 1) .. '_date = ' .. delistedDate)
            end
        end
    end

    -- 返回结果
    local output = '{{Infobox designation list\n| embed = yes\n' .. table.concat(result, '\n') .. '\n}}'

    if needsCleanup then
        output = '{{main other|[[Category:需要清理的美國國家歷史名勝模版]]}}\n' .. output
    end

    return frame:preprocess(output)
end

return p