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 Mr. Stradivarius (talk | contribs) at 15:42, 18 April 2015 (type( foo ) returns the variable type, which in Scribunto can only be "string", "number", "table", "boolean", "nil" or "function"; also, we can probably get away with only making one if statement here). 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 lang = mw.language.getContentLanguage()

local p = {}

function p._main( inputYear )
    -- 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.
	inputYear = tonumber( inputYear )
    if not inputYear or inputYear > tonumber( lang:formatDate( 'Y' ) ) 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, '|victor]]<ref>winner of the [[stadion race]]</ref>' or ''
                )
            elseif inputYear > dataYear then
                -- Years 2-4 of the Olympiad.
                return mw.ustring.format(
                    numberOl, '[[Olympiad]], year ', inbetweenYear + 1 or ''
                )
            else
                -- Before the first Olympiad.
                return mw.ustring.format(
                    inbetweenyear, 'before the [[776 BC|1st]] [[Olympiad]]' or ''
                )
            end
		end
    end
end

function p.main( frame )
	local args = require( 'Module:Arguments' ).getArgs( frame, {
		parentOnly = true
	} )
	return p._main( args[ 1 ] )
end

return p