Module:ColorText: Difference between revisions

From He is Coming Official Wiki
Benjiminio (talk | contribs)
mNo edit summary
Benjiminio (talk | contribs)
mNo edit summary
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:ColorText
-- Returns inline colored spans for keywords
local p = {}
local p = {}


-- Keyword-to-color mapping
local colors = {
local colors = {
     attack = "#e74444",
     attack = "#e74444",
Line 16: Line 13:
     riptide = "#5880cc",
     riptide = "#5880cc",
     stun = "#ffd569",
     stun = "#ffd569",
     thorns = "#8a961f"
     thorns = "#8a961f",
    thorn = "#8a961f"
}
}


-- Main function
function p.main(frame)
function p.main(frame)
     local keyword = frame.args[1] or ""
     local keyword = mw.text.trim(frame.args[1] or "")
     local color = colors[keyword:lower()] or "#888888" -- default gray
    if keyword == "" then return "" end
     return string.format('<span style="color:%s !important;">%s</span>', color, keyword)
 
     local color = colors[keyword:lower()] or "#888888"
 
    -- keep content as plain text, and remove any possible hidden breaks/control chars
    local safeText = mw.text.nowiki(keyword)
    safeText = safeText:gsub("[\r\n]+", " ")
    safeText = safeText:gsub("[%z\1-\31]", "") -- strip other control chars
    safeText = mw.text.trim(safeText)
 
     return string.format(
        '<span style="color:%s !important;">%s</span>',
        color,
        safeText
    )
end
 
function p._default(frame)
    return p.main(frame)
end
end


return p
return p

Latest revision as of 10:28, 15 July 2026

Documentation for this module may be created at Module:ColorText/doc

local p = {}

local colors = {
    attack = "#e74444",
    armor = "#4179ab",
    health = "#93c13e",
    speed = "#ffd569",
    acid = "#ccf61f",
    freeze = "#62abd4",
    poison = "#bbc44e",
    purity = "#62abd4",
    regeneration = "#9b4c51",
    riptide = "#5880cc",
    stun = "#ffd569",
    thorns = "#8a961f",
    thorn = "#8a961f"
}

function p.main(frame)
    local keyword = mw.text.trim(frame.args[1] or "")
    if keyword == "" then return "" end

    local color = colors[keyword:lower()] or "#888888"

    -- keep content as plain text, and remove any possible hidden breaks/control chars
    local safeText = mw.text.nowiki(keyword)
    safeText = safeText:gsub("[\r\n]+", " ")
    safeText = safeText:gsub("[%z\1-\31]", "") -- strip other control chars
    safeText = mw.text.trim(safeText)

    return string.format(
        '<span style="color:%s !important;">%s</span>',
        color,
        safeText
    )
end

function p._default(frame)
    return p.main(frame)
end

return p