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

Module:Lyrics/multiver

猛汉♂百科,万男皆可猛的百科全书!转载请标注来源页面的网页链接,并声明引自猛汉百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
  1. local module = {}
  2. local getArgs = require('Module:Arguments').getArgs
  3. local buildLyrics = require('Module:Lyrics/Sandbox')._lyrics
  4. local initHandler = require('Module:HooksHandler').init
  5. local preParse = function (original, translated, customArgs)
  6. customArgs.mutiTags = {}
  7. customArgs.tags_map = {}
  8. customArgs.btn_args = {}
  9. local btn_count = 0
  10. local idx, count, start, tag_name = 1, #original, nil, nil
  11. local frame = mw.getCurrentFrame()
  12. local count_base = frame:callParserFunction{ name = '#var', args = { 'Lyrics-Btn-Count', '0' } }
  13. local endup = function ()
  14. if not customArgs.tags_map[tag_name] then
  15. customArgs.tags_map[tag_name] = 'Lyrics-Btn-' .. count_base + btn_count
  16. customArgs.btn_args['Lyrics-Btn-' .. count_base + btn_count] = tag_name
  17. btn_count = btn_count + 1
  18. end
  19. table.insert(customArgs.mutiTags, {start, idx, name = tag_name})
  20. table.remove(original, idx) count = count - 1
  21. start = nil
  22. end
  23. while idx <= count do
  24. if start then
  25. if mw.ustring.match(original[idx], '^<%-%-Tag%-End%-%->$') then
  26. endup()
  27. else
  28. idx = idx + 1
  29. end
  30. else
  31. tag_name = mw.ustring.match(original[idx], '^<%-%-Tag%-Start:(.-)%-%->$')
  32. if tag_name then
  33. table.remove(original, idx) count = count - 1
  34. start = idx
  35. else
  36. idx = idx + 1
  37. end
  38. end
  39. end
  40. if start then
  41. endup()
  42. end
  43. if customArgs.defaultVer and customArgs.tags_map[customArgs.defaultVer] then
  44. customArgs.btn_args['@default'] = customArgs.tags_map[customArgs.defaultVer]
  45. elseif btn_count == 1 and customArgs.defaultVer then
  46. customArgs.btn_args['Lyrics-Btn-' .. count_base + btn_count] = customArgs.defaultVer
  47. customArgs.btn_args['@default'] = 'Lyrics-Btn-' .. count_base + 1
  48. btn_count = btn_count + 1
  49. else
  50. customArgs.btn_args['@default'] = 'Lyrics-Btn-' .. count_base + 1
  51. end
  52. frame:callParserFunction{ name = '#vardefine', args = { 'Lyrics-Btn-Count', count_base + btn_count } }
  53. end
  54. local postParse = function (lines, customArgs)
  55. for idx, tag in ipairs(customArgs.mutiTags) do
  56. table.insert(lines, tag[1]+(idx-1)*2, '<div class="textToggleDisplay hidden" data-id="' .. customArgs.tags_map[tag.name] .. '">')
  57. table.insert(lines, tag[2]+(idx-1)*2+1, '</div>')
  58. end
  59. end
  60. local preOutput = function (html, customArgs)
  61. local count = 1
  62. customArgs.btn_args['@radio'] = true
  63. customArgs.btn_args['@forceNoCancel'] = true
  64. return mw.getCurrentFrame():expandTemplate{ title = '切换显示按钮', args = customArgs.btn_args } .. html
  65. end
  66. function module.initHooks(args, hooksHandler, customArgs)
  67. customArgs.defaultVer = args.default
  68. return hooksHandler({ preParse = preParse, postParse = postParse, preOutput = preOutput })
  69. end
  70. function module.main(frame)
  71. local args = getArgs(frame, { wrappers = 'Template:LyricsKai/mutiver' })
  72. local hooksHandler, customArgs = initHandler(), {}
  73. local hookTrigger = module.initHooks(args, hooksHandler, customArgs)
  74. return buildLyrics(args, hookTrigger, customArgs)
  75. end
  76. return module