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 22:52, 18 April 2015 (Why are there two functions now?). 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 -- Why the minus three here? This could probably do with a comment.
				-- This could also be: if inputYear >= dataYear +3 then
				-- What I was trying to do is invert the order in which the data is accessed, regarding the British regnal year system.
				-- There, in British regnal years, the data starts with the last ruler and ends with the first. And it is read that way.
				-- With the Olympiads I have to go the opposite way, because my data starts with the 1st Olympiad and ends with the last.
				-- That means I need to make sure that the input years - 496, - 495, - 494 and - 493 all address the data of - 496
				-- Then - 492, - 491, - 490 and - 489 need to fetch the data from - 492 and so on.
				-- To this purpose both formats proposed above should be the same.
			-- 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.
				--
				-- Not quite sure what you're trying to do here.
				-- First, mw.ustring.format is exactly the same as string.format,
				-- so you may as well use that.
				-- Second, string.format works like this:
				-- string.format('foo %s baz %d', 'bar', 42) -- gives "foo bar baz 42"
				-- Third, once you fix string.format so that the ref tags display,
				-- you'll find that they aren't automatically converted to
				-- references. That's because Scribunto output isn't automatically
				-- preprocessed. You'll need to use something like
				-- frame:extensionTag to do it properly (it's under "frame
				-- objects" in the Scribunto manual).
					-- This sounds complicated. Before I answer I have to do some testing to see if I understood it right.
					-- Thanks for the explanations on your discussion page. I think I can manage this part, if I only get the script running.
				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 )
			-- the following line gives an error now: Lua error in Module:Ancient_Olympiads at line 68: bad argument #1 to 'getArgs' (table expected, got number).
				-- You use this function from a template, not from Lua. You
				-- would need to create a template with the content:
				--   {{#invoke:Ancient Olympiads|main}}
				-- Then you call the function from that template. For example,
				-- if you named your template "Template:Foo", then you would
				-- call it with this code:
				--   {{foo|-776}}
					-- I must admit that I understand nothing here. What are these arguments? What should they do?
					-- The previous script had only one function p.main( frame) at the start.
					-- But now there are two functions and only the 2nd is using ( frame ) while the first is with ( inputYear ).
					-- What's the difference and the specifity? Which one is using the other?
					-- Why did you postpone this function and have the first one work with ( inputYear )?
					-- Is it possible that this second function can be deleted all together?
					-- I don't know why I should create another module:Arguments when British regnal year did without.
					-- Probably these questions make as little sense to you as your answers make to me.
					-- Please consider that I have not the slightest idea of programming in Lua.
					-- I don't even understand the vocabulary, but I'm really good at working round what others did before.
					-- That's how I learned to programme in Javascript in 2001: I had taken over a financial website from another programmer. 
					-- When he left it was still small with few functions, but I made it really huge and complex to awe everybody.
					-- I still couldn't do anything from scratch. I have to move with trial and error.
					-- All I need is a script that's runnning, but this is currently not.
	local args = require( 'Module:Arguments' ).getArgs( frame, {
		parentOnly = true
	} )
	return p._main( args[ 1 ] )
end

return p