Module:ColorText: Difference between revisions

From He is Coming Official Wiki
Benjiminio (talk | contribs)
mNo edit summary
Benjiminio (talk | contribs)
mNo edit summary
Line 23: Line 23:
     local color = colors[keyword:lower()] or "#888888"
     local color = colors[keyword:lower()] or "#888888"


     -- nowiki the text but keep it inline/compact
     -- prevent line breaks / whitespace from becoming paragraph separators
    keyword = keyword:gsub("[\r\n]+", " ")
 
     local safeText = mw.text.nowiki(keyword)
     local safeText = mw.text.nowiki(keyword)


     local out = mw.html.create('span')
     local span = mw.html.create('span')
     out:cssText(string.format('color:%s !important;', color))
     span:cssText(string.format('color:%s !important;', color))
     out:wikitext(safeText)
     span:wikitext(safeText)


    -- ensure the returned HTML contains no newlines/outer whitespace
     local s = tostring(span)              -- make sure it's a string
     local s = out:tag('span')
     s = s:gsub("[\r\n]+", " ")         -- remove any newlines just in case
     s = s:gsub("[\r\n]+", " "):match("^%s*(.-)%s*$")
    s = s:match("^%s*(.-)%s*$")         -- trim outer whitespace
     return s
     return s
end
end

Revision as of 10:16, 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"

    -- prevent line breaks / whitespace from becoming paragraph separators
    keyword = keyword:gsub("[\r\n]+", " ")

    local safeText = mw.text.nowiki(keyword)

    local span = mw.html.create('span')
    span:cssText(string.format('color:%s !important;', color))
    span:wikitext(safeText)

    local s = tostring(span)              -- make sure it's a string
    s = s:gsub("[\r\n]+", " ")          -- remove any newlines just in case
    s = s:match("^%s*(.-)%s*$")         -- trim outer whitespace
    return s
end

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

return p