Module:Module sandbox: Difference between revisions
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
Welcome to Module:Module sandbox, a Wikipedia module sandbox. This page is itself a module, and it allows you to carry out experiments related to module editing. If you wish to experiment with article editing, use the Wikipedia Sandbox or your own user sandbox.
To edit, click the edit tab above, make your changes and click the Publish changes button when finished. Please do not place malicious Lua code here, or copyrighted, offensive, illegal or libelous content in the sandboxes. For assistance with Lua coding, try the technical forum at the Village Pump. There are also several template sandboxes you can use to carry out experiments:
You can also conduct tests using the Wikipedia Sandbox. For instance, to invoke this module there, edit it so that it includes: {{#invoke:Module sandbox}} |
More info
Sandbox games |
Usage
{{#invoke:Module sandbox|function_name}}
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