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 80: Line 80:
if key ~= 'image' and key ~= 'imagecaption' and key ~= 'imgc' and key ~= 'imagesize' then
if key ~= 'image' and key ~= 'imagecaption' and key ~= 'imgc' and key ~= 'imagesize' then
if key ~= 'description' and not (key:sub(1, 11) == 'description' and firstDescriptionFound) then
if key ~= 'description' and not (key:sub(1, 11) == 'description' and firstDescriptionFound) then
local convertedValue = value
if type(value) == "string" then
if value:match("%s*cm%s*") then
convertedValue = cmToFeetAndInches(value)
elseif value:match("%s*kg%s*") or value:match("%s*kgs%s*") then
convertedValue = kgToPounds(value)
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'
elseif value and value ~= "" then -- Display the value alone even if there is no parameter
local convertedValue = value
local convertedValue = value
if type(value) == "string" then
if type(value) == "string" then

Revision as of 16:24, 14 April 2024

local p = {}

local function calculateAge(dateOfBirth)
    if not dateOfBirth then
        return nil
    end
    
    local year, month, day = dateOfBirth:match("(%d+)%D(%d+)%D(%d+)")
    
    if not year or not month or not day then
        return nil
    end
    
    local currentDate = os.date("*t")
    local birthDate = os.time({year = tonumber(year), month = tonumber(month), day = tonumber(day)})
    
    local age = currentDate.year - tonumber(year)
    if currentDate.month < tonumber(month) or (currentDate.month == tonumber(month) and currentDate.day < tonumber(day)) then
        age = age - 1
    end
    
    return age
end

local function cmToFeetAndInches(cm)
    if not cm then
        return nil
    end
    
    local cmValue = tonumber(cm:match("(%d+) *cm"))
    
    if not cmValue then
        return nil
    end
    
    local inches = cmValue / 2.54
    local feet = math.floor(inches / 12)
    local remainingInches = inches % 12
    
    return string.format("%.0fcm (%dft %.1fin)", cmValue, feet, remainingInches)
end

local function kgToPounds(kg)
    if not kg then
        return nil
    end
    
    local kgValue = tonumber(kg:match("(%d+) *kg")) or tonumber(kg:match("(%d+) *kgs"))
    
    if not kgValue then
        return nil
    end
    
    local pounds = kgValue * 2.20462
    
    return string.format("%.0fkg (%.1flb)", kgValue, pounds)
end

function p.infobox(frame)
    local args = frame:getParent().args
    local output = '{| class="infobox biography vcard" style="width: 22em;"\n'
    local firstDescriptionFound = false
    
    for key, value in pairs(args) do
        if key:sub(1, 11) == 'description' then
            if value ~= "" then
                output = output .. '| style="text-align: center; background-color: ' .. (args.descriptionbgcolor or "#f2f2f2") .. '; color: ' .. (args.descriptioncolor or "inherit") .. '; font-weight: bold;" | ' .. value .. '\n|-\n'
                firstDescriptionFound = true
            end
        end
    end
    
    if args.image then
        local imgc = args.imgc or ""
        local imagesize = args.imagesize or ""  
        output = output .. '|' ..'<div style="text-align:center;">'.. args.image.. '</div>'..'<small style="display: block; text-align: center;">' .. imgc .. '</small>\n|-\n'
    end
    
    for key, value in pairs(args) do
        if key ~= 'image' and key ~= 'imagecaption' and key ~= 'imgc' and key ~= 'imagesize' then
            if key ~= 'description' and not (key:sub(1, 11) == 'description' and firstDescriptionFound) then
                local convertedValue = value
                if type(value) == "string" then
                    if value:match("%s*cm%s*") then
                        convertedValue = cmToFeetAndInches(value)
                    elseif value:match("%s*kg%s*") or value:match("%s*kgs%s*") then
                        convertedValue = kgToPounds(value)
                    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'
            elseif value and value ~= "" then -- Display the value alone even if there is no parameter
                local convertedValue = value
                if type(value) == "string" then
                    if value:match("%s*cm%s*") then
                        convertedValue = cmToFeetAndInches(value)
                    elseif value:match("%s*kg%s*") or value:match("%s*kgs%s*") then
                        convertedValue = kgToPounds(value)
                    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
    
    output = output .. '|}\n'
    
    return output
end

return p