Module:Color
Jump to navigation
Jump to search
This module implements {{Color}}.
-- <pre>
local p = {}
local aliases = {
bz = "buzz",
buzzword = "buzz",
action = "menu",
tutorial = "help",
old = "red",
new = "green",
freeze = "ice",
shatter = "ice",
frozen = "ice",
freezing = "ice",
frostbite = "ice",
frost = "ice",
frostburn = "ice",
shock = "electric",
zap = "electric",
burn = "fire",
blaze = "fire",
}
local colors = {
buzz = "text-buzz",
menu = "text-menu",
green = "text-green",
red = "text-red",
help = "text-help",
orange = "text-orange",
gold = "text-gold",
yellow = "text-yellow",
electric = "text-electric",
ice = "text-ice",
fire = "text-fire",
wind = "text-wind"
}
-- 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 text = ""
local link = args.link
local color = ""
local class = ""
-- Determine if the first argument is a hex code or keyword
if mw.ustring.match(args[1], "^#%x%x%x%x%x%x$") then
-- If it's a hex code, use it directly
color = args[1]
text = args[2] or args[1] -- Use the second argument as text if present, otherwise use the hex code
else
-- Otherwise, look up the keyword for the color class
if args[2] then
class = p._getKeywordColor(mw.ustring.lower(args[1]))
text = args[2]
else
class = p._searchTextForKeyword(mw.ustring.lower(args[1]))
text = args[1]
end
end
-- Handle link creation
if link ~= nil then
if link == "1" or link == 1 then
text = "[[" .. text .. "]]"
else
text = "[[" .. link .. "|" .. text .. "]]"
end
end
-- Output: either use direct hex color or a CSS class
if color ~= "" then
-- Apply hex color if present
if args.nobold then
return '<span style="color:' .. color .. ';">' .. text .. '</span>'
else
return '<span style="color:' .. color .. ';"><b>' .. text .. '</b></span>'
end
else
-- Apply CSS class if using a predefined keyword
if args.nobold then
return '<span class="' .. class .. '">' .. text .. '</span>'
else
return '<span class="' .. class .. '"><b>' .. text .. '</b></span>'
end
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
-- </pre>