Module:Template style

From bg3.wiki
Jump to navigation Jump to search

This implements Template:Template style. The provided CSS text is hashed, and the hash value used to construct a unique variable name. That variable is then used, via Extension:Variables, to check/store whether the given CSS was already added to the page or not.


local p = {}

-- Allows other Lua modules to apply some style.
function p.apply(frame, style)
	local hash = mw.hash.hashValue("xxh3", style)
	local v = "TemplateStyleApplied-" .. hash
	local check = frame:callParserFunction("#var", v, "0")
	if check == "0" then
		-- Add a no-op like "p{}" at the start to ensure that we don't end up
		-- doing something like {{#css:/*foo*/...}} if the user-provided CSS
		-- begins with a comment, because {{#css:/...}} would be interpreted as
		-- a filename by Extension:CSS.
		frame:callParserFunction("#css", "p{}\n" .. style)
		frame:callParserFunction("#vardefine", v, "1")
	end
end

-- Implements [[Template:Template style]].
function p.main(frame)
	p.apply(frame, frame:getParent().args[1])
end

return p