模块:Lyrics/hover

来自维阿百科
跳转至: 导航搜索

此模块的文档可以在模块:Lyrics/hover/doc创建

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