Module:Infocard

From MB Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Infocard/doc

local p = {}
local getArgs = require('Module:Arguments').getArgs

-- Color definitions
local colors = {
    melee = '#d20000',
    warrior = '#d20000',
    ranged = '#21a08d',
    ranger = '#21a08d',
    magic = '#9d0d79',
    mage = '#9d0d79',
    summoning = '#066aff',
    summon = '#066aff',
    summoner = '#066aff',
    terraria = '#afcfe2',
    default = '#afcfe2',
    white = '#ffffff',
    orange = '#ffa500',
    red = '#ff0000',
    green = '#00ff00',
    blue = '#0000ff',
    purple = '#800080',
    pink = '#ffc0cb',
    lime = '#00ff00',
    yellow = '#ffff00',
    cyan = '#00ffff',
    brown = '#a52a2a'
}

function p.main(frame)
    local args = getArgs(frame)
    local output = {}
    
    -- Get basic parameters
    local name = args['name'] or mw.title.getCurrentTitle().text
    local subname = args['subname'] or ''
    local namenote = args['namenote'] or ''
    local type = args['type'] or ''
    local image = args['image'] or ''
    local icon = args['icon'] or ''
    local size = args['size'] or '150px'
    local theme = args['theme'] or 'default'
    local color = args['color'] or ''
    local class = args['class'] or ''
    local css = args['css'] or args['style'] or ''
    local id = args['id'] or ''
    
    -- Determine border color
    local borderColor = colors[theme:lower()] or colors[color:lower()] or colors[theme] or color or colors.default
    
    -- Start infocard div
    local infocardStyle = string.format('border-left: 5px solid %s; padding: 10px; margin: 10px 0; background: #f8f9fa; border: 1px solid #a2a9b1; border-radius: 4px; overflow: hidden; %s', borderColor, css)
    table.insert(output, string.format('<div class="infocard clearfix %s" style="%s" id="%s">', class, infocardStyle, id))
    
    -- Image/Icon section
    if image ~= '' or icon ~= '' then
        local mediaWidth = size
        if icon ~= '' and image == '' then
            mediaWidth = '32px' -- Smaller for icons
        end
        
        table.insert(output, string.format('<div class="infocard-media" style="float: left; margin-right: 15px; width: %s;">', mediaWidth))
        
        if image ~= '' then
            table.insert(output, string.format('[[File:%s|%s|link=]]', image, size))
        elseif icon ~= '' then
            table.insert(output, string.format('[[File:%s|32px|link=]]', icon))
        end
        
        table.insert(output, '</div>')
    end
    
    -- Header section
    table.insert(output, '<div class="infocard-header">')
    table.insert(output, '<div class="infocard-title">')
    table.insert(output, string.format('<span class="infocard-name">%s</span>', name))
    
    if namenote ~= '' then
        table.insert(output, string.format('<span class="infocard-namenote">(%s)</span>', namenote))
    end
    
    if subname ~= '' then
        table.insert(output, string.format('<div class="infocard-subname">%s</div>', subname))
    end
    
    if type ~= '' then
        table.insert(output, string.format('<div class="infocard-type">%s</div>', type))
    end
    
    table.insert(output, '</div></div>')
    
    -- Content section
    local marginLeft = '0'
    if image ~= '' or icon ~= '' then
        if image ~= '' then
            marginLeft = size
        else
            marginLeft = '50px' -- For icons
        end
    end
    
    table.insert(output, string.format('<div class="infocard-content" style="margin-left: %s; margin-top: 10px;">', marginLeft))
    
    -- Process all card parameters
    local rows = {}
    
    -- First, collect all card_ parameters
    for key, value in pairs(args) do
        local keyStr = tostring(key)
        if keyStr:match('^card_') then
            local rowKey = keyStr:gsub('^card_', '')
            local rowValue = args[rowKey]
            if rowValue and rowValue ~= '' then
                table.insert(rows, {
                    label = value,
                    value = rowValue,
                    key = rowKey
                })
            end
        end
    end
    
    -- Sort rows to maintain order (cards might be in random order due to Lua table)
    table.sort(rows, function(a, b)
        -- Extract numbers from keys if possible for natural sorting
        local numA = tonumber(a.key:match('%d+')) or 0
        local numB = tonumber(b.key:match('%d+')) or 0
        return numA < numB
    end)
    
    -- Output rows
    for _, row in ipairs(rows) do
        local rowHtml = string.format([[
            <div class="infocard-row" style="padding: 5px 0; border-bottom: 1px solid #eee; clear: both;">
                <div style="float: left; width: 120px; font-weight: bold; color: #555;">%s:</div>
                <div style="margin-left: 130px; color: #333;">%s</div>
            </div>
        ]], row.label, row.value)
        table.insert(output, rowHtml)
    end
    
    -- Close content and infocard divs
    table.insert(output, '</div></div>')
    
    return table.concat(output, '\n')
end

return p