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

Module:Sandbox/C8H17OH/Lyrics/colors

贴贴♀百科,万娘皆可贴的百科全书!转载请标注来源页面的网页链接,并声明引自贴贴百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
  1. local module = {}
  2. local getArgs = require('Module:Arguments').getArgs
  3. local rainbow = require('Module:rainbow')._main
  4. local genCharaBlock = require('Module:Lyrics/colors/sub')._charaBlock
  5. local buildLyrics = require('Module:Lyrics')._lyrics
  6. local initHandler = require('Module:HooksHandler').init
  7. function module.parseArgs(colorsStr, charasStr, charaBlock)
  8. local colors = mw.text.split(mw.ustring.gsub(colorsStr, ';+$', ''), '%s*;+%s*')
  9. local aliases = {}
  10. for idx, val in ipairs(colors) do
  11. if mw.ustring.find(val, '=', 1, true) then
  12. colors[idx], aliases[idx] = mw.ustring.match(val, '^%s*([^=]+)%f[%s=]%s*=%s*(.+)%f[%s%z]%s*$')
  13. end
  14. end
  15. local charas = mw.text.split(mw.ustring.gsub(charasStr, ';+$', ''), '%s*;+%s*')
  16. for idx, val in ipairs(charas) do
  17. if charaBlock then
  18. charas[idx] = mw.ustring.gsub(val, '%(.+%)', '')
  19. end
  20. end
  21. local map_aliases = {}
  22. for idx, val in pairs(aliases) do
  23. map_aliases[val] = idx
  24. end
  25. for idx, val in pairs(charas) do
  26. map_aliases[val] = idx
  27. end
  28. return colors, map_aliases, charas
  29. end
  30. function module.parse(colors, charas, chorusName, sentence)
  31. local count_colors = #colors
  32. -- 包裹节点
  33. local chorus = '合唱'
  34. if #charas > count_colors then chorus = charas[#charas] end
  35. if chorusName ~='' then chorus = chorusName end
  36. for i=1, #sentence do
  37. local preParsed = mw.ustring.gsub(sentence[i], '(@%d%d?)([^@\n]+)\n-', function(m1, m2)
  38. if m1:len() > 2 and tonumber(m1:sub(2)) > count_colors then
  39. m1 = m1:sub(1, 2)
  40. m2 = m1:sub(3) .. m2
  41. end
  42. return mw.ustring.format('<*span title="%s" style="color:%s">【%s】%s<*/span>', m1, m1, m1, m2)
  43. end)
  44. if mw.ustring.find(sentence[i], '^@d+.-@.') or mw.ustring.find(sentence[i], '^[^@]') then
  45. preParsed = '<span class="NoTitlebox" title="'..chorus..'">'..preParsed..'</span>'
  46. end
  47. sentence[i] = preParsed
  48. end
  49. local mode = {
  50. lg = 'linear',
  51. rg = 'radial',
  52. rlg = 'repeating-linear',
  53. rrg = 'repeating-radial',
  54. rb = 'linear'
  55. }
  56. local special_color = {}
  57. for idx, val in ipairs(colors) do
  58. local match = val:match('^([lrc][lr]?[gbo])%(', 1)
  59. if match and (mode[match] or match == 'co') then
  60. special_color[idx] = match
  61. end
  62. end
  63. for i, material in ipairs(sentence) do
  64. local need_special = {}
  65. material = mw.ustring.gsub(material, 'color:@(%d+)', function (m)
  66. m = tonumber(m)
  67. if special_color[m] then need_special[special_color[m]] = true end
  68. return 'color:' .. (colors[m] or '')
  69. end)
  70. material = mw.ustring.gsub(material, 'title="@(%d+)"', function (m)
  71. m = tonumber(m)
  72. return charas[m] and ('title="' .. charas[m] ..'"') or ''
  73. end)
  74. material = mw.ustring.gsub(material, '【@(%d+)】', function (m)
  75. m = tonumber(m)
  76. return charas[m] and ('<span class="Lyrics-charatext" lang="zh">【' .. charas[m] ..'】</span>') or ''
  77. end)
  78. -- 渐变色实现
  79. for k, _ in pairs(need_special) do
  80. v = mode[k]
  81. if v then
  82. material = mw.ustring.gsub(material, '<%*span ([^>]-)style="color:'..k..'(%(.-%))">(.-)<%*/span>', function(s1, s2, s3)
  83. if k == 'rb' then
  84. local pattern = ''
  85. s2 = mw.ustring.gsub(s2, '%((.+)%)', '%1')
  86. local colors = mw.text.split(s2, ',', true)
  87. local quotient = 100 / #colors
  88. for i, v in ipairs(colors) do
  89. local color = mw.text.trim(v)
  90. if i == 1 then
  91. pattern = color..' '..quotient..'%'
  92. else
  93. pattern = pattern..', '..color..' '..(quotient * (i - 1))..'%'..', '..color..' '..(quotient * i)..'%'
  94. end
  95. end
  96. s2 = '('..pattern..')'
  97. end
  98. local style = 'background:-webkit-'..v..'-gradient'..s2..';-webkit-background-clip:text;-webkit-text-fill-color:transparent;-webkit-box-decoration-break:clone;'
  99. --[[
  100. s3 = mw.ustring.gsub(s3, '<rt%s*([^>]-)%s*>', function(s)
  101. if s ~= '' then
  102. local before, attr, after = mw.ustring.match(s, '^(.-style=")([^"]*)(".*)$')
  103. if attr ~= nil and mw.text.trim(attr) == '' then attr = nil end
  104. if attr ~= nil then
  105. s = before..attr..'; '..style..after
  106. else
  107. s = (before or s..' style="')..style..(after or '"')
  108. end
  109. else
  110. s = ' style="'..style..'"'
  111. end
  112. return '<rt '..s..'>'
  113. end)
  114. ]]--
  115. -- 额外套一层span,以免影响titlebox
  116. return '<span '..s1..'><span class="Lyrics-gradient" style="'..style..'">'..s3..'</span></span>'
  117. end)
  118. end
  119. end
  120. -- 交替色实现
  121. if need_special['co'] then
  122. material = mw.ustring.gsub(material, '<%*span ([^>]-)style="color:co%((.-)%)">(.-)<%*/span>',
  123. function(s1, s2, s3)
  124. local colors = mw.text.split(s2, ',')
  125. for idx, val in ipairs(colors) do
  126. colors[idx] = mw.text.trim(val)
  127. end
  128. return '<span '..s1..'>'..rainbow(colors, s3)..'</span>'
  129. end)
  130. end
  131. -- 整理节点
  132. sentence[i] = mw.ustring.gsub(mw.ustring.gsub(material, '<%*(/?span)', '<%1'), '@', '')
  133. end
  134. return sentence
  135. end
  136. local preSplit = function (original, translated, customArgs)
  137. local ce = {}
  138. local deal = function (text)
  139. text = mw.ustring.gsub(text, '%$([%d%$%[@])', function(s)
  140. if not ce[s] then ce[s] = mw.getCurrentFrame():expandTemplate{ title = 'ce', args = {s} } end
  141. return ce[s]
  142. end)
  143. text = mw.ustring.gsub(text, '@%[([^%[]-)%]', function(match) return '@' .. (customArgs.map_aliases[match] and customArgs.map_aliases[match] or '['..match..']') end)
  144. return text
  145. end
  146. return deal(original), deal(translated)
  147. end
  148. local preParse = function (original, translated, customArgs)
  149. original = module.parse(customArgs.colors, customArgs.charas, customArgs.chorusName, original)
  150. if customArgs.traColors and translated then
  151. translated = module.parse(customArgs.colors, customArgs.charas, customArgs.chorusName, translated)
  152. end
  153. end
  154. local preOutput = function (html, customArgs)
  155. if customArgs.doCharaBlock then
  156. html = '<p>' .. genCharaBlock(customArgs.genCharaBlock) .. '</p>' .. html
  157. end
  158. local css = mw.getCurrentFrame():extensionTag{ name = 'templatestyles', args = { src = 'Template:LyricsKai/colors/styles.css' } }
  159. return css .. html
  160. end
  161. function module.initHooks(args, hooksHandler, customArgs)
  162. customArgs.traColors = (args.traColors or '') == 'on'
  163. customArgs.doCharaBlock = (args.charaBlock or '') == 'on'
  164. customArgs.chorusName = args.chorusName or ''
  165. customArgs.colors, customArgs.map_aliases, customArgs.charas = module.parseArgs(args.colors or '', args.charas or '', customArgs.doCharaBlock)
  166. customArgs.genCharaBlock = { colors = args.colors or '', charas = args.charas or '', groupName = args.groupName, groupColor = args.lstyle }
  167. return hooksHandler({ preSplit = preSplit, preParse = preParse, preOutput = preOutput })
  168. end
  169. function module.main(frame)
  170. local args = getArgs(frame, { wrappers = 'User:C8H17OH/Sandbox/Template' })
  171. local hooksHandler, customArgs = initHandler(), {}
  172. local hookTrigger = module.initHooks(args, hooksHandler, customArgs)
  173. return buildLyrics(args, hookTrigger, customArgs)
  174. end
  175. return module