Module:Utils

local p = {}

local function nocase(s) return string.gsub(       s,        "%a",        function(c)            return string.format("[%s%s]", string.lower(c), string.upper(c))        end    ) end

local loreColors = { grail = "fe6150", moth = "f2e9c2", lantern = "ffe300", forge = "ff8e3e", heart = "fe7e8b", edge = "d7dd49", winter = "beeeff", knock = "b54efc", secrethistories = "fe43f0" }

function p.aspectColors return { grail = "fe6150", moth = "f2e9c2", lantern = "ffe300", forge = "ff8e3e", heart = "fe7e8b", edge = "d7dd49", winter = "beeeff", knock = "b54efc", secrethistories = "fe43f0" } end

function p.infoboxAspects(aspects) local aspectsData = mw.loadData("Module:Aspects/data")

local t = {}

for aspect, value in pairs(aspects) do       if loreColors[aspect] then table.insert(               t,                "' .. aspectsData[aspect].label .. "  " .. value            ) else if value == 1 then table.insert(t, "" .. aspectsData[aspect].label .. "") else table.insert(t, "" .. aspectsData[aspect].label .. " " .. value) end end end

return table.concat(t, " ") end

function p.renderAspects(aspects) local aspectsData = mw.loadData("Module:Aspects/data")

local t = {}

for aspect, value in pairs(aspects) do       if loreColors[aspect] then table.insert(               t,                "' .. aspectsData[aspect].label .. "  " .. value            ) else if value == 1 then table.insert(t, "" .. aspectsData[aspect].label .. "") else table.insert(t, "" .. aspectsData[aspect].label .. " " .. value) end end end

return table.concat(t, ", ") end

function p.singleAspect(aspect) local aspectsData = mw.loadData("Module:Aspects/data") local text if loreColors[aspect] then text = "' .. aspectsData[aspect].label .. " " else text = "" .. aspectsData[aspect].label .. "" end return text end

function p.addReferenceLinks(text) local references = mw.loadData("Module:Books/referenceData")

for _, item in ipairs(references) do       local link = item.link local reference = item.reference:gsub("-", ".") text = text:gsub('([%s\'"])(' .. nocase(reference) .. ')([%s\'",.:;])', "%1%2%3") text = text:gsub("^(" .. nocase(reference) .. ')([%s\'".,])', "%1%2")       text = text:gsub('(%s\'")(' .. nocase(reference) .. ")$", "%1%2") text = text:gsub("(%[%^[%*)%[%^[%*|([^[%]]*)]]([^%]]*]])", "%1%2%3") end

return text end

function p.renderQuote(text, source) return string.format(       ,        p.addReferenceLinks(text),        source    ) end

function p.findByLabel(map, label) for id, it in pairs(map) do       if it.label == label then return id, it       end end return nil, nil end

function p.listContainsItem(list, item) for _, it in pairs(list) do       if it == item then return true end end return false end

function p.startsWith(str, start) return str:sub(1, #start) == start end

return p