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

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