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

Module:MultiLyrics

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

local lang = require('Module:Lang')

function p._lyrics(args)
	local html = mw.html.create("table")
	html:addClass("Lyrics")
	local maxn = 0
	for i = 1, table.maxn(args) do
		if maxn < table.maxn(args[i]) then
			maxn = table.maxn(args[i])
		end
	end
	local width = 100 * (1 / table.maxn(args))
	for i = 1, maxn do
		local line = mw.html.create("tr")
		line:addClass("Lyrics-line")
		line:css("height", "1.75em") -- 防止由于内容为空字符串导致表格单元格萎缩,指定单元格高度为固定值1.75个文字高度。
		for j = 1, table.maxn(args) do
			local lrc = mw.html.create("td")
			if j == 1 then
				lrc:addClass('Lyrics-original')
			else
				lrc:addClass('Lyrics-translated')
			end
			lrc:cssText("width: " .. width .. "%")
			local cont = lang.wrap(mw.text.trim(args[j][i] or ""), args[j].lang)
			if args[j].mstyle then
				local mspan = mw.html.create("span")
				mspan:node(cont)
				mspan:cssText(args[j].mstyle)
				mspan:addClass('mobileonly')
				local span = mw.html.create("span")
				span:node(cont)
				span:cssText(args[j].style)
				span:addClass('nomobile')
				lrc:node(span):node(mspan)
			else
				local span = mw.html.create("span")
				span:node(cont)
				span:cssText(args[j].style)
				lrc:node(span)
			end
			line:node(lrc)
		end
		html:node(line)
	end
	html:allDone()
	local css = mw.getCurrentFrame():extensionTag{ name = "templatestyles", args = { src = "Template:MultiLyrics/style.css" } }
	return css .. tostring(html)
end

function p.lyrics(frame)
	local args = frame:getParent().args
	local normargs = {}
	if mw.ustring.match(args[1], "^-%s*$") then
		local no = 0
		local sno = 1
		for i, k in ipairs(args) do
			if mw.ustring.match(args[i], "^-%s*$") then
				no = no + 1
				sno = 1
				normargs[no] = {}
			else
				if sno <= 3 then
					local style = mw.ustring.match(args[i], "^-style%s+(.+)$")
					local lang = mw.ustring.match(args[i], "^-lang%s+(.+)$")
					local mstyle = mw.ustring.match(args[i], "^-mobilestyle%s+(.+)$")
					if style then
						normargs[no].style = style
					elseif mstyle then
						normargs[no].mstyle = mstyle
					elseif lang then
						normargs[no].lang = mw.ustring.gsub(lang, "%s", "")
					else
						table.insert(normargs[no], args[i])
					end
				else
					table.insert(normargs[no], args[i])
				end
				sno = sno + 1
			end
		end
	else
		for i = 1, table.maxn(args) do
			local lyric = mw.text.split(args[i], "\n")
			normargs[i] = {}
			for j = 1, table.maxn(lyric) do
				if j <= 3 then
					local style = mw.ustring.match(lyric[j], "^-style%s+(.+)$")
					local lang = mw.ustring.match(lyric[j], "^-lang%s+(.+)$")
					local mstyle = mw.ustring.match(args[i], "^-mobilestyle%s+(.+)$")
					if style then
						normargs[i].style = style
					elseif mstyle then
						normargs[no].mstyle = mstyle
					elseif lang then
						normargs[i].lang = lang
					else
						table.insert(normargs[i], lyric[j])
					end
				else
					table.insert(normargs[i], lyric[j])
				end
			end
		end
	end
	-- mw.log(mw.dumpObject(normargs))
	return p._lyrics(normargs)
end

--[[
function p.test_html()
	local a = {}
	for i = 1, 3 do
		a[i] = {}
		a[i].lang="zh"
		a[i].style="color: red"
		a[i].mstyle="color: green"
		for j = 1, 5 do
			a[i][j]="Hello"
		end
	end
	return p._lyrics(a)
end

function p.test_args()
	local a = {}
	a.args = {
		"-",
		"-lang zh",
		"-style color: red;",
		"-mobilestyle color: yellow;",
		"世界,你好!",
		"歌词",
		"第三行",
		"-",
		"-lang en",
		"-style color: blue;",
		"-mobilestyle color: green;",
		"Hello, world!",
		"......",
		"3rd line",
	}
	a.getParent = function(self)
		return self
	end
	return p.lyrics(a)
end
]]

return p