Module:Lyrics/colors/sub

From Moegirlpedia
< Module:Lyrics‎ | colors
Revision as of 15:21, 18 November 2023 by LiaMinina (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Template-info.svg Module Documentation  [Create] [Refresh]
local module = {}

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

function module._charaBlock(args)
	local colors = mw.text.split(mw.ustring.gsub(args.colors, ';+$', ''), ';')
	for i, v in ipairs(colors) do
		colors[i] = mw.ustring.match(v, '^%s*([^=]+)%f[%s=%z]') -- %f边界判断
	end
	local charas = mw.text.split(mw.ustring.gsub(args.charas, ';+$', ''), ';')
	local groupColor = args.groupColor or ''
	local hasGroupColor = false
	
	for k,v in ipairs(charas)do
		if v == 'chorus' then
			charas[k] = v .. '(@nolink)'
			break
		end
	end
	
	if string.find(groupColor, '^color:([^;]+)') then
		table.insert(colors, string.match(groupColor, '^color:([^;]+)'))
		hasGroupColor = true
	end

	if string.find(charas[#colors] or '', '^%s*$') then
		charas[#colors] = 'chorus(@nolink)'
	end

	if hasGroupColor and charas[#colors]:find('%(@nolink%)$') == nil then
		charas[#colors] = charas[#colors]..'(@nolink)'
	end

	if args['groupName'] then
		charas[#colors] = args['groupName']..'(@nolink)'
	end

	local product = {}
	for i, v in ipairs(colors) do
		v = string.gsub(v, '\n', '')
		repeat
			local ban = {
			    'lg%(.+%)', 'rg%(.+%)', 'rlg%(.+%)', 'rrg%(.+%)', 'co%(.+%)', 'rb%(.+%)', '^%s*$'
			}
			local flag = false
			for index, ptn in ipairs(ban) do
				if string.find(v, ptn) then
					flag = true
					break
				end
			end
			if flag then break end
			local charaName = '&nbsp;'..string.gsub(charas[i], '(.+)%(.+%)', '%1')
			local arg = string.gsub(charas[i], '.+%((.-)%)', '%1')
			local block = mw.html.create('span')
				:css('display', 'inline-block')
				:css('color', v)
				:tag('span'):css({
					width = '10px',
					height = '10px',
					background = v,
					display = 'inline-block'
				})
				:done():wikitext(charaName)
			:allDone()

			function link(link, block)
				return '[['..link..'|'..tostring(block)..']]'
			end

			if arg == '@nolink' then
				table.insert(product, tostring(block))
			elseif arg:find('^@link:.+') then
				table.insert(product, link(arg:match('^@link:(.+)'), block))
			else
				table.insert(product, link(charas[i], block))
			end
		until true
	end

	local productWiki = ''
	for i, v in ipairs(product) do
		productWiki = productWiki..v..'<span style="cursor:default; -webkit-user-select:none; -moz-user-select:none; -ms-user-select:none; -o-user-select:none; user-select:none;">&emsp;</span>'
	end

	return productWiki
end

function module.charaBlock(frame)
	local args = getArgs(frame)
	return module._charaBlock(args)
end

return module