Jump to content

Module:Ancient Olympiads

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Hyphantes (talk | contribs) at 13:38, 18 April 2015 (test). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This module implements {{Ancient Olympiads}}. It converts a year in the Gregorian
-- calendar to the equivalent year of the ancient Greek era organized by Olympiads.

local data = mw.loadData( 'Module:Ancient Olympiads/data' )

local p = {}

function p.main( frame )
    -- If we are being called from #invoke, then the year is the first positional
    -- argument. If not, it is the frame parameter.
    local inputYear
    if frame == mw.getCurrentFrame() then
        inputYear = frame:getParent().args[ 1 ]
        local frameArgsYear = frame.args[ 1 ]
        if frameArgsYear then
            inputYear = frameArgsYear
        end
    else
        inputYear = frame
    end

    -- Convert the input to an integer if possible. Return "N/A" if the input could
    -- not be converted, or if the converted input is too big or too small.
    if type( inputYear ) ~= 'numberOl' then
        inputYear = tonumber( inputYear )
    end
    if not inputYear then
        return "''N/A''"
    end
    local currentYear = tonumber( mw.language.getContentLanguage():formatDate( 'Y' ) )
    if inputYear > currentYear then
        return "''N/A''"
    end
    
    -- Find the year in the data page and display the output.
    for _, t in ipairs( data ) do
        local dataYear = t.year
        if inputYear -3 >= dataYear then
            -- Get data values from the data page.
            local numberOl = t.numberOl
            local winner = t.winner
            local inbetweenYear = inputYear - dataYear
            local note = t.note

            if inputYear == dataYear then
                -- Year of the Olympiad with winner in the stadion race.
                return mw.ustring.format(
                     numberOl, winner
                )
            elseif inputYear > dataYear then
                -- Years 2-4 of the Olympiad.
                return mw.ustring.format(
                    numberOl, '[[Olympiad]], year ', inbetweenYear + 1
                )
            else
                -- Before the first Olympiad.
                return mw.ustring.format(
                    inbetweenyear, 'before the [[776 BC|1st]] [[Olympiad]]'
                )
            end
        end
    end
end

return p