Module:Sandbox/Xzonn
跳到导航
跳到搜索
本模块用于专题页“历史上的今天”一栏,尚处于实验阶段。如有其他好想法,欢迎提出或者直接修改。
使用方法
{{#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