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 20: Line 20:
end
end
return "Invalid input"
return "Invalid input"
end

local function calculateAge(dateString)
local year = tonumber(dateString:match("%d+")) -- Extract the year from the date string
if year then
return currentYear - year .. " years old"
end
return nil
end
end


Line 72: Line 64:
end
end
elseif value:match("%d%d%d%d") then -- Check if the value is a date
elseif value:match("%d%d%d%d") then -- Check if the value is a date
local age = calculateAge(value)
-- Code for calculating age removed
if age then
convertedValue = value .. ' (' .. age .. ')'
end
end
end
elseif type(value) == "number" then
elseif type(value) == "number" then
local age = calculateAge(tostring(value))
-- Code for calculating age removed
if age then
convertedValue = tostring(value) .. ' (' .. age .. ')'
end
end
end

Revision as of 11:46, 15 April 2024

local p = {}
local currentYear = tonumber(os.date("%Y"))

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.dynamic(frame)
    local args = frame:getParent().args
    local output = '{| class="infobox biography vcard" style="width: 22em;"\n|-\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~=('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
                    elseif value:match("%d%d%d%d") then -- Check if the value is a date
                        -- Code for calculating age removed
                    end
                elseif type(value) == "number" then
                    -- Code for calculating age removed
                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