Jump to content

Module:යුගය

From Wikisource

Module implementing the Wikisource-specific categorisation of years into literary "eras": Ancient, Medieval, Renaissance, Early modern, Modern, and Future. Its main user-accessible client is {{What era is}}, but the main use is for {{කර්තෘ}} (through Module:කර්තෘ) and its automatically added categories (the subcategories of Category:යුගය අනුව කතුවරු).

The named eras represent years as follows:

යුගය වසර
Ancient Before 600
Medieval 601–1420
Renaissance 1421–1630
Early modern 1631–1900
Modern 1901–2024
Future 2025–

Use

[edit]

From template code the function is called with {{#invoke:යුගය|main}} and the year parameter is extracted from the calling frame (the arguments given to the template). See {{What era is}} for an example. From a wikipage the year can be provided directly in the invocation: {{#invoke:යුගය|main|1623}} → Renaissance.

From a Lua module there is a simplified (and nominally faster) calling convention that avoids the need for having a frame object or checking the parent frame. After loading the module (local e = require Module:Era) you can call its .era() function with a year as the only parameter: local era = e.era(1623).

However called the return value is always a plain string with no markup or styling.


--[[
  Get the name of an era, based on Wikisource's definition.
  This implements the logic of Template:What era is
--]]

local getArgs = require('Module:Arguments').getArgs
local p = {}

function p.main(frame)
	local args = getArgs(frame, {
		wrappers = {
			'Template:What era is',
		}
	})
	return p.era(args[1])
end

function p.era(year)
	-- Nil years need to trigger bailout before we do anything else.
	if year == nil then
		return 'Unknown era'
	end

	--[[
		The template (What era is) treated every year that failed to parse as
		"Ancient", and years given as "BCE" fell into this category. With the
		module logic this no loonger works, so we need to treat BCE years as
		negative instead, so the numeric logic can deal wiith them.
	--]]
	if mw.ustring.match(year, ' BCE$') ~= nil then
		year = mw.ustring.gsub(year, ' BCE$', '') -- Strip the "BCE"
		year = '-' .. year -- And prepend a minus (we tonumber() it later)
	end

	-- Unknown value.
	if tonumber(year) == nil or mw.ustring.lower(year) == 'unknown' or year == '?' then
		return 'Unknown era'
	end

	-- Handle numeric years.
	year = tonumber(year)
	if year < 601 then
		return 'අතීත'
	elseif year < 1421 then
		return 'Medieval'
	elseif year < 1631 then
		return 'Renaissance'
	elseif year < 1900 then
		return 'Early modern'
	elseif year <= tonumber(os.date('%Y')) then
		return 'නූතන'
	else
		return 'Future'
	end
end


return p