2023年政策修订增补工作正在进行中,欢迎参与!
  • Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

Module:Lyrics/hover

萌娘百科,万物皆可萌的百科全书!转载请标注来源页面的网页链接,并声明引自萌娘百科。内容不可商用。
跳转到导航 跳转到搜索
Template-info.svg 模块文档  [创建] [刷新]
local module = {}

local getArgs = require('Module:Arguments').getArgs
local buildLyrics = require('Module:Lyrics')._lyrics
local initHandler = require('Module:HooksHandler').init

local preSplit = function (original, translated, customArgs)
	-- 旧的示例里面翻译部分也有加,但是实际上没有必要
	return original, mw.ustring.gsub(mw.ustring.gsub(translated, '#HoverWithNextLine', ''), '#NoHover', '')
end

local preParse = function (original, translated, customArgs)
	customArgs.noHover = {}
	customArgs.hoverWithNextLine = {}
	for idx, val in ipairs(original) do
		val = mw.ustring.gsub(val, '#NoHover', function()
			customArgs.noHover[idx] = true
			return ''
		end)
		original[idx] = mw.ustring.gsub(val, '#HoverWithNextLine', function()
			customArgs.hoverWithNextLine[idx] = true
			return ''
		end)
	end
end

local postParse = function (lines, customArgs)
	local index, all, add, start = 1, #lines, 0, nil
	local warp = function (_start)
		if not _start then _start = start end
		table.insert(lines, _start, '<div class="bg_hover">')
		table.insert(lines, index + 2, '</div>')
		index = index + 2
		add = add + 2
		start = nil
	end
	repeat
		if not customArgs.noHover[index - add] then
			if not customArgs.hoverWithNextLine[index - add] then
				warp(start or index)
			else
				start = start or index
			end
		elseif start then
			warp()
		end
		index = index + 1
	until index > all + add
	if start then warp() end
end

function module.initHooks(args, hooksHandler, customArgs)
	return hooksHandler({ preSplit = preSplit, preParse = preParse, postParse = postParse })
end

function module.main(frame)
	local args = getArgs(frame, {wrappers='Template:LyricsKai/colors/hover/Sandbox'})
	local hooksHandler, customArgs = initHandler(), {}
	local hookTrigger = module.initHooks(args, hooksHandler, customArgs)

	return buildLyrics(args, hookTrigger, customArgs)
end

return module