模組:沙盒/魔琴/SDR

local data = require('Module:Delete/data')

local z = {}

function extractAliases(item)
    allnames = {item['code']}
    for j, alias in ipairs(item['aliases']) do
        table.insert(allnames, alias)
    end
    return allnames
end

function extractShortDesc(item)
    if item['description'] then
        return item['description']:gsub('{', '{'):gsub('}', '}'):gsub('|', '|')
    else
        return ''
    end
end

function extractDeleteReason(item)
    return item['deletereason'] or item['criteria'] or ''
end

function shortDesc(frame, name)
    name = mw.text.trim(name):upper()
    wt = {}
    for i, item in ipairs(data) do
        if name == '' or #name == 1 and item['code']:sub(1, 1) == name or item['code'] == name then
            para = extractShortDesc(item)
            if para ~= '' then
                table.insert(wt, para)
            end
        end
    end
    if short then
        return table.concat(wt, '\n')
    else
        return frame:preprocess(table.concat(wt, '\n'))
    end
end

function z.input(frame)
    if frame.args.parent then
        args = frame:getParent().args
    else
        args = frame.args
    end
    -- precache
    map = {}
    for i, item in ipairs(data) do
        map[item['code']:lower()] = i
        for j, alias in ipairs(item['aliases']) do
            map[alias:lower()] = i
        end
    end
    -- parse
    i = 1
    rows = {}
    pretext = {}
    deletelinks = {}
    while i < 10 do
        arg = args[i]
        if arg and map[mw.text.trim(arg:lower())] then
            item = data[map[mw.text.trim(arg:lower())]]
            if frame.args.reasoncode then
                return item['code']
            end
            title = mw.title.getCurrentTitle()
            checkfunc = item['check']
            if checkfunc then
                check = checkfunc(title)
            else
                check = nil
            end
            rowsuffix2 = ''
            deletesuffix = ''
            -- special case for F1/F5
            if item['code'] == 'F1' or item['code'] == 'F5' then
                i = i + 1
                if args[i] then
                    img = mw.text.trim(args[i])
                else
                    img = nil
                end
                if img then
                    imgtitle = mw.title.new(img, 'Media')
                else
                    imgtitle = nil
                end
                if imgtitle then
                    deletesuffix = '([[:File:' .. imgtitle.text .. ']])'
                end
            end
            row = '* <span title="' .. extractShortDesc(item) .. '">' .. '<strong>[[WP:CSD#' .. item['code'] .. '|' .. item['code'] .. ']]</strong>' .. deletesuffix .. ':' .. item['criteria'] .. '</span>'
            table.insert(rows, row)
        elseif arg and mw.text.trim(arg) ~= '' then
            if frame.args.reasoncode then
                return ''
            end
            -- try to read it as a title
            title = mw.title.new(mw.text.trim(arg))
            if title and title.exists then
                if frame.args.deletelink then
                    table.insert(deletelinks, '[[:' .. arg .. ']]')
                end
                table.insert(rows, '*<strong>'  .. '[[:' .. arg .. ']]</strong>')
            else
                if frame.args.deletelink then
                    table.insert(deletelinks, arg)
                end
                arg = string.gsub(arg, '^([*:#]*)(.*)', '%1<strong>%2</strong>')
                table.insert(rows, '*'  .. arg)
            end
        end

        arg = args['c' .. i]
        if arg and mw.text.trim(arg) ~= '' then
            table.insert(rows, '*' .. arg)
        end

        i = i + 1
    end
    if #rows > 0 then
        return mw.text.trim(table.concat(pretext) .. '\n' .. table.concat(rows, '\n'))
    else
        return '* <span style="color:#080;">(未填寫理由)</span>'
    end
end

return z