模块:Lyrics/Roma

来自维阿百科
< 模块:Lyrics
霓虹灯鱼讨论 | 贡献2023年10月7日 (六) 23:26的版本 (创建页面,内容为“local p = {} local getArgs = require('Module:Arguments').getArgs local lang = require('Module:Lang') function p._lyrics(args, hookTrigger, customArgs) local ori…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

此模块的文档可以在模块:Lyrics/Roma/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 ''
	local photrans = args.photrans 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 phot = {}
	local iter = mw.ustring.gmatch(photrans..'\n', '([^\n]*)\n')
	for val in iter do
		table.insert(phot, 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 fold = args.fold or 'mw-collapsed'
 	if fold ~= 'on' then
 		fold = ''
 	end
	local lines = {}
	local len = math.max(#orig, #tran)
	local ruby = {}
 	local name = args.name or 'l'
	for i=1, #orig do
		if phot[i] == nil then 
			error("第" .. i .. "行日语歌词" .. '"' .. orig[i] .. '"' .. "没有对应的罗马音。请检查确保original参数和photrans参数的行数相等。")
		end
		local fuck = '<ruby><rb data-id="template-ruby">'..orig[i]..'<span class="template-ruby-hidden">(</span></rb><rt style="font-size:0.75em"><span class="mw-collapsible ' .. fold ..'mw-collapsed " id="mw-customcollapsible-'..name..'" style="">'..phot[i]..'</span></rt><span class="template-ruby-hidden">)</span></ruby>'
		table.insert( ruby , fuck )
	end
 
	for i=1, len do
		local line = mw.html.create('div'):addClass('Lyrics-line')
			:tag('div')
				:addClass('Lyrics-original')
				:cssText(lstyle)
				:node(lang.wrap(ruby[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
 
	local copyright = ''
	local frame = mw.getCurrentFrame()
	local LDC = frame:callParserFunction{ name = '#var', args = { 'LDC', '0' } }
	if LDC ~= '1' and args.override ~= '1' then
		copyright = '<small>本段落中所使用的歌词,其著作权属于' .. (args.author or '原著作权人') .. ',仅以介绍为目的引用。</small>'
	elseif LDC == '1' and args.override == '1' then
		copyright = frame:expandTemplate{ title = 'ac', args = { '不必要使用override参数的音乐条目' } }
	end
	local css = frame:extensionTag{ name = 'templatestyles', args = { src = 'Template:LyricsKai/styles.css' } }
 
	return copyright .. css .. html
end
 
function p.lyrics(frame)
	local args = getArgs(frame, {wrappers='Template:LyricsKai/Roma'})
	return p._lyrics(args)
end
 
return p