2023年政策修订增补工作正在进行中,欢迎参与!
  • Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

Module:Sandbox/Xzonn

萌娘百科,万物皆可萌的百科全书!转载请标注来源页面的网页链接,并声明引自萌娘百科。内容不可商用。
跳转到导航 跳转到搜索
Template-info.svg 模块文档  [查看] [编辑] [历史] [刷新]

本模块用于专题页“历史上的今天”一栏,尚处于实验阶段。如有其他好想法,欢迎提出或者直接修改。

使用方法

{{#invoke:Sandbox/Xzonn|main|<数据页面>}}

其中<数据页面>是可选项,不填则默认为“<调用页面>/历史上的今天”。

输出会以年份倒序显示(最近的年份在前)。

例如:

{{#invoke:Sandbox/Xzonn|main|User:Xzonn/任天堂/历史上的今天/{{#time:M}}}}

显示效果:

数据页面写法

实际为CSV格式,每行的各项以半角逗号“,”分隔,依次为:

年,月,日,内容,图片名

例如:

2008,8,8,北京奥运会在北京举办,Nintendo Switch JP - Mario & Sonic at the Olympic Games Tokyo 2020.jpg
local module = {}

function module.main(frame)
    local pageName = frame.args[1] or (mw.title.getCurrentTitle().fullText .. "/历史上的今天")
    local text = mw.title.new(pageName):getContent()
    if not text then
        mw.addWarning("页面“[[" .. pageName .. "]]”不存在。")
        return frame.args["empty"] or ""
    else
        local date = os.date("!*t", os.time() + 3600 * 8) -- UTC+0800
        local today = date["month"] .. "-" .. date["day"]
        local splitedText = mw.text.gsplit(text, "\n", true)
        local resultText = ""
        local resultImages = {}
        local lastYear = ""
        for line in splitedText do
            local splitedLine = mw.text.split(line, ",", true)
            local lineDate = splitedLine[2] .. "-" .. splitedLine[3]
            if lineDate == today then
                resultText = resultText .. "\n* " .. splitedLine[1] .. "年" .. splitedLine[2] .. "月" .. splitedLine[3] .. "日:"
                resultText = resultText .. splitedLine[4]
                if splitedLine[5] ~= "" then
                    table.insert(resultImages, splitedLine[5])
                end
                lastYear = splitedLine[1]
            end
        end
        math.randomseed(os.time())
        if resultText ~= "" then
            if #resultImages > 0 then
                resultText = "\n[[File:" .. resultImages[math.random(#resultImages)] .. "|200x200px|right|link=]]" .. resultText
            end
            resultText = frame:preprocess(mw.text.unstripNoWiki(frame.args["prefix"] or "") .. resultText .. mw.text.unstripNoWiki(frame.args["postfix"] or ""))
            return resultText
        else
            return frame.args["empty"] or ""
        end
    end
end

return module