Module:Color: Difference between revisions

From Honkai: Nexus Anima Wiki
Jump to navigation Jump to search
Created page with "local p = {} local lib = require('Module:Feature') local aliases = { } local colors = { new = 'text-new', old = 'text-old' } -- 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..."
 
No edit summary
Line 7: Line 7:
local colors = {
local colors = {
     new        = 'text-new',
     new        = 'text-new',
     old        = 'text-old'
     old        = 'text-old',
    buzz      = 'text-buzz',
    menu      = 'text-menu',
    help      = 'text-help',
}
}



Revision as of 17:50, 30 August 2025

This module implements {{Color}}.


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

local aliases = {
}

local colors = {
    new        = 'text-new',
    old        = 'text-old',
    buzz       = 'text-buzz',
    menu       = 'text-menu',
    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