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:43, 18 April 2015 (we usually indent with tabs, so convert all the spaces to tabs). 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