Module:Icon

From Mighty Party Wiki
Jump to navigation Jump to search

-- This module implements [[Template:Icon]].
 
local data = mw.loadData('Module:Icon/data')
 
local iconSize = {
    ["small"]           = "12px",
    ["normal"]          = "24px",
    ["medium"]          = "36px",
    ["large"]           = "48px",
}
 
local p = {}
 
function p._main(args)
	local code = args.code or args[1]
	local iconData
	local size
	local result
	if code then
		code = code:match('^%s*(.-)%s*$'):lower() -- trim whitespace and put in lower case
		iconData = data[code]
	end
	if not iconData then
		iconData = data._DEFAULT
	end
    if args.size then
        size = tostring(args.size)
        if iconSize[size] ~= nil then
            size = iconSize[size]
        end
    else
        size = iconSize["normal"]
    end
    result = '';
    
    if args.align then
    	result = result .. '<div style="text-align: ' .. args.align .. ';">'
    end
    if not (args.icon and (args.icon ~= 'no' or args.icon ~= 'false')) then
        result = result .. string.format(
        	'[[File:%s%s|%s|link=%s]]',
        	iconData.image,
        	iconData.tooltip and '|' .. iconData.tooltip or '',
        	size,
        	iconData.link or iconData.tooltip
        	) 
	    if args.linebreak and (args.linebreak == 'yes' or args.linebreak == 'true') then 
	    	result = result .. '<br>'
	    end        
    end
    if args.link and (args.link == 'yes' or args.link == 'true') then
    	result = result .. ' '
        if iconData.link then
            result = result .. '[[' .. iconData.link .. '|' 
            if args.color then
            	result = result .. '<span style="color:' .. args.color .. '">'
            end
            result = result .. iconData.tooltip 
            if args.color then
            	result = result .. '</span>'
            end            
            result = result .. ']]'
        else
            result = result .. '[[' 
            if args.color then
            	result = result .. iconData.tooltip .. '|<span style="color:' .. args.color .. '">'
            end
            result = result .. iconData.tooltip 
            if args.color then
            	result = result .. '</span>'
            end            
            result = result .. ']]'
        end
	    if args.linebreak and (args.linebreak == 'yes' or args.linebreak == 'true') then 
	    	result = result .. '<br>'
	    end              
    end
    if args.text and (args.text == 'yes' or args.text == 'true') then
	    result = result .. ' '
	    if args.color then
        	result = result .. '<span style="color:' .. args.color .. '">'
        end
        result = result .. iconData.tooltip
	    if args.color then
        	result = result .. '</span>'
        end        
	    if args.linebreak and (args.linebreak == 'yes' or args.linebreak == 'true') then 
	    	result = result .. '<br>'
	    end              
    end
    if args.description and (args.description == 'yes' or args.description == 'true') then
        result = result .. ' ' .. mw.getCurrentFrame():preprocess(iconData.description)
	    if args.linebreak and (args.linebreak == 'yes' or args.linebreak == 'true') then 
	    	result = result .. '<br>'
	    end                
    end
	if args.linebreak and (args.linebreak == 'yes' or args.linebreak == 'true') then 
    	result = mw.ustring.sub(result, 1, mw.ustring.len(result)-4)
    end
    if args.align then
    	result = result .. '</div>'
    end
    return result
end
 
function p.main(frame)
	local args = {}
	for k, v in pairs(frame:getParent().args) do
		args[k] = v
	end
	return p._main(args)
end
 
