本网站正在建设中(~ ̄▽ ̄)~

  • 你好~!欢迎来到中文歌声合成个人收集站-VCPedia.cn!
  • 若发现页面信息有误投稿至本站,请联系管理员。

Module:切换显示

VCPedia.cn ——关于中文歌声合成的一切。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
  1. local module = {}
  2. local getArgs = require('Module:Arguments').getArgs
  3. local sortedArgs = require('Module:SortArgs').sortedArgs
  4. local initSpan = function(className, templateName, frame)
  5. local args = getArgs(frame, { wrappers = 'Template:' .. templateName })
  6. local span = mw.html.create('span')
  7. span:attr('class', className)
  8. if className == 'textToggleDisplayButtons' then -- 自Template:切换显示调用。
  9. local operators = {} -- 操作参数。
  10. local contentlist = {} --[[内容表列。结构为:
  11. {
  12. strcode = 字符串代号,
  13. on = @on后缀的内容,
  14. off = @off后缀的内容,
  15. default = 没有@on或@off后缀的内容,
  16. }
  17. ]]
  18. for k, v in sortedArgs(args, frame.args['@sort'], '|') do
  19. mw.log(k, v)
  20. if mw.ustring.sub(k, 1, 1) == '@' then
  21. operators[k] = v
  22. else
  23. local strcode
  24. local onoff = nil
  25. -- 获取字符串代号和开关类型。
  26. if mw.ustring.sub(k, -3) == '@on' then
  27. strcode = mw.ustring.sub(k, 1, mw.ustring.len(k) - 3)
  28. onoff = 'on'
  29. elseif mw.ustring.sub(k, -4) == '@off' then
  30. strcode = mw.ustring.sub(k, 1, mw.ustring.len(k) - 4)
  31. onoff = 'off'
  32. else
  33. strcode = k
  34. end
  35. onoff = onoff or 'default' -- 没有开关类型就是默认值。
  36. if not contentlist[strcode] then
  37. table.insert(contentlist, {strcode = strcode}) -- 增加空的内容表。
  38. contentlist[strcode] = #contentlist -- 定位新增加的索引,绑定到参数k。
  39. end
  40. contentlist[contentlist[strcode]][onoff] = v -- 替换内容。
  41. end
  42. end
  43. local counter = 0 -- 计数器。
  44. for k_operators, v_operators in pairs(operators) do
  45. span:attr('data-key-' .. counter, k_operators):attr('data-value-' .. counter, v_operators)
  46. counter = counter + 1
  47. end
  48. for index, content in ipairs(contentlist) do
  49. span:tag('span'):attr('data-order', index - 1):attr('data-key', content.strcode):tag('span'):addClass(
  50. 'textToggleDisplayButtonLabelText on'
  51. ):wikitext(frame:preprocess(content.on or content.default)):done():tag('span'):addClass(
  52. 'textToggleDisplayButtonLabelText off'
  53. ):wikitext(frame:preprocess(content.off or content.default))
  54. end
  55. else
  56. local counter = 0 -- 计数器
  57. for k, v in pairs(args) do
  58. if v ~= nil then
  59. span:attr('data-key-' .. counter, k):attr('data-value-' .. counter, v)
  60. counter = counter + 1
  61. end
  62. end
  63. end
  64. return tostring(span)
  65. end
  66. function module.template(frame)
  67. local args = getArgs(frame, { wrappers = 'Template:切换显示/模板' })
  68. local tTitle = args['@'] or ''
  69. if tTitle == '' then
  70. return nil
  71. end
  72. local tag = args['@tag']
  73. local branches = {}
  74. local params = {}
  75. for k, v in sortedArgs(args, frame.args['@sort'], '|') do
  76. if not mw.ustring.match(k, '^@[^@]') then
  77. local i = 1
  78. while true do
  79. local _, j, flag = mw.ustring.find(k, '@(.)', i)
  80. if j == nil then
  81. i = mw.ustring.len(k)
  82. break
  83. elseif flag == '@' then
  84. i = j + 1
  85. else
  86. i = j - 2
  87. break
  88. end
  89. end
  90. local param = mw.text.trim(mw.ustring.gsub(mw.ustring.sub(k, 1, i), '@@', '@'))
  91. local branch = mw.ustring.sub(k, i + 2)
  92. if mw.text.trim(param) ~= '' then
  93. if params[param] == nil then
  94. params[param] = {}
  95. end
  96. if branch ~= '' and branches[branch] == nil then
  97. branches[branch] = #branches + 1
  98. table.insert(branches, branch)
  99. end
  100. if branch == '' then
  101. params[param][0] = v
  102. else
  103. params[param][branch] = v
  104. end
  105. end
  106. end
  107. end
  108. local result = {}
  109. for _, branch in ipairs(branches) do
  110. if type(branch) == 'string' then
  111. local tArgs = {}
  112. for param, bValue in pairs(params) do
  113. tArgs[param] = bValue[branch] or bValue[0]
  114. end
  115. table.insert(
  116. result,
  117. frame:expandTemplate {
  118. title = '切换显示',
  119. args = {
  120. [1] = branch,
  121. [2] = frame:expandTemplate {title = tTitle, args = tArgs},
  122. [3] = tag
  123. }
  124. }
  125. )
  126. end
  127. end
  128. return table.concat(result)
  129. end
  130. function module.button(frame)
  131. return initSpan('textToggleDisplayButtons', '切换显示按钮', frame)
  132. end
  133. function module.style(frame)
  134. return initSpan('textToggleDisplayStyle', '切换显示样式', frame)
  135. end
  136. function module.buttonStyle(frame)
  137. return initSpan('textToggleDisplayButtonsStyle', '切换显示按钮样式', frame)
  138. end
  139. return module