Jump to content

Module:Color: Difference between revisions

From The Petit Planet Wiki
ReisuDesign (talk | contribs)
Created page with "local p = {} local lib = require('Module:Feature') local aliases = { bz = 'buzz', buzzword = 'buzz', event = 'bp', action = 'menu', tutorial = 'help' } local colors = { buzz = 'text-buzz', bp = 'text-bp', menu = 'text-menu', new = 'text-new', old = 'text-old', help = 'text-help', citlali = 'text-citlali' } -- Main function for wiki usage. -- If 2 arguments are present, treats t..."
 
this SHOULD fix Uncommon getting picked up as Common.
 
(6 intermediate revisions by 2 users not shown)
Line 5: Line 5:
bz        = 'buzz',
bz        = 'buzz',
buzzword  = 'buzz',
buzzword  = 'buzz',
event     = 'bp',
neighbor  = 'highlight',
action    = 'menu',
neighbour = 'highlight',
tutorial  = 'help'
green     = 'new',
red      = 'old',
tutorial  = 'help',
item      = 'help',
wp        = 'whisper',
common    = 'common',
gray      = 'common',
uncommon  = 'uncommon',
blue      = 'uncommon',
rare      = 'rare',
purple    = 'rare',
stellar  = 'stellar',
gold      = 'stellar',
yellow    = 'stellar',
miracle  = 'miracle',
rainbow  = 'miracle'
}
}


local colors = {
local colors = {
    accent    = 'text-accent',
     buzz      = 'text-buzz',
     buzz      = 'text-buzz',
     bp        = 'text-bp',
     highlight  = 'text-highlight',
     menu      = 'text-menu',
     menu      = 'text-menu',
     new        = 'text-new',
     new        = 'text-new',
     old        = 'text-old',
     old        = 'text-old',
     help      = 'text-help',
     help      = 'text-help',
     citlali   = 'text-citlali'
     catch      = 'text-catch',
    whisper    = 'text-whisper',
    common    = 'text-common',
    uncommon  = 'text-uncommon',
    rare      = 'text-rare',
    stellar    = 'text-stellar',
    miracle   = 'text-miracle',
}
}


Line 53: Line 75:
end
end


if (args.nobold and lib.isNotEmpty(class)) then
if (args.no_bold and lib.isNotEmpty(class)) then
return '<span class="' .. class .. '">' .. text .. '</span>'
return '<span class="' .. class .. '">' .. text .. '</span>'
elseif (args.nobold and lib.isNotEmpty(span)) then
elseif (args.no_bold and lib.isNotEmpty(span)) then
return '<span style="color:' .. span .. '">' .. text .. '</span>'
return '<span style="color:' .. span .. '">' .. text .. '</span>'
elseif lib.isNotEmpty(span) then
elseif lib.isNotEmpty(span) then
Line 83: Line 105:
-- Helper method to search given text for the keys of given table t.
-- Helper method to search given text for the keys of given table t.
-- If a key is found, returns its value; returns nil otherwise.
-- If a key is found, returns its value; returns nil otherwise.
-- Checks longer keys first to avoid matching partial keywords (Uncommon getting detected as Common.)
local function searchTextForKeys(text, t)
local function searchTextForKeys(text, t)
for key, val in pairs(t) do
    local keys = {}
result = mw.ustring.find(text, key, 1, true)
 
        if result ~= nil then return val end
    for key in pairs(t) do
        keys[#keys + 1] = key
    end
 
    table.sort(keys, function(a, b)
        return #a > #b
    end)
 
    for _, key in ipairs(keys) do
        if mw.ustring.find(text, key, 1, true) ~= nil then
            return t[key]
        end
     end
     end
end
end

Latest revision as of 11:23, 24 October 2026

Module documentation

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

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

local aliases = {
	bz        = 'buzz',
	buzzword  = 'buzz',
	neighbor  = 'highlight',
	neighbour = 'highlight',
	green     = 'new',
	red       = 'old',
	tutorial  = 'help',
	item      = 'help',
	wp        = 'whisper',
	common    = 'common',
	gray      = 'common',
	uncommon  = 'uncommon',
	blue      = 'uncommon',
	rare      = 'rare',
	purple    = 'rare',
	stellar   = 'stellar',
	gold      = 'stellar',
	yellow    = 'stellar',
	miracle   = 'miracle',
	rainbow   = 'miracle'
}

local colors = {
    accent     = 'text-accent',
    buzz       = 'text-buzz',
    highlight  = 'text-highlight',
    menu       = 'text-menu',
    new        = 'text-new',
    old        = 'text-old',
    help       = 'text-help',
    catch      = 'text-catch',
    whisper    = 'text-whisper',
    common     = 'text-common',
    uncommon   = 'text-uncommon',
    rare       = 'text-rare',
    stellar    = 'text-stellar',
    miracle    = 'text-miracle',
}

-- 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.no_bold and lib.isNotEmpty(class)) then
		return '<span class="' .. class .. '">' .. text .. '</span>'
	elseif (args.no_bold 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.
-- Checks longer keys first to avoid matching partial keywords (Uncommon getting detected as Common.)
local function searchTextForKeys(text, t)
    local keys = {}

    for key in pairs(t) do
        keys[#keys + 1] = key
    end

    table.sort(keys, function(a, b)
        return #a > #b
    end)

    for _, key in ipairs(keys) do
        if mw.ustring.find(text, key, 1, true) ~= nil then
            return t[key]
        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