Module:Crosscolor

From Moegirlpedia
Revision as of 16:12, 12 December 2023 by LiaMinina (talk | contribs) (Created page with "-- Co-creator: User:サンムル local p = {} function p._main(args,toggle) local texts = {} local colors = {} for k, v in pairs(args) do if type(k) == 'string' then -...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Template-info.svg Module Documentation  [View] [Edit] [History] [Refresh]

This module is used to execute the template {{Crosscolor}}, please go to the template page to read about the specific usage.

-- Co-creator: User:サンムル
local p = {}

function p._main(args,toggle)
	local texts = {}
	local colors = {}
	
	for k, v in pairs(args) do
		if type(k) == 'string' then --If it's a string, then it matches a string starting with "c"
			local index = k:match "^c(%d+)$"
			if index then colors[tonumber(index)] = v end
		else --是数字
			texts[k] = v
		end
	end
	colors[0] = colors[#colors] -- Output the divisor field when the remainder is equal to 0
	
	--Rendering section
	local root = mw.html.create 'span'
	for i, v in ipairs(texts) do
		root:tag 'span'
			:wikitext(v)
			:css {color = colors[i%#colors],
				["data-color"] = toggle and color}
			:addClass(toggle and 'colorToggleBlock')
	end
	return root
end



function p.main ( frame )
	local wrappers = { ['Template:Crosscolor'] =  true, ['Template:交叉颜色护眼版'] = true }
	local toggle = frame.args["toggle"]
	local parent = frame:getParent()
	if parent and wrappers[parent:getTitle()] then
		frame = parent
	end
	local args = frame.args
	return p._main(args,toggle)
end

return p