FANDOM


local Icon = {}
 
-- Dependencies
local FN = require('Module:Filename')
local getArgs = require('Dev:Arguments').getArgs
 
local function wrap(func)
    return function(frame)
		local args = getArgs(frame, {
	        parentFirst = true,
	        removeBlanks = false,
	        trim = true,
	    })
		return (Icon[func](args))
    end
end
local function deHash(text)
    if not text then return end
    if type(text) == 'string' then
        local f = mw.ustring.find(text, '#', 1, true)
        if f then
            return mw.ustring.sub(text, 1, f-1), mw.ustring.sub(text, f+1)
        end
    end
    return text
end
local function pipeTrick(text)
    if not text then return end
    if type(text) == 'string' then
        text = mw.ustring.gsub(text, '^:?(.-)%s*%(..-%)', '%1')
        text = mw.ustring.gsub(text, '^[^:]*:(.-)', '%1')
    end
    return text
end
local function nilFormat(text, ...)
    if #arg then return nil end
    for i,v in ipairs(arg) do
        if not v then return nil end
    end
    return mw.ustring.format(text, ...)
end
local function empty(text)
    if text == '' then return nil end
    return text
end
 
Icon.basic = wrap('_create')
Icon.create = Icon.basic
function Icon._create(args)
    local display = empty(args['display']) or 'inline'
    local label = empty(args['label']) or 'after'
    local border = not not empty(args['border'])
    local separator = empty(args['separator'])
 
    local image = args['image'] or args[1]
    local link = args['link'] or args[2]
    local text = args['text'] or args[3]
 
    local selflink
    if link == '' then
        link = nil
    elseif link then
        local title, hash = deHash(link)
        selflink = not hash and mw.title.getCurrentTitle().prefixedText == title
        if not text then
            text = link
        elseif text == '' then
            text = pipeTrick(title)
        end
    end
    if not text or text == '' then
        text = nil
        label = nil
    end
 
    local tag, icon, icon_label, icon_image
    if display == 'inline' then
        tag = 'span'
        icon = mw.html.create(tag):addClass('infoicon inline-image')
    elseif args['display'] == 'inline-block' then
        tag = 'div'
        icon = mw.html.create(tag):addClass('infoicon inline-block-image'):css('display', 'inline-block')
    elseif args['display'] == 'block' then
        tag = 'div'
        icon = mw.html.create(tag):addClass('infoicon block-image')
    end
    if selflink then
        link = nil
        icon.addClass('selflink')
    end
 
 
    if args['class'] and args['class'] ~= '' then icon:addClass(args['class']) end
    if args['style'] and args['style'] ~= '' then icon:cssText(args['style']) end
 
    if label == 'before' or label == 'after' then
        icon_label = mw.html.create(tag):addClass('label-' .. label)
 
        if selflink then
            icon_label:tag('strong'):addClass('selflink'):wikitext(text)
        elseif not link then
            icon_label:wikitext(text)
        elseif link == text then
            icon_label:wikitext('[[' .. link .. ']]')
        else
            icon_label:wikitext('[[' .. link .. '|' .. text .. ']]')
        end
    end
    if image and image ~= '' then
        icon_image = mw.html.create(tag):addClass('icon')
 
        local size, width, height
        if args['size'] then
            size = mw.text.split(mw.ustring.gsub(tostring(args['size']), 'px$', ''), 'x')
        end
        width = tonumber(args['width'] or size and size[1])
        height = tonumber(args['height'] or size and size[2])
 
        if not width and not height then
            icon_image:addClass('dynamic-icon')
            size = '48x48px'
        elseif width and not height then
            size = width .. 'px'
        elseif not width and height then
            size = 'x' .. height .. 'px'
        else
            size = width .. 'x' .. height .. 'px'
        end
        if border then icon_image:addClass('border') end
 
        icon_image:wikitext(mw.ustring.format(
            '[[File:%s|%s|%slink=%s]]',
            image,
            size,
            border and 'border|' or '',
            link or ''
        ))
    end
 
    if icon_label and args['label'] == 'before' then
        if icon_label then icon:node(icon_label) end
        if separator and icon_image then icon:wikitext(separator) end
        if icon_image then icon:node(icon_image) end
    elseif icon_label and args['label'] == 'after' then
        if icon_image then icon:node(icon_image) end
        if separator and icon_image then icon:wikitext(separator) end
        if icon_label then icon:node(icon_label) end
    else
        if icon_image then icon:node(icon_image) end
    end
    return icon, icon_image, icon_label
end
 
return Icon

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.