Aller au contenu

Module:YouTube

Cette page fait l’objet d’une mesure de semi-protection étendue.
Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 25 mai 2023 à 00:50 et modifiée en dernier par Od1n (discuter | contributions) (ajout des formats « mm:ss » et « h:mm:ss » (non supportés par YouTube dans ses URL), que les utilisateurs préfèreront je pense). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.

 Documentation[voir] [modifier] [historique] [purger]

Ce module Lua est utilisé par le modèle {{YouTube}}.

Page à prévisualiser pour vérifier les modifications : Discussion module:YouTube/Test.

--[[
* format canonique : 1230s (tout en secondes)

* YouTube supporte davantage de formats, mais redirige vers le format canonique :
** il peut y avoir des leading zeroes
** heures, minutes, secondes peuvent être spécifiés dans n'importe quel ordre
** si on spécifie uniquement des secondes, le suffixe "s" peut être omis
]]

local p = {}

local function toSeconds( number, unit )
    if unit == 's' then
        return tonumber( number )
    elseif unit == 'm' then
        return number * 60
    elseif unit == 'h' then
        return number * 3600
    end
end

local function timestampToNbSeconds( timestamp )
    local num1, unit1, num2, unit2, num3, unit3

    -- "30s"
    num1, unit1 = timestamp:match( '^0*(%d+)([hms])$' )
    if num1 then
        return toSeconds( num1, unit1 )
    end

    -- "20m30s"
    num1, unit1, num2, unit2 = timestamp:match( '^0*(%d+)([hms])0*(%d+)([hms])$' )
    if num1 then
        return toSeconds( num1, unit1 ) + toSeconds( num2, unit2 )
    end

    -- "1h20m30s"
    num1, unit1, num2, unit2, num3, unit3 = timestamp:match( '^0*(%d+)([hms])0*(%d+)([hms])0*(%d+)([hms])$' )
    if num1 then
        return toSeconds( num1, unit1 ) + toSeconds( num2, unit2 ) + toSeconds( num3, unit3 )
    end

    local hours, minutes, seconds

    -- "30"
    seconds = timestamp:match( '^0*(%d+)$' )
    if seconds then
        return tonumber( seconds )
    end

    -- "20:30"
    minutes, seconds = timestamp:match( '^0*(%d+):0*(%d+)$' )
    if minutes then
        return minutes * 60 + seconds
    end

    -- "1:20:30"
    hours, minutes, seconds = timestamp:match( '^0*(%d+):0*(%d+):0*(%d+)$' )
    if hours then
        return hours * 3600 + minutes * 60 + seconds
    end

    return false
end

local function abbr( text, title )
    return '<abbr class="abbr" title="' .. title .. '">' .. text .. '</abbr>'
end

function p.timestamp( frame )
    local timestamp = frame.args[ 1 ]

    local nbSeconds = timestampToNbSeconds( timestamp )
    if not nbSeconds then
        -- fallback: return the original input
        return timestamp
    end

    return nbSeconds .. 's'
end

function p.libelle( frame )
    local timestamp = frame.args[ 1 ]

    local nbSeconds = timestampToNbSeconds( timestamp )
    if not nbSeconds then
        -- fallback: return the original input
        return timestamp
    end

    local parts = {}

    local hours = math.floor( nbSeconds / 3600 )
    if hours > 0 then
        parts[ #parts + 1 ] = hours .. '\194\160' .. abbr( 'h', 'heure' .. ( hours > 1 and 's' or '' ) )
        nbSeconds = nbSeconds - hours * 3600
    end

    local minutes = math.floor( nbSeconds / 60 )
    if minutes > 0 then
        parts[ #parts + 1 ] = minutes .. '\194\160' .. abbr( 'min', 'minute' .. ( minutes > 1 and 's' or '' ) )
        nbSeconds = nbSeconds - minutes * 60
    end

    if nbSeconds > 0 then
        parts[ #parts + 1 ] = nbSeconds .. '\194\160' .. abbr( 's', 'seconde' .. ( nbSeconds > 1 and 's' or '' ) )
    end

    return table.concat( parts, '\194\160' )
end

return p