function p.documentation()
	--sort the icon table
    keys = {}
    for k in pairs(data) do
        table.insert(keys, k)
    end
    table.sort(keys)	
	
    --skill table
    result = '=== Skill Icons ===\n{| class="wikitable sortable"\n!Icon\n!Code\n!Tooltip\n!Description\n'
    for k, t in ipairs(keys) do
        if data[t].category == 'skill' then
            result = result .. '|-\n'
            result = result .. "|[[File:" .. data[t].image .. "|25px]]" .. '\n'
            result = result .. "|<code>" .. t .. "</code>" .. '\n'
            if data[t].link then
                result = result .. "|[[" .. data[t].link .. "|" .. data[t].tooltip .. "]]" .. '\n'
            else    
                result = result .. "|[[" .. data[t].tooltip .. "]]" .. '\n'
            end
            result = result .. "|" .. data[t].description .. '\n'
        end
    end
    result = result .. "|}\n"
    --rarity table
    result = result .. '=== Basic Icons ===\n{| class="wikitable sortable"\n!Icon\n!Code\n!Tooltip\n!Description\n'
    for k, t in ipairs(keys) do
        if data[t].category == 'basic' then
            result = result .. '|-\n'
            result = result .. "|[[File:" .. data[t].image .. "|25px]]" .. '\n'
            result = result .. "|<code>" .. t .. "</code>" .. '\n'
            if data[t].link then
                result = result .. "|[[" .. data[t].link .. "|" .. data[t].tooltip .. "]]" .. '\n'
            else
                result = result .. "|[[" .. data[t].tooltip .. "]]" .. '\n'
            end
            result = result .. "|" .. data[t].description .. '\n'
        end
    end
    result = result .. "|}\n"    
    --rarity table
    result = result .. '=== Rarity Icons ===\n{| class="wikitable sortable"\n!Icon\n!Code\n!Tooltip\n!Description\n'
    for k, t in ipairs(keys) do
        if data[t].category == 'rarity' then
            result = result .. '|-\n'
            result = result .. "|[[File:" .. data[t].image .. "|25px]]" .. '\n'
            result = result .. "|<code>" .. t .. "</code>" .. '\n'
            if data[t].link then
                result = result .. "|[[" .. data[t].link .. "|" .. data[t].tooltip .. "]]" .. '\n'
            else
                result = result .. "|[[" .. data[t].tooltip .. "]]" .. '\n'
            end
            result = result .. "|" .. data[t].description .. '\n'
        end
    end
    result = result .. "|}\n"    
    --alignment table
    result = result .. '=== Alignment Icons ===\n{| class="wikitable sortable"\n!Icon\n!Code\n!Tooltip\n!Description\n'
    for k, t in ipairs(keys) do
        if data[t].category == 'alignment' then
            result = result .. '|-\n'
            result = result .. "|[[File:" .. data[t].image .. "|25px]]" .. '\n'
            result = result .. "|<code>" .. t .. "</code>" .. '\n'
            if data[t].link then
                result = result .. "|[[" .. data[t].link .. "|" .. data[t].tooltip .. "]]" .. '\n'
            else
                result = result .. "|[[" .. data[t].tooltip .. "]]" .. '\n'
            end
            result = result .. "|" .. data[t].description .. '\n'
        end
    end
    result = result .. "|}\n"    
    --type table
    result = result .. '=== Type Icons ===\n{| class="wikitable sortable"\n!Icon\n!Code\n!Tooltip\n!Description\n'
    for k, t in ipairs(keys) do
        if data[t].category == 'type' then
            result = result .. '|-\n'
            result = result .. "|[[File:" .. data[t].image .. "|25px]]" .. '\n'
            result = result .. "|<code>" .. t .. "</code>" .. '\n'
            if data[t].link then
                result = result .. "|[[" .. data[t].link .. "|" .. data[t].tooltip .. "]]" .. '\n'
            else
                result = result .. "|[[" .. data[t].tooltip .. "]]" .. '\n'
            end
            result = result .. "|" .. data[t].description .. '\n'
        end
    end
    result = result .. "|}\n"    
    --gender table
    result = result .. '=== Gender Icons ===\n{| class="wikitable sortable"\n!Icon\n!Code\n!Tooltip\n!Description\n'
    for k, t in ipairs(keys) do
        if data[t].category == 'gender' then
            result = result .. '|-\n'
            result = result .. "|[[File:" .. data[t].image .. "|25px]]" .. '\n'
            result = result .. "|<code>" .. t .. "</code>" .. '\n'
            if data[t].link then
                result = result .. "|[[" .. data[t].link .. "|" .. data[t].tooltip .. "]]" .. '\n'
            else
                result = result .. "|[[" .. data[t].tooltip .. "]]" .. '\n'
            end
            result = result .. "|" .. data[t].description .. '\n'
        end
    end
    result = result .. "|}\n"    
    --currency table
    result = result .. '=== Currency Icons ===\n{| class="wikitable sortable"\n!Icon\n!Code\n!Tooltip\n!Description\n'
    for k, t in ipairs(keys) do
        if data[t].category == 'currency' then
            result = result .. '|-\n'
            result = result .. "|[[File:" .. data[t].image .. "|25px]]" .. '\n'
            result = result .. "|<code>" .. t .. "</code>" .. '\n'
            if data[t].link then
                result = result .. "|[[" .. data[t].link .. "|" .. data[t].tooltip .. "]]" .. '\n'
            else
                result = result .. "|[[" .. data[t].tooltip .. "]]" .. '\n'
            end
            result = result .. "|" .. data[t].description .. '\n'
        end
    end
    result = result .. "|}\n"
    --uncategorized table
    result = result .. '=== uncategorized Icons ===\n{| class="wikitable sortable"\n!Icon\n!Code\n!Tooltip\n!Description\n'
    for k, t in ipairs(keys) do
        if data[t].category == '' then
            result = result .. '|-\n'
            result = result .. "|[[File:" .. data[t].image .. "|25px]]" .. '\n'
            result = result .. "|<code>" .. t .. "</code>" .. '\n'
            if data[t].link then
                result = result .. "|[[" .. data[t].link .. "|" .. data[t].tooltip .. "]]" .. '\n'
            else
                result = result .. "|[[" .. data[t].tooltip .. "]]" .. '\n'
            end
            result = result .. "|" .. data[t].description .. '\n'
        end
    end
    result = result .. "|}\n"         
    --alias table
    result = result .. '=== Icons aliases ===\n{| class="wikitable sortable"\n!Icon\n!Code\n!Tooltip\n!Description\n'
    for k, t in ipairs(keys) do
        if data[t].category == 'alias' then
            result = result .. '|-\n'
            result = result .. "|[[File:" .. data[t].image .. "|25px]]" .. '\n'
            result = result .. "|<code>" .. t .. "</code>" .. '\n'
            if data[t].link then
                result = result .. "|[[" .. data[t].link .. "|" .. data[t].tooltip .. "]]" .. '\n'
            else
                result = result .. "|[[" .. data[t].tooltip .. "]]" .. '\n'
            end
            result = result .. "|" .. data[t].description .. '\n'
        end
    end
    result = result .. "|}"       
    return result
end

function p.skillslist()
	--sort the icon table
    keys = {}
    for k in pairs(data) do
        table.insert(keys, k)
    end
    table.sort(keys)	
    
    --skill table
    result = '{| class="article-table sortable"\n!Icon\n!Skill\n!Description\n'
    for k, t in ipairs(keys) do
        if data[t].category == 'skill' then
            result = result .. '|-\n'
            result = result .. "|[[File:" .. data[t].image .. "|25px]]" .. '\n'
            if data[t].link then
                result = result .. "|[[" .. data[t].link .. "|" .. data[t].tooltip .. "]]" .. '\n'
            else
                result = result .. "|[[" .. data[t].tooltip .. "]]" .. '\n'
            end
            result = result .. "|" .. data[t].description .. '\n'
        end
    end
    result = result .. "|}"  
    return result
end
 
return p