Genshin Impact Wiki
Genshin Impact Wiki
Genshin Impact Wiki
Documentation icon Module documentation

This is the module is used by {{Color}}.

local p = {}
local lib = require('Module:Feature')

local aliases = {
	wind      = 'anemo',
	earth     = 'geo',
	lightning = 'electro',
	shock     = 'electro',
	nature    = 'dendro',
	wood      = 'dendro',
	mushroom  = 'dendro',
	water     = 'hydro',
	wet       = 'hydro',
	fire      = 'pyro',
	burn      = 'pyro',
	frost     = 'cryo',
	ice       = 'cryo',
	cold      = 'cryo',
	frozen    = 'cryo',
	freeze    = 'cryo',
	bz        = 'buzz',
	buzzword  = 'buzz',
	event     = 'bp',
	action    = 'menu',
	tutorial  = 'help'
}

local colors = {
    anemo    = 'text-anemo',
    geo      = 'text-geo',
    electro  = 'text-electro',
    dendro   = 'text-dendro',
    hydro    = 'text-hydro',
    pyro     = 'text-pyro',
    cryo     = 'text-cryo',
    physical = 'text-physical',
    pneuma   = 'text-pneuma',
    ousia    = 'text-ousia',
    buzz     = 'text-buzz',
    bp       = 'text-bp',
    menu     = 'text-menu',
    new      = 'text-new',
    old      = 'text-old',
    help     = 'text-help'
}

-- Main function for wiki usage.
-- If 2 arguments are present, treats the first one as a keyword.
-- If only 1 argument is present, searches it for keywords.
-- Returns the color associated with the keyword, or "inherit" if not found.
function p.main(frame)
    local args = require('Module:Arguments').getArgs(frame)
	local class = ''
	local span = ''
	local text = ''
	local link = args.link
    -- choose variant based on number of arguments
	if args[2] then
		if args[1]:find('#') then
			span = args[1]
		else
			class = p._getKeywordColor(mw.ustring.lower(args[1]))
		end
		text = args[2]
	else
		class = p._searchTextForKeyword(mw.ustring.lower(args[1]))
		text = args[1]
	end

	if link ~= nil then
		if link == '1' or link == 1 then
			text = '[[' .. text .. ']]'
		else
			text = '[[' .. link .. '|' .. text .. ']]'
		end
	else
		text = '' .. text .. ''
	end

	if (args.nobold and lib.isNotEmpty(class)) then
		return '<span class="' .. class .. '">' .. text .. '</span>'
	elseif (args.nobold and lib.isNotEmpty(span)) then
		return '<span style="color:' .. span .. '">' .. text .. '</span>'
	elseif lib.isNotEmpty(span) then
		return '<span style="color:' .. span .. '"><b>' .. text .. '</b></span>'
	else
		return '<span class="' .. class .. '"><b>' .. text .. '</b></span>'
	end

end

-- Library functions usable in other modules

-- Returns the color associated with given keyword,
-- or "inherit" if input is not a keyword.
-- Runs output through nowiki by default,
-- unless noescape is specified to be true.
-- (input must be in lower case.)
function p._getKeywordColor(input, noescape)
    local element = aliases[input] or input
    local color = colors[element]
    if noescape then
    	return color or 'inherit'
	end
    return color and mw.text.nowiki(color) or 'inherit'
end

-- Helper method to search given text for the keys of given table t.
-- If a key is found, returns its value; returns nil otherwise.
local function searchTextForKeys(text, t)
	for key, val in pairs(t) do
		result = mw.ustring.find(text, key, 1, true)
        if result ~= nil then return val end
    end
end

-- Searches given text for keywords and returns the associated color,
-- or "inherit" if no keyword is found.
-- (text must be in lower case.)
function p._searchTextForKeyword(text)
	-- try elements first
	local color = searchTextForKeys(text, colors)
    if color ~= nil then
        return mw.text.nowiki(color)
    end
    -- try aliases afterwards
    local keyword = searchTextForKeys(text, aliases)
    if keyword ~= nil then
    	return mw.text.nowiki(colors[keyword])
	end

    return 'inherit'
end

return p