Module:KnightMiner/Sandbox

From AIOWiki
Jump to navigation Jump to search

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

local p = {}
p.airing = function( f )
    local args = f
    if f == mw.getCurrentFrame() and args[1] == nil then
        args = f:getParent().args
    else
        f = mw.getCurrentFrame()
    end
 
    local formatDate = function( var )
        return tonumber( f:callParserFunction( '#timel', 'U', var or '' ) )
    end
 
    -- Remove "Schedule:" from the date
    local schedule = args[1]:gsub( '^Schedule:', '' )
 
    local airing = {}
    -- If in the past and at most one week ago
    if ( formatDate( schedule ) <= formatDate() ) and ( formatDate() - formatDate( schedule ) < 604800 ) then  
        local episode = mw.title.new( args[1] ):getContent():gsub( '^.-([^]|[]+)%]%]$', '%1' )
 
        -- Split the title in the case of double episodes
        local episodes = mw.text.split( episode, ']] and [[', true )
 
        local number = f:expandTemplate{ title = 'ep num', args = { episodes[1] } }
            :gsub( '[abc]', '')
            :gsub( '^0*', '' )
 
        -- Transforms
        local episodeUrl = mw.text.trim( episode ):lower()
            :gsub( "[][,'.!:’?“”\"]", '' )    -- Remove punctuation 
            :gsub( ' ', '-' )                 -- Replace spaces with dashes
            :gsub( 'part%-([0-9]*)', 'pt%1' ) -- Switch to the most common form of parts

        local episodePart = mw.loadData( 'Module:Currently Airing/parts' )[episodeUrl]
        if not episodePart then
            episodePart = episodeUrl
        end
 
        table.insert( airing, '* [http://www.focusonthefamily.com/media/adventures-in-odyssey/episode-' .. number .. '-' .. episodePart )
        table.insert( airing, ' <span title="Listen Online!">' .. f:callParserFunction( '#time', 'F d, Y', schedule ) .. '</span>] &ndash; ' )    
        table.insert( airing, f:expandTemplate{ title = 'ep', args = { episodes[1], num = 'yes' } } )
 
        if episodes[2] then
            table.insert( airing, ' and ' .. f:expandTemplate{ title = 'ep', args = { episodes[2], num = 'yes' } } )
        end
        table.insert( airing, '\n' )
    end
    return table.concat( airing )
end
 
-- Standalone call
p.available = function( f )
    local data = f:callParserFunction( '#dpl', {
        'count = 200',
        namespace = 'Schedule',
        format = ',%PAGE%,====,',
        order = 'descending'
    } ):gsub( '====$', '' )
 
    local output = {}
 
    for name in mw.text.gsplit( data, '====' ) do
        table.insert( output, p.airing{ name } )
    end
 
    return table.concat( output )
end
 
return p