Module:ColorsListHandler
From VRising
More actions
Documentation for this module may be created at Module:ColorsListHandler/doc
local p = {}
function p.capitalizeWords(str)
return str:gsub("(%a)([%w_']*)", function(first, rest)
return first:upper() .. rest:lower()
end)
end
function p.stableSort(tbl, cmp)
local size = #tbl
local indexedTbl = {}
-- Pair each element with its index
for i = 1, size do
indexedTbl[i] = {index = i, value = tbl[i]}
end
-- Define a comparison function that considers the original indices
local function stableCmp(a, b)
local aValue, bValue = a.value, b.value
local result = cmp(aValue, bValue)
if result == nil then
return a.index < b.index
else
return result
end
end
-- Sort the paired table
table.sort(indexedTbl, stableCmp)
-- Unpair the elements back into the original table
for i = 1, size do
tbl[i] = indexedTbl[i].value
end
end
function p.customSort(a, b)
local _, _, intA = a:match("([^,]+),%s*([^,]+),%s*(%d*)")
local _, _, intB = b:match("([^,]+),%s*([^,]+),%s*(%d*)")
-- Convert empty strings to a high value to keep them at the end
intA = intA and tonumber(intA) or math.huge
intB = intB and tonumber(intB) or math.huge
-- Sort by integer value, with empty strings staying at the end
if intA ~= intB then
return intA < intB
else
return false
end
end
function p.coloredSpans(frame)
local spans = {}
-- preset Keywords
local all = {"Red","Orange","Yellow","Green","Lime","Turquoise","Blue","Purple","Pink","White","Grey","Black"}
-- Process Colors
local str = frame.args[1] or ""
if str ~= "" then
local result = {}
for color in (str .. ","):gmatch("([^,]*)" .. ",") do
color = p.capitalizeWords(color)
if color == "All" then
for _, value in ipairs(all) do
table.insert(result, value)
end
else
table.insert(result, color)
end
end
for i, v in ipairs(result) do
if i > 1 then
table.insert(spans, ' / ')
end
table.insert(spans, string.format('<span style="color:%s"><span style="background-color: %s; height: 18.89px; width: 18.89px; border: 1px solid black; display: inline-block; vertical-align: sub;"></span> %s</span>', v, v, v))
end
end
-- Process Customs
local customs = frame.args[2] or ""
local sortedItem = {}
if customs ~= "" then
for item in customs:gmatch("%((.-)%)") do
table.insert(sortedItem, item)
end
p.stableSort(sortedItem, p.customSort)
for i, item in ipairs(sortedItem) do
local parts = {}
for part in item:gmatch("([^,]+)") do
table.insert(parts, part)
end
local name = p.capitalizeWords(parts[1])
local color = parts[2]
local index = tonumber(parts[3]) -- Convert index to number if present
if index and index > 0 then
-- Ensure the index is within bounds
if (index+1*(index-1)) > #spans then
-- If index is greater than the length of spans, append
if #spans > 0 then
table.insert(spans, ' / ')
end
table.insert(spans, string.format('<span style="color:%s"><span style="background-color: %s; height: 18.89px; width: 18.89px; border: 1px solid black; display: inline-block; vertical-align: sub;"></span> %s</span>', color, color, name))
else
-- Insert at the specified index
if index > 1 then
table.insert(spans, (index+1*(index-1)), string.format('<span style="color:%s"><span style="background-color: %s; height: 18.89px; width: 18.89px; border: 1px solid black; display: inline-block; vertical-align: sub;"></span> %s</span>', color, color, name))
if (index+1*(index-1)) > #spans then
table.insert(spans, (index+1*(index-1))-1,' / ')
else
table.insert(spans, (index+1*(index-1))+1,' / ')
end
else
table.insert(spans, 1, string.format('<span style="color:%s"><span style="background-color: %s; height: 18.89px; width: 18.89px; border: 1px solid black; display: inline-block; vertical-align: sub;"></span> %s</span>', color, color, name))
table.insert(spans, 2,' / ')
end
end
else
-- Append if no valid index is provided
if #spans > 0 then
table.insert(spans, ' / ')
end
table.insert(spans, string.format('<span style="color:%s"><span style="background-color: %s; height: 18.89px; width: 18.89px; border: 1px solid black; display: inline-block; vertical-align: sub;"></span> %s</span>', color, color, name))
end
end
end
return table.concat(spans, "")
end
return p