跳至內容

模組:FUMO Regular Conferences

出自微國家百科

可在模組:FUMO Regular Conferences/doc建立此模組的說明文件

local p = {}

-- 輔助函數:根據狀態參數返回對應的背景顏色樣式和顯示文字
local function getStatusStyleAndText(status)
    if not status or status == "" then
        return "", ""
    end
    
    -- 標準化輸入:轉為小寫並去除前後空白
    local s = mw.ustring.lower(mw.text.trim(status))
    
    -- 判斷是否為「通過」(yes/y/是)
    if s == "yes" or s == "y" or s == "是" or s == "通過" then
        return 'style="background:#90FF90; text-align:center;"', "通過"
    
    -- 判斷是否為「否決」(no/n/否)
    elseif s == "no" or s == "n" or s == "否" or s == "否決" then
        return 'style="background:#FF9090; text-align:center;"', "否決"
    
    -- 判斷是否為「擱置」(shelved/s)
    elseif s == "shelved" or s == "s" or s == "擱置" then
        return 'style="background:#FFFF90; text-align:center;"', "擱置"
    
    -- 其他輸入則回傳空樣式和原始文字
    else
        return 'style="text-align:center;"', status
    end
end

function p.renderTable(frame)
    -- 獲取所有參數
    local args = frame:getParent().args
    local rows = {}

    -- 定義表格樣式
    local tableStyle = 'class="wikitable plainrowheaders sticky-header" style="text-align:center; margin:auto;"'
    local headerStyle = 'style="background:#00334d; color:#FFF; text-align:center;"'

    -- 遍歷參數,構建行數據
    -- 參數命名為 number, type, note, status, content, number1, type1, note1, status1, content1, ...
    local i = 0
    while true do
        local suffix = (i == 0) and "" or tostring(i)
        local num = args["number" .. suffix]
        local ty = args["type" .. suffix]
        local cont = args["content" .. suffix]
        local stat = args["status" .. suffix]
        local nt = args["note" .. suffix]

        -- 如果找不到編號(核心標識),則停止循環
        if num == nil then
            break
        end

        -- 將這一行數據存入表格
        table.insert(rows, {
            number = num,
            type = ty or "",
            content = cont or "",
            status = stat or "",
            note = nt or ""
        })

        i = i + 1
    end

    -- 開始構建表格輸出
    local result = {}
    table.insert(result, '{| ' .. tableStyle)
    
    -- 表頭
    table.insert(result, '|-')
    table.insert(result, '! ' .. headerStyle .. ' | 編號')
    table.insert(result, '! ' .. headerStyle .. ' | 類型')
    table.insert(result, '! ' .. headerStyle .. ' | 內容')
    table.insert(result, '! ' .. headerStyle .. ' | 狀態')
    
    -- 遍歷行數據,生成表格行
    for index, row in ipairs(rows) do
        -- 判斷是否為偶數行(雙數行使用灰色背景)
        local isEven = (index % 2 == 0)
        local rowBackground = isEven and 'style="background:#f2f5f7;"' or 'style="background:#FFFFFF;"'
        
        -- 獲取狀態的樣式(背景顏色)和顯示文字
        local statusStyle, statusText = getStatusStyleAndText(row.status)
        
        -- 數據行(編號、類型、註釋使用斑馬背景)
        table.insert(result, '|-')
        table.insert(result, '| ' .. rowBackground .. ' | ' .. row.number)
        table.insert(result, '| ' .. rowBackground .. ' | ' .. row.type)
        table.insert(result, '| ' .. rowBackground .. ' | ' .. row.content)
        
        -- 狀態欄位:套用狀態顏色,覆蓋斑馬背景
        table.insert(result, '| ' .. statusStyle .. ' | ' .. statusText)
        
        -- 內容行(合併四列,使用對應的斑馬背景)
        table.insert(result, '|-')
        table.insert(result, '| colspan="4" ' .. rowBackground .. ' | ' .. row.note)
    end
    
    -- 結束表格
    table.insert(result, '|}')
    
    return table.concat(result, '\n')
end

return p