Slay the Spire Wiki
No edit summary
(Undo revision 15761 by Kassyndra (talk))
Line 196: Line 196:
 
end
 
end
 
 
  +
if(image ~= nil) then
  +
result = '[[File:'..image..'|link='..link..'|20x20px|middle]] '..result..''
  +
end
 
if(addTooltip == 'YES' or addTooltip == "TRUE") then
 
if(addTooltip == 'YES' or addTooltip == "TRUE") then
 
result = '<span class = "basic-tooltip" title="'..tooltip..'">'..result..'</span>'
 
result = '<span class = "basic-tooltip" title="'..tooltip..'">'..result..'</span>'

Revision as of 17:33, 4 February 2020

Documentation for this module may be created at Module:Keywords/doc

--Code for getting text for Keywords & Icons

local p = {}

local KeyData = mw.loadData( 'Module:Keywords/data')
local Shared = require( 'Module:Shared' )

function p.getIconValue(icon, valName, asString)
    if(asString == nil) then asString = false end
    
    if(valName == nil or valName == "") then
        return "ERROR: No value chosen"
    end
    
    local result = nil
    valName = string.upper(valName)
    if(valName == "NAME") then
        result = icon.Name
    elseif(valName == "CODE") then
        if(icon.Code ~= nil) then
            result = icon.Code
        else
            result = icon.Name
        end
    elseif(valName == "IMAGE") then
        result = icon.Image
    elseif(valName == "LINK") then
        result = icon.Link
    elseif(valName == "TEXT") then
        result = icon.Text
    elseif(valName == "TOOLTIP") then
        local name = p.getIconValue(icon, "NAME")
        local text = p.getIconValue(icon, "TEXT")
        result = ''
        if name ~= nil then
            result = name
        end
        if text ~= nil then
            if name ~= nil then result = result..':\\n' end
            result = result..text
        end
    end
    
    if(result == nil and asString) then
        return ''
    end
    return result
end

function p.getKeyValue(keyword, valName, asString)
    if(asString == nil) then asString = false end
    if(keyword == nil) then return nil end
    
    if(valName == nil or valName == "") then
        return "ERROR: No value chosen"
    end
    
    local result = nil
    valName = string.upper(valName)
    if(valName == "NAME") then
        return keyword.Name
    elseif(valName == "CODE") then
        if(keyword.Code ~= nil) then
            return keyword.Code
        else
            return keyword.Name
        end
    elseif(valName == "LINK") then
        if(keyword.Link ~= nil) then
            return keyword.Link
        else
            return keyword.Name
        end
    elseif(valName == "TEXT") then
        return keyword.Text
    elseif(valName == "IMAGE") then
        if(keyword.Image ~= nil) then
            return keyword.Image
        end
    elseif(valName == "TOOLTIP") then
        local name = p.getKeyValue(keyword, "NAME")
        local text = p.getKeyValue(keyword, "TEXT")
        result = ''
        if name ~= nil then
            result = name
        end
        if text ~= nil then
            if name ~= nil then result = result..':\\n' end
            result = result..text
        end
    end
    
    if(result == nil and asString) then
        return ''
    end
    return result
end

function p.Icon(frame)
    local iName = frame.args ~= nil and frame.args[1] or nil
    local addtext = frame.args ~= nil and frame.args[2] or nil
    local imgsize = frame.args ~= nil and frame.args.imgsize or nil
    local addtooltip = frame.args ~= nil and frame.args.tooltip or nil
    local text = frame.args ~= nil and frame.args.text or nil
    
    return p._Icon(iName, addtext, imgsize, addtooltip, text)
end

