置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

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
  1. local module = {}
  2. function module.main(frame)
  3. local pageName = frame.args[1] or (mw.title.getCurrentTitle().fullText .. "/历史上的今天")
  4. local text = mw.title.new(pageName):getContent()
  5. if not text then
  6. mw.addWarning("页面“[[" .. pageName .. "]]”不存在。")
  7. return frame.args["empty"] or ""
  8. else
  9. local date = os.date("!*t", os.time() + 3600 * 8) -- UTC+0800
  10. local today = date["month"] .. "-" .. date["day"]
  11. local splitedText = mw.text.gsplit(text, "\n", true)
  12. local resultText = ""
  13. local resultImages = {}
  14. local lastYear = ""
  15. for line in splitedText do
  16. local splitedLine = mw.text.split(line, ",", true)
  17. local lineDate = splitedLine[2] .. "-" .. splitedLine[3]
  18. if lineDate == today then
  19. resultText = resultText .. "\n* " .. splitedLine[1] .. "年" .. splitedLine[2] .. "月" .. splitedLine[3] .. "日:"
  20. resultText = resultText .. splitedLine[4]
  21. if splitedLine[5] ~= "" then
  22. table.insert(resultImages, splitedLine[5])
  23. end
  24. lastYear = splitedLine[1]
  25. end
  26. end
  27. math.randomseed(os.time())
  28. if resultText ~= "" then
  29. if #resultImages > 0 then
  30. resultText = "\n[[File:" .. resultImages[math.random(#resultImages)] .. "|200x200px|right|link=]]" .. resultText
  31. end
  32. resultText = frame:preprocess(mw.text.unstripNoWiki(frame.args["prefix"] or "") .. resultText .. mw.text.unstripNoWiki(frame.args["postfix"] or ""))
  33. return resultText
  34. else
  35. return frame.args["empty"] or ""
  36. end
  37. end
  38. end
  39. return module