Module:Module sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
Tags: Mobile edit Mobile web edit Advanced mobile edit
No edit summary
Tags: Reverted Mobile edit Mobile web edit Advanced mobile edit
Line 51: Line 51:
for key, value in pairs(args) do
for key, value in pairs(args) do
if key ~= 'image' and key ~= 'imagecaption' and key ~= 'caption' and key ~= 'headcolor' and key ~= 'color' and key ~= 'abvstyle' and not key:match("^abv%d+$") and key~=abvParam then
if key ~= 'image' and key ~= 'imagecaption' and key ~= 'caption' and key ~= 'headcolor' and key ~= 'color' and key ~= 'abvstyle' and not key:match("^abv%d+$") then
if key ~= 'up' and not (key:sub(1, 2) == 'up' and firstDescriptionFound) then
if key ~= 'up' and not (key:sub(1, 2) == 'up' and firstDescriptionFound) then
local convertedValue = value
local convertedValue = value
Line 78: Line 78:


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


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

Revision as of 02:02, 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 = '{| class="infobox biography vcard" style="width: 22em;"\n'
    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 ~= 'caption' and key ~= 'headcolor' and key ~= 'color' and key ~= 'abvstyle' and not key:match("^abv%d+$") 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 = ""  
    end

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

return p