Description

This module is the Lua back-end for {{Character Avatar}}, to display character icons/images.


Usage

{{#invoke:CharacterAvatar|getAvatar|<parameter list>}}

For parameter details see Template:Character Avatar/doc


------
-- LUA script replacement for wikitext template
--
-- [[File:c-{{lc:{{{1}}}}}{{#if:{{{2|}}}
-- |{{#ifexist: File:c-{{lc:{{{1}}}-{{{2}}}.png}}
-- |-{{lc:{{{2}}}}}|}}|}}.png|x50px|{{{1}}}|link={{{1}}}]]
--
-- Parameters:
-- 1: Character Name
--      Separate multiple characters with dollar sign "$"
-- 2: Event / IP version
--
-- The list of available versions is kept in the module data
-- to avoid the expensive function "ifexist"
--
-- Plain version is returned if Event version is not in data.
------

local p = {}

local avatarData = mw.loadData( 'Module:CharacterAvatar/data' )
local util = require('Module:Utility')

function p.getAvatar(frame)
    local tArgs = util.getArgs(frame)

    local character = tArgs[1] or ""
    local eventName  = mw.ustring.lower(tArgs[2] or "")
    local size = tArgs[3]
    
    if not size or size == "" then
        size = "50"
    end

    character = character:gsub("^%s*(.-)%s*$", "%1") or ''
    eventName = eventName:gsub("{{{%d}}}", "") or ''
    eventName = eventName:gsub("^%s*(.-)%s*$", "%1") or ''

    local avatarFile = {}

	local t1Char = {
			"",
			"[[File:", "", "",
			"", "",
			".png|x", size, "px|",
			"",
			"|link=",
			"",
			"]]"
	}

    for cName in mw.text.gsplit(character, "%s*%$%s*") do
        if cName ~= "" then
            local charlow = cName:lower()
            t1Char[ 3] = "c-"
            t1Char[ 4] = charlow:gsub("[%.']", "")
                            :gsub("^(.-)%s*%((.+)%)%s*$", "%1-%2")
                            :gsub(" ", "_")
            t1Char[10] = cName
            t1Char[12] = cName

            if avatarData[eventName] and avatarData[eventName][charlow] then
            	t1Char[5] = "-"
                t1Char[6] = avatarData[eventName]["F"] or eventName
            elseif eventName == "side"  then
            	t1Char[5] = "-"
                t1Char[6] = "side"
            else
            	t1Char[5] = ""
            	t1Char[6] = ""
            end
        else
        	t1Char[ 3] = ""
        	t1Char[ 4] = "work_in_progress"
        	t1Char[ 5] = ""
        	t1Char[ 6] = ""
        	t1Char[10] = "Unknown"
        	t1Char[12] = "Work In Progress"
        end

		table.insert(avatarFile, table.concat(t1Char))
		t1Char[1] = " "
    end

    return table.concat(avatarFile)
end

return p

-- </nowiki>
-- [[Category:Lua Modules]]
Community content is available under CC-BY-SA unless otherwise noted.