--Pulls an icon from the list of icons and displays it
--Adds a tooltip if available, and links to the appropriate page
function p._Icon(iconName, addtext, imgsize, addTooltip, overrideText)
    if(addtext == nil) then addtext = '' end
    addtext = string.upper(addtext)
    
    if(addTooltip == nil or addTooltip == '') then addTooltip = 'YES' 
    else addTooltip = string.upper(addTooltip) end
    
    imgsize = imgsize ~= nil and imgsize or 'x26'
                
    local result = ''
    for i, Icon in pairs(KeyData["Icons"]) do
        local code = p.getIconValue(Icon, "Code")
        local name = p.getIconValue(Icon, "Name")
        if(code == iconName or name == iconName) then
            local text = p.getIconValue(Icon, "Text")
            local image = p.getIconValue(Icon, "Image")
            local link = p.getIconValue(Icon, "Link")
            local tooltip = p.getIconValue(Icon, "Tooltip")
            
            result = '[[File:'..image..'|'..imgsize..'px'
            if(link ~= nil) then
                result = result..'|link='..link
            elseif(name ~= nil) then
                result = result..'|'..text
            end
            result = result..']]'
            
            if(addtext == 'YES' or addtext == 'TEXT') then
                if(link ~= nil) then
                    if(overrideText == nil or overrideText == '') then
                        result = result..' [['..link..'|'..name..']]'
                    else
                        result = result..' [['..link..'|'..overrideText..']]'
                    end
                elseif(name ~= nil) then
                    if(overrideText == nil or overrideText == '') then
                        result = result..' '..name
                    else
                        result = result..' '..overrideText
                    end
                end
            end
            
            if(addTooltip == 'YES' or addTooltip == "TRUE") then
                result = '<span class = "basic-tooltip" title="'..tooltip..'">'..result..'</span>'
            end
            
            return result
        end
    end
    return nil
end

function p.Keyword(frame)
    local keyname = frame.args ~= nil and frame.args[1] or nil
    local addtooltip = frame.args ~= nil and frame.args.tooltip or nil
    local text = frame.args ~= nil and frame.args.text or nil
    return p._Keyword(keyname, addtooltip, text)
end

function p._Keyword(keyname, addTooltip, overrideText)
    if(addTooltip == nil or addTooltip == '') then addTooltip = 'YES' 
    else addTooltip = string.upper(addTooltip) end
    local result = ''
    
    for i, Key in pairs(KeyData["Keywords"]) do
        local code = p.getKeyValue(Key, 'Code')
        local name = p.getKeyValue(Key, 'Name')
        if(code == keyname or name == keyname) then
            local link = p.getKeyValue(Key, 'Link')
            local tooltip = p.getKeyValue(Key, 'Tooltip')
            local image = p.getKeyValue(Key, 'Image')
            
            if(link ~= nil) then
                if(overrideText == nil or overrideText == '') then
                    result = '[['..link..'|'..name..']]'
                else
                    result = '[['..link..'|'..overrideText..']]'
                end
            else
                if(overrideText == nil or overrideText == '') then
                    result = name
                else
                    result = overrideText
                end
            end
            
            if(image ~= nil) then
                result = '[[File:'..image..'|link='..link..'|20x20px|middle]] '..result..''
            end
            if(addTooltip == 'YES' or addTooltip == "TRUE") then
                result = '<span class = "basic-tooltip" title="'..tooltip..'">'..result..'</span>'
            end
            
            return result
        end
    end
end

function p.FillIcons(text, addTooltip)
    local result = text
    for i, Icon in pairs(KeyData["Icons"]) do
        local code = p.getIconValue(Icon, "Code")
        if(Shared.contains(result, code)) then
            local newText = p._Icon(code, nil, nil, addTooltip)
            result = string.gsub(result, code, newText)
        end
    end
    
    return result
end

function p.FillKeywords(text, addTooltip)
    local result = text
    for i, Key in pairs(KeyData["Keywords"]) do
        local code = p.getKeyValue(Key, "Code")
        if(Shared.contains(result, code)) then
            local newText = p._Keyword(code, addTooltip)
            result = string.gsub(result, code:gsub('%-','%%-'), newText)
        end
    end
    
    return result
end

function p.ListKeywords(frame)
    local data = {}
    for i, Key in pairs(KeyData["Keywords"]) do
        local code = p.getKeyValue(Key, "Code")
        if(code ~= nil) then
            local text = p._Keyword(code)
            table.insert(data, code..' ('..text..')')
        end
    end

    table.sort(data)
    local result = 'All '..Shared.tableCount(data)..' keywords:\n'
    
    for i, str in pairs(data) do
        result = result..'\n* '..str
    end
    
    return result
end

function p.ListIcons(frame)
    local data = {}
    for i, Icon in pairs(KeyData["Icons"]) do
        local code = p.getIconValue(Icon, "Code")
        if(code ~= nil) then
            local text = p._Icon(code)
            table.insert(data, code..' ('..text..')')
        end
    end

    table.sort(data)
    local result = 'All '..Shared.tableCount(data)..' icons:\n'
    
    for i, str in pairs(data) do
        result = result..'\n* '..str
    end
    
    return result
end

return p