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

Module:Lua banner

猛汉♂百科,万男皆可猛的百科全书!转载请标注来源页面的网页链接,并声明引自猛汉百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
  1. local yesno = require('Module:Yesno')
  2. local mList = require('Module:List')
  3. local mTableTools = require('Module:TableTools')
  4. local mMessageBox = require('Module:Message box')
  5. local p = {}
  6. function p.main(frame)
  7. local origArgs = frame:getParent().args
  8. local args = {}
  9. for k, v in pairs(origArgs) do
  10. v = v:match('^%s*(.-)%s*$')
  11. if v ~= '' then
  12. args[k] = v
  13. end
  14. end
  15. return p._main(frame, args)
  16. end
  17. function p._main(frame, args)
  18. local modules = mTableTools.compressSparseArray(args)
  19. local box = p.renderBox(modules)
  20. local trackingCategories = p.renderTrackingCategories(args, modules)
  21. return box .. trackingCategories
  22. end
  23. -- if action=edit
  24. function p.main2(frame)
  25. local origArgs = frame:getParent().args
  26. local args = {}
  27. for k, v in pairs(origArgs) do
  28. v = v:match('^%s*(.-)%s*$')
  29. if v ~= '' then
  30. args[k] = v
  31. end
  32. end
  33. local modules = mTableTools.compressSparseArray(args)
  34. return p.renderBox(modules)
  35. end
  36. --end
  37. function p.renderBox(modules)
  38. local boxArgs = {}
  39. if #modules < 1 then
  40. boxArgs.text = '<strong class="error">錯誤:没有指定-{zh-hans:模块; zh-hant:模組;}-</strong>'
  41. else
  42. local moduleLinks = {}
  43. for i, module in ipairs(modules) do
  44. moduleLinks[i] = string.format('%s', module)
  45. moduleLinks[i] = '[[:Module:' .. mw.title.new(moduleLinks[i]).text .. ']]'
  46. end
  47. local moduleList = mList.makeList('bulleted', moduleLinks)
  48. boxArgs.text = '此' ..
  49. (mw.title.getCurrentTitle():inNamespaces(828,829) and '-{zh-hans:模块; zh-hant:模組;}-' or '模板') ..
  50. '使用[[Help:Lua|Lua語言]]:\n' .. moduleList
  51. end
  52. boxArgs.type = 'notice'
  53. boxArgs.small = true
  54. boxArgs.image = '[[File:Lua-logo.svg|30px|alt=|link=]]'
  55. return mMessageBox.main('mbox', boxArgs)
  56. end
  57. function p.renderTrackingCategories(args, modules, titleObj)
  58. if yesno(args.nocat) then
  59. return ''
  60. end
  61. local cats = {}
  62. -- 错误分类
  63. if #modules < 1 then
  64. cats[#cats + 1] = '有错误的Lua模板'
  65. end
  66. -- Lua templates category
  67. titleObj = titleObj or mw.title.getCurrentTitle()
  68. local subpageBlacklist = {
  69. doc = true,
  70. sandbox = true,
  71. sandbox2 = true,
  72. testcases = true
  73. }
  74. if titleObj.namespace == 10
  75. and not subpageBlacklist[titleObj.subpageText]
  76. then
  77. local category = args.category
  78. if not category then
  79. local categories = {
  80. ['Module:String'] = '使用String模块的模板',
  81. --['Module:Math'] = '使用Math模块的模板',
  82. --['Module:BaseConvert'] = '使用BaseConvert模块的模板',
  83. --['Module:Citation'] = '使用Citation模块的模板'
  84. }
  85. categories['Module:Citation/CS1'] = categories['Module:Citation']
  86. category = modules[1] and categories[modules[1]]
  87. category = category or 'x'
  88. end
  89. cats[#cats + 1] = category
  90. local protLevels = {
  91. autoconfirmed = 1,
  92. extendedconfirmed = 2,
  93. techedit = 3,
  94. patrolleredit = 4,
  95. sysop = 5
  96. }
  97. local currentProt = titleObj.protectionLevels["edit"][1]
  98. if currentProt == nil then currentProt = 0 else currentProt = protLevels[currentProt] end
  99. for i, module in ipairs(modules) do
  100. if mw.title.new(module).protectionLevels["edit"] then
  101. local moduleProt = mw.title.new(module).protectionLevels["edit"][1]
  102. if moduleProt == nil then moduleProt = 0 else moduleProt = protLevels[moduleProt] end
  103. if moduleProt < currentProt then
  104. cats[#cats + 1] = "使用受保护Lua模块的模板"
  105. break
  106. end
  107. end
  108. end
  109. end
  110. for i, cat in ipairs(cats) do
  111. cats[i] = string.gsub(string.format('[[Category:%s]]', cat), '%[%[Category:x%]%]', '')
  112. end
  113. return table.concat(cats) .. '[[Category:Lua模板]]'
  114. end
  115. return p