Module:Icon

From Predecessor Wiki
Jump to navigation Jump to search
Template-info.svg Documentation

Modified from the Mighty Party wiki's Icon module.


-- This module implements [[Template:Icon]].
local Icon = {}

local iconData = mw.loadData('Module:Icon/data').Icons
local getArgs = require('Module:Arguments').getArgs
local builder = require("Module:SimpleHTMLBuilder")

function Icon.getIcon(frame)
	local args = getArgs(frame)
	local data = iconData[(args['icon'] or args[1]):lower()]
	if data == nil then data = iconData['default'] end
	args['image'] = args['image'] or data.file or ''
	args['text'] = args['text'] or data.name or ''
	args['link'] = args['link'] or data.link or ''
	args['showlabel'] = args['showlabel'] or false
	local icon, img, label = Icon.createIcon(args)
	return icon
end

function Icon.createIcon(frame)
	local args = getArgs(frame)
	local icon = builder.create('span')
	icon:cssText('white-space: pre;')
	args['size'] = args['size'] or '15px'

	args['image'] = args['image'] or ''
	
	args['link'] = args['link'] or ''
	
    args['text'] = args['text'] or args['link'] or ''
    
    args['class'] = args['class'] or ''
    
    args['showlabel'] = args['showlabel'] or false
    if args['showlabel'] == 'true' or args['showlabel'] == '1' then args['showlabel'] = true end
    
	args['border'] = args['border'] or false
    if args['border'] == 'true' or args['border'] == '1' then args['border'] = true end
    
    local img = builder.create()
    if args['image'] ~= '' then
        img:wikitext(mw.ustring.format('[[File:%s%s%s%s|class=img-icon%s]]', 
            args['image'],
            '|'..args['size'],
            args['border'] == true and '|border' or '',
            args['link'] ~= '' and '|link='..args['link'] or '',
            args['class'] ~= '' and ' '..args['class'] or ''
            ))
    else
    	img = nil
    end
    
	local label = builder.create('span')
	if args['showlabel'] == true then
	    if args['text'] ~= '' then
	        if args['link'] == '' then
	            label:wikitext(args['text'])
	        elseif args['text'] == args['link'] then
	            label:wikitext(mw.ustring.format('[[%s]]', args['text']))
	        else
	            label:wikitext(mw.ustring.format('[[%s|%s]]', args['link'], args['text']))
	        end
	    end
    else
    	label = nil
	end

    if img then icon:node(img) end
    if img and label then icon:wikitext(' ') end
    if label then icon:node(label) end
    return icon, img, label
end

return Icon