Module:Module sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Restored revision 1219021175 by Thisasia (talk)
Tags: Twinkle Undo Mobile edit Mobile web edit Advanced mobile edit
No edit summary
Tags: Reverted Mobile edit Mobile web edit Advanced mobile edit
Line 26: Line 26:
function p.infobox(frame)
function p.infobox(frame)
local args = frame:getParent().args
local args = frame:getParent().args
local output = '{| class="infobox biography vcard" style="width: 22em;"\n|-\n'
local output = '|'.. '<div style="display:none;">'..'{| class="infobox biography vcard" style="width: 22em;"\n|-\n'.. '</div>'
local firstDescriptionFound = false
local firstDescriptionFound = false
local cmProcessed = false
local cmProcessed = false

Revision as of 08:03, 15 April 2024

local p = {}

local function calculateAge(dateOfBirth)
end

local function cmToFeetAndInches(cm)
    local cmValue = tonumber(cm:match("%d+"))
    if cmValue then
        local totalInches = cmValue / 2.54
        local feet = math.floor(totalInches / 12)
        local inches = math.floor(totalInches % 12)
        return string.format("%d cm (%d feet %d inches)", cmValue, feet, inches)
    end
    return "Invalid input"
end

local function kgToPounds(kg)
    local kgValue = tonumber(kg:match("%d+"))
    if kgValue then
        local pounds = kgValue * 2.20462
        return string.format("%d kg (%.2f pounds)", kgValue, pounds)
    end
    return "Invalid input"
end

function p.infobox(frame)
    local args = frame:getParent().args
    local output = '|'.. '<div style="display:none;">'..'{| class="infobox biography vcard" style="width: 22em;"\n|-\n'.. '</div>'
    local firstDescriptionFound = false
    local cmProcessed = false
    local abvParams = {}  -- Store abv parameters separately

    for key, value in pairs(args) do
        if key:sub(1, 2) == 'up' then
            if value ~= "" then
                output = output .. '| style="font-size:20px;text-align: center; background-color: ' .. (args.headcolor or "#f2f2f2") .. '; color: ' .. (args.color or "inherit") .. '; font-weight: bold;" | ' .. value .. '\n|-\n'
                firstDescriptionFound = true
            end
        elseif key:sub(1, 3) == 'abv' then
            if value ~= "" then
                abvParams[value] = true  -- Store abv parameters in a table with value as key to avoid duplicates
            end
        end
    end
    
    if args.image then
        local caption = args.caption or ""
        local imagesize = args.imagesize or ""  
        output = output .. '|' ..'<div style="text-align:center;">'.. args.image.. '</div>'..'<small style="display: block; text-align: center;">' .. caption .. '</small>\n|-\n'
    end
    
    for key, value in pairs(args) do
        if key ~= 'image' and key ~= 'imagecaption' and key~=('abv'or'abv1'or'abv2'or' abv3'or'abv4'or'abv5'or'abv6'or'abv7'or'abv8'or' abv9'or'abv10'or'abv11') 
        and key ~= 'caption' and key ~= 'headcolor' and key ~= 'color' and key ~= 'abvstyle' and not key:match("^abv%d+$") and key~=abvParams then
            if key ~= 'up' and not (key:sub(1, 2) == 'up' and firstDescriptionFound) then
                local convertedValue = value
                if type(value) == "string" then
                    if value:match("%s*cm%s*") then
                        convertedValue = cmToFeetAndInches(value)
                        cmProcessed = true
                    elseif value:match("%s*kg%s*") or value:match("%s*kgs%s*") then
                        if cmProcessed then
                            convertedValue = cmToFeetAndInches(value) .. '\n' .. kgToPounds(value)
                        else
                            convertedValue = kgToPounds(value)
                        end
                    end
                elseif type(value) == "number" then
                    local age = calculateAge(tostring(value))
                    if age then
                        convertedValue = tostring(value) .. ' (' .. age .. ' years old)'
                    end
                end
                
                output = output .. '| <b>' .. key .. '</b>\n| ' .. tostring(convertedValue) .. '\n|-\n'
            end
        end
    end

    if not args.abvstyle then
        args.abvstyle = ""  -- Set default value for abvstyle if not provided
    end

    for abvParam, _ in pairs(abvParams) do
        output = output .. '| style="text-align:center;' .. args.abvstyle .. '" | ' .. abvParam .. '\n|-\n'
    end
    
    output = output .. '|}\n'
    
    return output
end

return p