Prijeđi na sadržaj

Modul:TV sezona

Izvor: Wikipedija


local p = {}

-- Provjeri postoji li stranica
local function pageExists(title)
    local result = mw.title.new(title)
    return result and result.exists
end

-- Potrazi clanak za prethodnu sezonu
function p.prethodna(frame)
    -- Get the full page title
    local title = mw.title.getCurrentTitle().text

    -- Extract the base name and number using pattern matching
    local base, number = string.match(title, '^(.-)%s*%((%d+)%.%s*sezona%)$')

    -- If match fails, return empty string to avoid errors
    if not base or not number then
        return ''
    end

    local prevNumber = tonumber(number) - 1
    if prevNumber < 1 then
        return '' -- No season 0 or negative
    end

    local linkTitle = string.format('%s (%d. sezona)', base, prevNumber)

    -- Check if the previous season's page exists
    if not pageExists(linkTitle) then
        return '' -- Return empty string if the page doesn't exist
    end

    local linkText = string.format('%d. sezona', prevNumber)
    return string.format('[[%s|%s]]', linkTitle, linkText)
end

-- Potrazi clanak za sljedecu sezonu
function p.sljedeca(frame)
    -- Get the full page title
    local title = mw.title.getCurrentTitle().text

    -- Extract the base name and number using pattern matching
    local base, number = string.match(title, '^(.-)%s*%((%d+)%.%s*sezona%)$')

    -- If match fails, return empty string to avoid errors
    if not base or not number then
        return ''
    end

    local nextNumber = tonumber(number) + 1

    local linkTitle = string.format('%s (%d. sezona)', base, nextNumber)

    -- Check if the next season's page exists
    if not pageExists(linkTitle) then
        return '' -- Return empty string if the page doesn't exist
    end

    local linkText = string.format('%d. sezona', nextNumber)
    return string.format('[[%s|%s]]', linkTitle, linkText)
end

-- New function to retrieve the current season's number (brojsezone)
function p.brojsezone(frame)
    -- Get the full page title
    local title = mw.title.getCurrentTitle().text

    -- Extract the base name and number using pattern matching
    local base, number = string.match(title, '^(.-)%s*%((%d+)%.%s*sezona%)$')

    -- If no match is found, return an empty string
    if not base or not number then
        return ''
    end

    -- If the season number was found, return it in the format "x. sezona"
    return string.format('%s. sezona', number)
end

-- New function for Popis epizoda (popisepizoda)
function p.popisepizoda(frame)
    -- Get the full page title
    local title = mw.title.getCurrentTitle().text

    -- Extract the base name from the page title (remove any bracket content)
    local base = string.match(title, '^(.-)%s*%b()')
    
    if not base then
        return ''  -- Return empty if no base name could be extracted
    end

    -- Define possible page titles to check
    local candidates = {
        string.format('Popis epizoda serije %s', base),
        string.format('Popis epizoda emisije %s', base),
        string.format('Dodatak:Popis epizoda serije %s', base),
        string.format('Dodatak:Popis epizoda emisije %s', base)
    }

    -- Iterate through candidates and return the first one that exists
    for _, candidate in ipairs(candidates) do
        if pageExists(candidate) then
            return string.format('[[%s|Popis epizoda]]', candidate)
        end
    end

    -- If none found, return empty string
    return ''
end
return p