Sari la conținut

Modul:Categories by date

Documentația acestui modul poate fi creată la Modul:Categories by date/doc

local p = {}

local getArgs = require('Modul:Arguments').getArgs
local yesno = require('Modul:Yesno')
local ordinal = require('Modul:Ordinal')._ordinal

local function pad_number(n, pad)
	n = tostring(n)
	return string.rep('0', pad - string.len(n)) .. n
end

function p._categories_by_date(args)
	local testing = yesno(args.testing) or mw.title.getCurrentTitle().fullText == 'Format:Categorii după dată/teste'
	
	local caption = args.descriere or '{{{descriere}}}'
	local name = args.nume or '{{{nume}}}'
	local supercatname = args.supercatname
	
	local year = tonumber(args.an)
	local decade = tonumber(args.deceniu)
	local century = tonumber(args.secol) or 0
	
	local era_suffix = ''
	args.bce = args.era == 'î.e.n.' or args.era == 'î.Hr.' or yesno(args.ien) == true
	if args.bce then
		era_suffix = ' î.e.n.'
	end
	
	local century_intertext = ' '
	if yesno(args['cratima-secol']) then
		century_intertext = '-'
	end
	local supercat_century_intertext = ' '
	if yesno(args['cratima-secol-supercat']) then
		supercat_century_intertext = '-'
	end
	
	local year_num = 100 * century + 10 * (decade or 0) + (year or 0)
	local decade_num = 100 * century + 10 * (decade or 0)
	local century_num = 100 * century
	local century_suffix_text = ordinal(century + 1, false, 'm')
	local century_suffix_sup = ordinal(century + 1, true, 'm')
	
	local millennium = math.floor(century/10)
	local millennium_num = millennium * 1000
	local millennium_suffix_text = ordinal(millennium + 1, false, 'm')
	local millennium_suffix_sup = ordinal(millennium + 1, true, 'm')
	
	local caption_text = caption .. ' în '
	local parent_category = args['categorie-părinte']
	local parent_category_display = args['afișaj-categorie-părinte'] or parent_category
	local by_date_category = args['categorie-după-dată']
	local supercat
	local nearby_categories = {}
	
	local parent_sortkey
	local by_date_sortkey
	if not args.bce then
		parent_sortkey = ' ' .. year_num
		by_date_sortkey = pad_number(year_num, 4)
	else
		parent_sortkey = ' -' .. year_num
		by_date_sortkey = '-' .. pad_number(year_num, 4)
	end
	
	if year then
		caption_text = caption_text .. 'anul ' .. year_num
		
		parent_category = parent_category or name .. ' din anii ' .. decade_num .. era_suffix
		parent_category_display = parent_category_display or parent_category
		
		by_date_category = by_date_category or name .. ' după an'
		
		if supercatname then
			supercat = supercatname .. ' din ' .. year_num .. era_suffix
		end
		
		local start_year = math.max(decade_num, 1)
		local end_year = decade_num + 9
		local inc = 1
		
		if args.bce then
			start_year = decade_num + 9
			end_year = math.max(decade_num, 1)
			inc = -1
		end
		
		local before_start_year = start_year - inc
		local after_end_year = end_year + inc
		
		local before_start_year_text = before_start_year .. era_suffix
		local after_end_year_text = after_end_year .. era_suffix
		if before_start_year == 0 then
			before_start_year_text = '1 î.e.n.'
		end
		if after_end_year == 0 then
			after_end_year_text = '1'
		end
		
		table.insert(nearby_categories, {cat = name .. ' din ' .. before_start_year_text, display = '← ' .. before_start_year_text})
		for y = start_year, end_year, inc do
			table.insert(nearby_categories, {cat = name .. ' din ' .. y .. era_suffix, display = y .. era_suffix})
		end
		table.insert(nearby_categories, {cat = name .. ' din ' .. after_end_year_text, display = after_end_year_text .. ' →'})
	elseif decade then
		caption_text = caption_text .. 'anii ' .. decade_num
		
		parent_category = parent_category or name .. ' din secolul ' .. century_suffix_text .. century_intertext .. era_suffix
		parent_category_display = parent_category_display or name .. ' din secolul ' .. century_suffix_sup .. era_suffix
		
		by_date_category = by_date_category or name .. ' după deceniu'
		
		if supercatname then
			supercat = supercatname .. ' din anii ' .. decade_num .. era_suffix
		end
		
		local start_year = century_num
		local end_year = century_num + 90
		local inc = 10
		
		if args.bce then
			start_year = century_num + 90
			end_year = century_num
			inc = -10
		end
		
		local before_start_year = start_year - inc
		local after_end_year = end_year + inc
		
		local before_start_year_text = 'anii ' .. before_start_year .. era_suffix
		local after_end_year_text = 'anii ' .. after_end_year .. era_suffix
		if before_start_year == -10 then
			before_start_year_text = 'primul deceniu î.e.n.'
		end
		if after_end_year == -10 then
			after_end_year_text = 'primul deceniu e.n.'
		end
		
		table.insert(nearby_categories, {cat = name .. ' din ' .. before_start_year_text, display = '← ' .. before_start_year_text})
		for d = start_year, end_year, inc do
			table.insert(nearby_categories, {cat = name .. ' din anii ' .. d .. era_suffix, display = 'anii ' .. d .. era_suffix})
		end
		table.insert(nearby_categories, {cat = name .. ' din ' .. after_end_year_text, display = after_end_year_text .. ' →'})
	else
		caption_text = caption_text .. 'secolul ' .. century_suffix_sup
		
		parent_category = parent_category or name .. ' din mileniul ' .. millennium_suffix_text ..  era_suffix
		parent_category_display = parent_category_display or name .. ' din mileniul ' .. millennium_suffix_sup .. era_suffix
		
		by_date_category = by_date_category or name .. ' după secol'
		
		if supercatname then
			supercat = supercatname .. supercat_century_intertext .. ' din secolul ' .. century_suffix_text .. era_suffix
		end
		
		if not args.bce then
			by_date_sortkey = pad_number(century + 1, 2)
		else
			by_date_sortkey = '-' .. pad_number(century + 1, 2)
		end
		
		local start_year = millennium * 10
		local end_year = millennium * 10 + 9
		local inc = 1
		
		if args.bce then
			start_year = millennium * 10 + 9
			end_year = millennium * 10
			inc = -1
		end
		
		local before_start_year = start_year - inc
		local after_end_year = end_year + inc
		
		local before_start_year_text = 'secolul ' .. century_intertext .. ordinal(before_start_year + 1, false, 'm') .. era_suffix
		local after_end_year_text = 'secolul ' .. century_intertext .. ordinal(after_end_year + 1, false, 'm') .. era_suffix
	
		local before_start_year_display = 'secolul ' .. ordinal(before_start_year + 1, true, 'm') .. era_suffix
		local after_end_year_display = 'secolul ' .. ordinal(after_end_year + 1, true, 'm') .. era_suffix
		
		if before_start_year == -1 then
			before_start_year_text = 'secolul ' .. century_intertext .. ordinal(1, false, 'm') .. ' î.e.n.'
			before_start_year_display = 'secolul ' .. century_intertext .. ordinal(1, true, 'm') .. ' î.e.n.'
		end
		if after_end_year == -1 then
			after_end_year_text = 'secolul ' .. century_intertext .. ordinal(1, false, 'm')
			after_end_year_display = 'secolul ' .. ordinal(1, true, 'm')
		end
		
		table.insert(nearby_categories, {
			cat = name .. ' din ' .. before_start_year_text,
			display = '← ' .. before_start_year_display
		})
		for c = start_year, end_year, inc do
			table.insert(nearby_categories, {
				cat = name .. ' din secolul ' .. century_intertext .. ordinal(c + 1, false, 'm') .. era_suffix,
				display = 'secolul ' .. ordinal(c + 1, true, 'm') .. era_suffix
			})
		end
		table.insert(nearby_categories, {
			cat = name .. ' din ' .. after_end_year_text,
			display = after_end_year_display .. ' →'
		})
	end
	caption_text = caption_text .. era_suffix .. '.'
	
	nearby_category_list = mw.html.create('ul')
	for i = 1, #nearby_categories do
		nearby_category_list:tag('li')
			:wikitext('[[:Categorie:' .. nearby_categories[i]['cat'] .. '|' .. nearby_categories[i]['display'] .. ']]')
	end
	
	local cats = {}
	if testing or mw.title.getCurrentTitle():inNamespace(14) then
		table.insert(cats, '[[Categorie:' .. by_date_category .. '|' .. by_date_sortkey .. ']]')
		if parent_category ~= by_date_category then
			table.insert(cats, '[[Categorie:' .. parent_category .. '|' .. parent_sortkey .. ']]')
		end
		if supercat then
			table.insert(cats, '[[Categorie:' .. supercat .. '|' .. name .. ']]')
		end
	end
	
	local cats_by_date_div = mw.html.create('div')
		:tag('p')
			:wikitext(caption_text)
			:done()
		:tag('table')
			:addClass('toccolours plainlinks wst-cats-by-date-table')
			:tag('tr')
				:tag('td')
					:addClass('wst-cats-by-date-parent')
					:wikitext('[[:Categorie:' .. parent_category .. '|' .. parent_category_display .. ']]:')
					:done()
				:tag('td')
					:tag('div')
						:addClass('wst-flatlist')
						:node(nearby_category_list)
					:done()
				:done()
			:done()
		:wikitext(table.concat(cats))
		:allDone()
	
	return cats_by_date_div
end

function p.categories_by_date(frame)
	return p._categories_by_date(getArgs(frame))
end

return p