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

Module:If preview

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

此模块用于实现模板{{if preview}}和{{preview warning}}的功能。它有助于模板/模块确定它们是否处于被预览的状态。

倾向于在其他模板中实现该模板的版本。

若要在一个模块中使用main(),你需要传递一个框架表和一个args表。

对于预览警告,请使用_warning()

  1. local p = {}
  2. local cfg = mw.loadData('Module:If preview/configuration')
  3. --[[
  4. main
  5. This function returns either the first argument or second argument passed to
  6. this module, depending on whether the page is being previewed.
  7. ]]
  8. function p.main(frame)
  9. if cfg.preview then
  10. return frame.args[1] or ''
  11. else
  12. return frame.args[2] or ''
  13. end
  14. end
  15. --[[
  16. pmain
  17. This function returns either the first argument or second argument passed to
  18. this module's parent (i.e. template using this module), depending on whether it
  19. is being previewed.
  20. ]]
  21. function p.pmain(frame)
  22. return p.main(frame:getParent())
  23. end
  24. local function warning_text(warning)
  25. return mw.ustring.format(
  26. cfg.warning_infrastructure,
  27. cfg.templatestyles,
  28. warning
  29. )
  30. end
  31. function p._warning(args)
  32. local warning = args[1] and args[1]:match('^%s*(.-)%s*$') or ''
  33. if warning == '' then
  34. return warning_text(cfg.missing_warning)
  35. end
  36. if not cfg.preview then return '' end
  37. return warning_text(warning)
  38. end
  39. --[[
  40. warning
  41. This function returns a "preview warning", which is the first argument marked
  42. up with HTML and some supporting text, depending on whether the page is being previewed.
  43. disabled since we'll implement the template version in general
  44. ]]
  45. --function p.warning(frame)
  46. -- return p._warning(frame.args)
  47. --end
  48. --[[
  49. warning, but for pass-through templates like {{preview warning}}
  50. ]]
  51. function p.pwarning(frame)
  52. return p._warning(frame:getParent().args)
  53. end
  54. return p