跳转到内容

模組:VGNL

维基百科,自由的百科全书

这是本页的一个历史版本,由Lopullinen留言 | 贡献2023年7月4日 (二) 15:24 (识别草稿)编辑。这可能和当前版本存在着巨大的差异。

require('strict')

local p = {}
local getArgs = require('Module:Arguments').getArgs
local function makeInvokeFunc(funcName)
    return function(frame)
        local args = getArgs(frame)
        return p[funcName](args)
    end
end

p.target = makeInvokeFunc('_target')
function p._target(args)
    local is_draft = args[1] == 'draft'

    local text = mw.title.new('WikiProject:電子遊戲/簡訊/目錄'):getContent()
    local ptn = 'data%-sort%-value%s*=%s*"' .. (is_draft and 'd' or '') .. '(%d%d%d%d%d%d%d%d)"'
    local available_dates = {}
    for v in text:gmatch(ptn) do
        table.insert(available_dates, v)
    end
    table.sort(available_dates, function(a, b)
        if is_draft then
            return a < b
        end
        return a > b
    end)
    local result_date = available_dates[1]

    local year, month = result_date:sub(1, 4), result_date:sub(5, 6)
    local issue = year .. '-' .. month
    return 'WikiProject:電子遊戲/簡訊/' .. issue
end

p.label = makeInvokeFunc('_label')
function p._label(args)
    local target = p._target(args)
    local text = mw.title.new(target):getContent()
    return text:match('%d%d%d%d年第.-季')
end

p.link = makeInvokeFunc('_link')
function p._link(args)
    local target = p._target(args)
    local label = p._label(args)
    return '[[' .. target .. '|' .. label .. ']]'
end

p.content = makeInvokeFunc('_content')
function p._content(args)
    local target = p._target(args)
    return mw.getCurrentFrame():expandTemplate { title = target }
end

p.toc = makeInvokeFunc('_toc')
function p._toc(args)
    local target = p._target(args)
    local text = mw.title.new(target):getContent()

    local items = {}
    local pattern = mw.ustring.gsub(target, 'WikiProject:', '維基專題:')
    pattern = '%[%[' .. mw.ustring.gsub(pattern, '%-', '%-') .. '/.-|.-%]%]'
    local pos = 1

    while true do
        local s, e = mw.ustring.find(text, pattern, pos, false)
        if s == nil then
            break
        end
        table.insert(items, mw.ustring.sub(text, s, e))
        pos = e + 1
    end

    if args.style == 'comma' then
        return table.concat(items, '、')
    end

    do
        local items_ = { }
        for _, item in ipairs(items) do
            table.insert(items_, '* ' .. item)
        end
        return table.concat(items_, '\n')
    end
end
return p