Module:Categories by date/sandbox

From Wikisource
Jump to navigation Jump to search
local p = {}

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local ordinal = require('Module: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 caption = args.caption or '{{{caption}}}'
	local name = args.name or '{{{name}}}'
	local supercatname = args.supercatname
	
	local year = tonumber(args.year)
	local decade = tonumber(args.decade)
	local century = tonumber(args.century) or 0
	
	local era_suffix = ''
	if args.era == 'BCE' or args.era == 'BC' or yesno(args.bce) then
		era_suffix = ' BCE'
	end
	
	local century_intertext = ' '
	if yesno(args['century-hyphen']) then
		century_intertext = '-'
	end
	local supercat_century_intertext = ' '
	if yesno(args['supercat-century-hyphen']) 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, false)
	local century_suffix_sup = ordinal(century + 1, false, true)
	
	local millennium = math.floor(century/10)
	local millennium_num = millennium * 1000
	local millennium_suffix_text = ordinal(millennium + 1, false, false)
	local millennium_suffix_sup = ordinal(millennium + 1, false, true)
	
	local caption_text = caption .. ' in the '
	local parent_category
	local parent_category_display
	local by_date_category
	local supercat
	local nearby_categories = {}
	
	local parent_sortkey
	local by_date_sortkey
	if era_suffix == '' 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 .. 'year ' .. year_num
		
		parent_category = decade_num .. 's' .. era_suffix .. ' ' .. name
		parent_category_display = parent_category
		
		by_date_category = name .. ' by year'
		
		if supercatname then
			supercat = year_num .. era_suffix .. ' ' .. supercatname
		end
		
		for y = math.max(decade_num, 1), decade_num + 9 do
			table.insert(nearby_categories, {cat = y .. era_suffix .. ' ' .. name, display = y .. era_suffix})
		end
	elseif decade then
		caption_text = caption_text .. decade_num .. 's'
		
		parent_category = century_suffix_text .. century_intertext .. 'century' .. era_suffix .. ' ' .. name
		parent_category_display = century_suffix_sup .. ' ' .. 'century' .. era_suffix .. ' ' .. name
		
		by_date_category = name .. ' by decade'
		
		if supercatname then
			supercat = decade_num .. 's' .. era_suffix .. ' ' .. supercatname
		end
		
		for d = century_num, century_num + 90, 10 do
			table.insert(nearby_categories, {cat = d .. 's' .. era_suffix .. ' ' .. name, display = d .. 's' .. era_suffix})
		end
	else
		caption_text = caption_text .. century_suffix_sup .. ' century'
		
		parent_category = millennium_suffix_text .. ' millennium' .. era_suffix .. ' ' .. name
		parent_category_display = millennium_suffix_sup .. ' millennium' .. era_suffix .. ' ' .. name
		
		by_date_category = name .. ' by century'
		
		if supercatname then
			supercat = century_suffix_text .. era_suffix .. supercat_century_intertext .. 'century ' .. supercatname
		end
		
		if era_suffix == '' then
			by_date_sortkey = pad_number(century + 1, 2)
		else
			by_date_sortkey = '-' .. pad_number(century + 1, 2)
		end
		
		for c = millennium * 10, millennium * 10 + 9 do
			table.insert(nearby_categories, {
				cat = ordinal(c + 1, false, false) .. century_intertext .. 'century' .. era_suffix .. ' ' .. name,
				display = ordinal(c + 1, false, true) .. ' century' .. era_suffix
			})
		end
	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('[[:Category:' .. nearby_categories[i]['cat'] .. '|' .. nearby_categories[i]['display'] .. ']]')
	end
	
	local cats = {
		'[[Category:' .. parent_category .. '|' .. parent_sortkey .. ']]',
		'[[Category:' .. by_date_category .. '|' .. by_date_sortkey .. ']]'
	}
	if supercat then
		table.insert(cats, '[[Category:' .. supercat .. '|' .. name .. ']]')
	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')
					:wikitext('[[:Category:' .. 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