模块:Review checklist

require("strict")

local getArgs = require("Module:Arguments").getArgs
local p = {}

local config = require("Module:Review checklist/config")

local function get_icon(str, text)
    local images = {
        pass = {filename = "Artículo bueno.svg", alt = "通过"},
        fail = {filename = "Symbol delete vote oversat.svg", alt = "未通过"},
        partly = {filename = "Convertir en bueno.svg", alt = "未见问题"},
        unsure = {filename = "Symbol question.svg", alt = "不确定"},
        nointention = {filename = "Symbol neutral vote.svg", alt = "无意检查"},
        waiting = {filename = "Symbol dot dot dot.svg", alt = "待回应…"},
        checking = {filename = "Symbol_wait.svg", alt = "检查中…"}
    }

    local alias_table = {
        -- pass
        ["1"] = "pass",
        ["y"] = "pass",
        ["v"] = "pass",
        -- fail
        ["0"] = "fail",
        ["n"] = "fail",
        ["x"] = "fail",
        -- partly
        ["0.5"] = "partly",
        [".5"] = "partly",
        ["1/2"] = "partly",
        -- unsure
        ["?"] = "unsure",
        ["?"] = "unsure",
        -- nointention
        ["="] = "nointention",
        ["-"] = "nointention",
        -- waiting
        ["."] = "waiting",
        ["..."] = "waiting"
        -- other cases for checking
    }

    local status = nil
    for k, v in pairs(alias_table) do
        if k == str then
            status = v
            break
        end
    end
    status = status or "checking"

    local image = images[status]
    return "[[File:" .. image.filename .. "|24px|" .. image.alt .. "|alt=" .. image.alt .. "]]"
        .. (text and " " .. image.alt or "")
end

local function build_row(args, rev_num)
    local str_frags, str = {}, ""
    table.insert(str_frags, "|-")

    local revn = "rev" .. tostring(rev_num)

    local username = args[revn]
    str = str .. "[[User:" .. username .. "|" .. username .. "]]"
    str = str .. " ([[User talk:" .. username .. "|讨论]])"
    table.insert(str_frags, '! scope="row" | ' .. str)

    local conf = config[args.type]
    for i = 1, #conf do
        str = get_icon(args[revn .. "_" .. conf[i][1]])
        table.insert(str_frags, "| " .. str)
    end

    str = get_icon(args[revn .. "_" .. "overall"], true)
    table.insert(str_frags, '| style="font-weight: bold;" | ' .. str)

    str = args[revn .. "_" .. "comment"] or ""
    table.insert(str_frags, '| style="font-size: smaller;" | ' .. str)

    return table.concat(str_frags, "\n")
end

function p.main(frame)
    local args = getArgs(frame)
    return p._main(args)
end

function p._main(args)
    local str_frags = {
    	mw.getCurrentFrame():extensionTag{ name = "templatestyles", args = { src = "Template:Plain row headers/styles.css"} }
    }
    local conf = config[args.type]

    -- Header
    local align = ''
    if args.align == 'right' then
        align = 'float: right;'
    elseif args.align == 'center' then
        align = 'margin-left: auto; margin-right: auto;'
    end
    table.insert(str_frags, '{| class="wikitable plain-row-headers" style="text-align: center;' .. align .. '"')
    table.insert(str_frags, "|+ " .. conf.name .. "标准检查表")
    table.insert(str_frags, '! scope="col" | 评审人')

    for i = 1, #conf do
        table.insert(str_frags, '! scope="col" | ' .. conf[i][2])
    end

    table.insert(str_frags, '! scope="col" | 总评')
    table.insert(str_frags, '! scope="col" | 备注')

    -- Body
    local counter = 0
    for i = 1, 30 do
        if args["rev" .. tostring(i)] ~= nil then
            counter = counter + 1
            table.insert(str_frags, build_row(args, counter))
        end
    end

    -- Footer
    if args.overall == 'yes' then
        local str = ""
        table.insert(str_frags, "|-")
        table.insert(str_frags, '! scope="row" style="font-weight: bold; text-align: center;" | 总结')
        for i = 1, #conf do
            str = get_icon(args["overall" .. "_" .. conf[i][1]])
            table.insert(str_frags, "! " .. str)
        end
        str = get_icon(args["overall" .. "_" .. "overall"], true)
        table.insert(str_frags, "! " .. str)
        str = args["overall" .. "_" .. "comment"] or ""
        table.insert(str_frags, '! style="font-size: smaller;" | ' .. str)
    end
    table.insert(str_frags, "|}")

    return table.concat(str_frags, "\n")
end

function p.single(frame)
    local args = getArgs(frame)
    return p._single(args)
end

function p._single(args)
    local str_frags = {}
    local conf = config[args.type]

    -- Header;
    table.insert(str_frags, '<div style="float: right; clear: right; border: 1px solid black; padding: 1em; font-size: small; margin-left: 1em; margin-bottom: 0.5em; width: 16em;">')
    table.insert(str_frags, '<div style="text-align: center; width: 100%; font-weight: bold; font-size: large;">' .. conf.name .. '标准检查表</div>')
    for i = 1, #conf do
        local criterion =  mw.ustring.gsub(conf[i][2], '<br>', '\t')
        local result =  mw.ustring.gsub(get_icon(args["rev1_" .. conf[i][1]], true), '24px', '16px')
        table.insert(str_frags, '* ' .. criterion .. ' - ' .. result)
    end
    table.insert(str_frags,
        '<div style="text-align: center; width: 100%; font-weight: bold; font-size: larger;">' ..
        '总评' .. ' - ' .. mw.ustring.gsub(get_icon(args['rev1_overall'], true), '24px', '20px') ..
        '</div>'
    )
    table.insert(str_frags, '<div>' .. args['rev1_comment'] .. '</div>')
    table.insert(str_frags, '</div>')
    
    return table.concat(str_frags, '\n')
end

return p