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
    
    if(imgsize == nil  or imgsize == '') then
    imgsize = '20' 
    end
                
    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
            
		--Icon does not need tooltip IMO
            --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.KeywordTooltip(frame)
	local keyName = frame.args[1] or frame.args["keyword"] or nil
 
    return p._KeywordTooltip(keyName)
end

function p._Keyword(keyname, addTooltip, overrideText)
	if(keyname == nil or keyname == "") then return "ERROR: No keyword chosen" end
	
    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')
            local imageSize = '20px'
			if(name == 'Lightning' or name == 'Dark' or name == 'Frost' or name == 'Plasma') then
				imageSize = '25px'
			end
			
            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..'|'..imageSize..'|middle]] '..result..''
            end
            if(addTooltip == 'YES' or addTooltip == "TRUE") then
                result = '<span class = "keyword-tooltip" data-name="'..name..'">'..result..'</span>'
            end
            
            return result
        end
    end
end

function p._KeywordTooltip(keyname)
	if(keyname == nil or keyname == "") then return "ERROR: No keyword chosen" 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 text = p.getKeyValue(Key, 'Text')
            local image = p.getKeyValue(Key, 'Image')
            local imageSize = '40px'
			local windowSize = '320px'
			local cellSize = '10%'
			local padding = '10px 10px 10px 10px'
			if(name == 'Lightning' or name == 'Dark' or name == 'Frost' or name == 'Plasma') then
				imageSize = '80px'
				windowSize = '380px'
				cellSize = '80px'
				padding = '0px 0px 0px 0px'
			end
			
            if(image ~= nil) then
                result = '{| style="word-break:break-word; border: 2px solid #5959c0; background-color: #2C2A4E;  border-radius: 10px; width:'..windowSize..'; padding: 10px 10px 10px 10px"'
				result = result..'\n|rowspan="2" style="width:'..cellSize..'; padding: '..padding..'"|[[File:'..image..'|center|'..imageSize..']]'
				result = result..'\n|colspan="2" |<div style="font-size: 17px; font-weight:bold; font size: 17px">'..name
				result = result..'\n|-'
				result = result..'\n|colspan="2"|'..text
				result = result..'\n|-'
				result = result..'|}'
			else
				result = '{| style="word-break:break-word; border: 2px solid #5959c0; background-color: #2C2A4E;  border-radius: 10px; width: 300px; padding: 10px 10px 10px 10px"'
				result = result..'\n|<div style="font-size: 17px; font-weight:bold; font size: 17px">'..name
				result = result..'\n|-'
				result = result..'\n|'..text
				result = result..'\n|-'
				result = result..'|}'		
            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
Community content is available under CC-BY-SA unless otherwise noted.