Moduł:Wieki

Z Nonsensopedii, polskiej encyklopedii humoru

local tools = require('Moduł:Narzędzia')
local roman = require('Moduł:Rzymskie')
local p = {}

local function arrow(dir, double, size)
	size = size or 16
	local s
	if double then
		s = '[[Plik:Font Awesome 5 solid angle-double-'
	else
		s = '[[Plik:Font Awesome 5 solid angle-'
	end
	return s .. dir .. '.svg|x' .. size .. 'px|text-bottom|link=]]'
end

local function toRoman(num)
	if num == -2 then return 'starożytność|dawne dzieje' end
	if num == -3 then return 'prehistoria' end
	
	local s = roman._main({ math.abs(num) }) .. ' wiek'
	if num < 0 then
		s = s .. ' p.n.e.'
	end
	
	return s
end

local function centuryHeader(century)
	local prev = century - 1
	if prev == 0 then prev = -1
	elseif prev < -3 then prev = nil end
	
	local next = century + 1
	if next > 21 then next = nil
	elseif next == 0 then next = 1 end
	
	local s = '<div style="font-size: 1.2em;" class="nonsa-century-line">'
	if prev ~= nil then
		s = s .. '[[' .. toRoman(prev) .. ']] ' .. arrow('left', false, 19) .. ' '
	end
	s = s .. '<b>[[' .. toRoman(century) .. ']]</b>'
	if next ~= nil then
		s = s .. ' ' .. arrow('right', false, 19) .. ' [[' .. toRoman(next) .. ']]'
	end
	
	return s .. '</div>'
end

local function yearHeader(year)
	local s = ''
	
	-- w lewo
	for i = year-1,year-3,-1 do
		if i < 1901 then break end
		s = '[[' .. i .. ']] ' .. arrow('left') .. ' ' .. s 
	end
	
	s = ' ' .. arrow('left', true) .. ' ' .. s
	if year - 10 < 1901 then
		s = '[[Lata 1891–1900]]' .. s
	else
		s = '[[' .. (year - 10) .. ']]' .. s
	end
	
	-- w środek
	s = s .. '<b class="nonsa-hl-year">[[' .. year .. ']]</b>'
	
	-- w prawo
	local currentYear = os.date('*t')['year']
	for i = year+1,year+3 do
		if i > currentYear then break end
		s = s .. ' ' .. arrow('right') .. ' [[' .. i .. ']]'
	end
	
	local maxYear = year + 10
	if maxYear > currentYear then
		if year + 3 >= currentYear then maxYear = nil
		else maxYear = currentYear end
	end
	if maxYear ~= nil then
		s = s .. ' ' .. arrow('right', true) .. ' [[' .. maxYear .. ']]'
	end
	
	return s
end

function p.main(frame)
	local args = tools.getArgs(frame)
	local title = mw.title.getCurrentTitle().text
	local num = tonumber(title) or tonumber(args['rok'])
	local century
	
	if num ~= nil then century = math.ceil(num / 100)
	else century = tonumber(args['wiek']) or 20 end
	
	local s = centuryHeader(century)
	
	if num ~= nil then
		s = s .. yearHeader(num)
	end
	
	return '<div style="width: 100%; text-align: center;">' .. s .. '</div><hr/>'
end

return p