Module:NavJaor

Uut Wikipedia, de vrye encyklopedy
Nå navigaty springen Nå söken springen

Documentatie voor deze module kan aangemaakt worden op de volgende pagina: Module:NavJaor/doc

-- Module for navigation by year, decade and/or century
local p = {}

local BC = " v. Chr."
local AD = ""


-- local functions

local function epoch(year)
	if year > 0 then
		return AD
	else
		return BC
	end
end

local function yearInEpoch(year)
	if year <= 0 then
		year = -year + 1	-- 0 becomes 1 [BC]
	end
	return year
end

local function centuryInEpoch(year)
	return math.floor((yearInEpoch(year) - 1) / 100) + 1
end

-- returns 0 for the first decade of the epoch
local function decade(year)
	return math.floor(yearInEpoch(year) / 10)
end

-- e.g. "22 v. Chr."
local function yearString(year)
	return yearInEpoch(year) .. epoch(year)
end

-- e.g. "29-20 v. Chr."
local function decadeString(year)
	local decade = decade(year)
	local epoch = epoch(year)
	local r
	if epoch == BC then
		if decade == 0 then
			r = "9-1" .. epoch
		else
			r = decade .. "9-" .. decade .. "0" .. epoch
		end
	else
		if decade == 0 then
			r = "1-9" .. epoch
		else
			r = decade .. "0-" .. decade .. "9" .. epoch
		end
	end
	return r	
end

-- e.g. "1e eeuw v. Chr."
local function centuryString(year)
	return centuryInEpoch(year) .. "e eeuw" .. epoch(year)
end

local function bc(year)
	return -year + 1
end

local function link(title, label)
	if not label then
		return "[[" .. title .. "]]"
	else
		return "[[" .. title .. "|" .. label .. "]]"
	end
end

local function centuries(year)
	local r = "'''Eeuwen:''' "
	local first = -2
	local last = 2
	for co = first, last do
		local cs = centuryString(year + co * 100)
		if (co == first) then
			r = r .. link(cs, "<")
		elseif (co == last) then
			r = r .. link(cs, ">")
		elseif (co == 0) then
			r = r .. "'''" .. link(cs) .. "'''"
		else
			r = r .. link(cs)
		end
		if (co ~= last) then
			r = r .. " | "
		end
	end
	return r
end

local function decades(year)
	local r = "'''Decennia:''' "
	local first = -3
	local last = 3
	for offset = first, last do
		local f
		if (year >= bc(9) and year <= 9) and (offset == 1 or offset == -1) then
			f = 9
		else
			f = 10
		end
		local ds = decadeString(year + offset * f)
		if (offset == first) then
			r = r .. link(ds, "<")
		elseif (offset == last) then
			r = r .. link(ds, ">")
		elseif (offset == 0) then
			r = r .. "'''" .. link(ds) .. "'''"
		else
			r = r .. link(ds)
		end
		if (offset ~= last) then
			r = r .. " | "
		end
	end
	return r
end

local function years(year)
	local r = "'''Jaoren:''' "
	local first = -6;
	local last = 6;
	for i = first, last do
		local yearOffset = i
		local label
		if (i == first) then
			yearOffset = yearOffset - 4
			label = "<<"
		elseif (i == first + 1) then
			label = "<"
		elseif (i == last - 1) then
			label = ">"
		elseif (i == last) then
			yearOffset = yearOffset + 4
			label = ">>"
		end
		local ys = yearString(year + yearOffset);
		if (yearOffset == 0) then
			r = r .. "'''" .. link(ys, label) .. "'''"
		else
			r = r .. link(ys, label)
		end
		if (i ~= last) then
			r = r .. " | "
		end
	end
	return r
end

-- centuries + decades + years
local function cdy(year)
    return centuries(year) .. "\n\n" .. decades(year) .. "\n\n" .. years(year)
end

-- centuries + decades
local function cd(year)
    return centuries(year) .. "\n\n" .. decades(year)
end


local function yearNumber(yearStr)
    local i = mw.ustring.find(yearStr, BC, 1, true)
    local r
    if i then
    	r = bc(mw.ustring.sub(yearStr, 1, i))
    else
    	r = tonumber(yearStr)
    end
    return r
end


-- exported functions

-- centuries + decades
-- argument is a decade, e.g. "1390-1399" or a year
function p.cd(frame)
    local str = frame.args[1] or tostring(mw.title.getCurrentTitle())
    local i = mw.ustring.find(str, "-", 1, true)
    local yearStr
    if i then
    	yearStr = mw.ustring.sub(str, i + 1)
    else
    	yearStr = str
    end
    local yearNum = yearNumber(yearStr)
    return cd(yearNum)
end

-- centuries + decades + years
-- argument is a year, e.g. "22 v. Chr."
function p.cdy(frame)
    local yearStr = frame.args[1] or tostring(mw.title.getCurrentTitle())
    local yearNum = yearNumber(yearStr)
    return cdy(yearNum)
end

-- century e.g. "1e eeuw v. Chr."
-- argument is a year, e.g. "22 v. Chr."
function p.century(frame)
    local yearStr = frame.args[1] or tostring(mw.title.getCurrentTitle())
    local yearNum = yearNumber(yearStr)
    return centuryString(yearNum)
end

return p