Module:ColorText: Difference between revisions

From He is Coming Official Wiki
Benjiminio (talk | contribs)
m use mw.html
Benjiminio (talk | contribs)
m attempt to remove invoking requirement and stop odd formatting when multiple calls are used on the same line
Line 19: Line 19:
function p.main(frame)
function p.main(frame)
     local keyword = mw.text.trim(frame.args[1] or "")
     local keyword = mw.text.trim(frame.args[1] or "")
     if keyword == "" then return "" end
     if keyword == "" then
        return ""
    end


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


     local safeText = mw.text.nowiki(keyword)
     -- Create a span via MediaWiki's tag mechanism (more parser-safe than returning raw HTML)
 
    return frame:extensionTag{
    local span = mw.html.create('span')
        name = 'span',
    span:cssText(string.format('color:%s !important;', color))
        content = mw.text.nowiki(keyword),
    span:wikitext(safeText)
        attributes = {
 
            style = 'color:' .. color .. ' !important;'
     return tostring(span)
        }
     }
end
end



Revision as of 09:39, 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"

    -- Create a span via MediaWiki's tag mechanism (more parser-safe than returning raw HTML)
    return frame:extensionTag{
        name = 'span',
        content = mw.text.nowiki(keyword),
        attributes = {
            style = 'color:' .. color .. ' !important;'
        }
    }
end

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

return p