Module:Tracklist

From New Prairie Wiki
Revision as of 10:11, 15 July 2026 by Randombell (talk | contribs) (Created page with "local p = {} function p.main(frame) local args = frame:getParent().args local totalSeconds = 0 local output = '<table class="wikitable">\n<tr><th>#</th><th>Title</th><th>Length</th></tr>\n' local i = 1 while args['track' .. i] do local title = args['track' .. i] local length = args['length' .. i] or "0:00" -- Parse MM:SS into seconds local min, sec = length:match("(%d+):(%d+)") if min and sec then...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Tracklist/doc

local p = {}

function p.main(frame)
    local args = frame:getParent().args
    local totalSeconds = 0
    local output = '<table class="wikitable">\n<tr><th>#</th><th>Title</th><th>Length</th></tr>\n'
    
    local i = 1
    while args['track' .. i] do
        local title = args['track' .. i]
        local length = args['length' .. i] or "0:00"
        
        -- Parse MM:SS into seconds
        local min, sec = length:match("(%d+):(%d+)")
        if min and sec then
            totalSeconds = totalSeconds + (string.toint(min) * 60) + string.toint(sec)
        end
        
        output = output .. string.format("<tr><td>%d</td><td>%s</td><td>%s</td></tr>\n", i, title, length)
        i = i + 1
    end
    
    -- Format total seconds back to MM:SS or H:MM:SS
    local totalMin = math.floor(totalSeconds / 60)
    local totalSec = totalSeconds % 60
    local totalStr = string.format("%d:%02d", totalMin, totalSec)
    
    output = output .. string.format('<tr><th colspan="2">Total Length</th><th>%s</th></tr>\n</table>', totalStr)
    
    return output
end

-- Helper function to safely convert to integer
function string.toint(str)
    return tonumber(str) or 0
end

return p