Module:Lyrics

来自BanG Dream! Wiki
Admin讨论 | 贡献2022年7月21日 (四) 00:03的版本
跳到导航 跳到搜索

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

local p = {}

local getArgs = require('Module:Arguments').getArgs
local lang = require('Module:Lang')

function p._lyrics(args, hookTrigger, customArgs)
	local original = args.original or ''
	local translated = args.translated or ''
	if hookTrigger then
		original, translated = hookTrigger('preSplit', original, translated, customArgs)
	end

	--mw.text.split的效率太低啦,我愿称之为shit
	local orig = {}
	local iter = mw.ustring.gmatch(original..'\n', '([^\n]*)\n')
	for val in iter do
		table.insert(orig, val)
	end
	local tran = {}
	local iter = mw.ustring.gmatch(translated..'\n', '([^\n]*)\n')
	for val in iter do
		table.insert(tran, val)
	end

	local llang = args.llang or 'ja'
	local rlang = args.rlang or 'zh'
	local lstyle = args.lstyle or ''
	local rstyle = args.rstyle or ''

	local html = mw.html.create('div')
					:addClass('Lyrics')
					:css('width', args.width)
					:cssText(args.containerstyle or '')

	if ((args['hasRuby'] or '') ~= '') or (not args.colorsMode and (args.original or ''):find('<ruby')) then
		html:addClass('Lyrics-has-ruby')
	else
		html:addClass('Lyrics-no-ruby')
	end

	local hastran = true
	if #tran == 1 and tran[1] == '' then
		hastran = false
	else
		html:addClass('Lyrics-has-translate')
	end

	if hookTrigger then
		hookTrigger('preParse', orig, hastran and tran or false, customArgs)
	end

	local lines = {}
	local len = math.max(#orig, #tran)
	for i=1, len do
		local line = mw.html.create('div'):addClass('Lyrics-line')
			:tag('div')
				:addClass('Lyrics-original')
				:cssText(lstyle)
				:node(lang.wrap(orig[i] or '', llang))
				:done()
		if hastran then
			line:tag('div')
				:addClass('Lyrics-translated')
				:cssText(rstyle)
				:node(lang.wrap(tran[i] or '', rlang))
				:done()
		end
		table.insert(lines, tostring(line))
	end

	if hookTrigger then
		hookTrigger('postParse', lines, customArgs)
	end
	html:node(table.concat(lines))
	-- Clear the floating
	html:tag('div'):cssText('clear:both')
	html = tostring(html)

	if hookTrigger then
		html = hookTrigger('preOutput', html, customArgs)
	end

	return html
end

function p.lyrics(frame)
	local args = getArgs(frame, {wrappers='Template:LyricsKai'})
	return p._lyrics(args)
end

return p