Module:Subject by century

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules


Usage[edit]

{{#invoke:Subject by century|subject_by_century}}


Code

require('strict')

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local make_attribute_string = require('Module:Optional CSS attribute').make_attribute_string
local langSwitch = require('Module:LangSwitch')._langSwitch
local century = require('Module:Century')._century
local flatlist = require('Module:Flatlist')._flatlist

local p = {}

local function dir(lang)
	return require('Module:Dir')['select'](lang, 'rtl', 'ltr')
end

local function bdi_open(lang)
	if not lang then
		return '<bdi>'
	end
	return '<bdi lang="' .. lang .. '" dir="' .. dir(lang) .. '">'
end

local function bc_ad_label(lang, when, nobc)
	if when ~= 'BC' and when ~= 'AD' then
		return nil
	elseif nobc then
		return ''
	else
		local time_text = require('Module:BC AD')['_' .. when]({['lang'] = lang})
		return '<span style="white-space:nowrap; font-weight:bold;">(' .. bdi_open(lang) .. time_text .. '</bdi>)&#32;</span>'
	end
end

local function ordinal_catlink(lang, subject, incentury, ordinal)
	ordinal = tonumber(ordinal)
	if not ordinal then
		return nil
	end
	local bc_ad
	if ordinal < 0 then
		bc_ad = ' BC'
	else
		bc_ad = ''
	end
	
	local suffixed_ordinal = century({[1] = math.abs(ordinal), ['lang'] = 'en'})
	local display = century({[1] = ordinal, ['lang'] = lang})
	
	local text
	if incentury then
		if subject then
			text = subject .. ' '
		else
			text = ''
		end
		text = text .. incentury .. ' ' .. suffixed_ordinal .. bc_ad
	elseif subject then
		suffixed_ordinal = century({[1] = math.abs(ordinal), ['lang'] = 'en', ['hyphen'] = true})
		text = suffixed_ordinal .. bc_ad .. ' ' .. subject
	else
		text = suffixed_ordinal .. bc_ad
	end
	text = mw.ustring.upper(mw.ustring.sub(text, 1, 1)) .. mw.ustring.sub(text, 2)
	
	return {['text'] = text, ['link'] = '[[:Category:' .. text .. '|' .. display .. ']]'}
end

function p._subject_by_century(args)
	local lang = args.lang or mw.getCurrentFrame():preprocess("{{Int:Lang}}")
	
	local catlinks_class = make_attribute_string('class', {['class'] = 'catlinks'})
	local catlinks_dir = make_attribute_string('dir', {['dir'] = args.dir or dir(lang)})
	local catlinks_style = make_attribute_string('style', {
		['clear'] = 'none',
		['font-size'] = '88%',
		['line-height'] = 'normal',
		['margin'] = '2px 0',
		['padding'] = '2px',
		['style'] = args.style or args.css
	})
	local catlinks_div_open = '<div ' .. catlinks_class .. catlinks_style .. '>'
	
	local image_span = ''
	local image = args.image or args.img
	if image then
		local imagesize = args.imagesize or args.imgsize or args.size or 'x14px'
		local border = args.border
		image_span = '<span style="vertical-align:top">[[File:' .. image .. '|' .. imagesize
		if border then
			image_span = image_span .. '|' .. border
		end
		image_span = image_span .. ']]</span>&thinsp;'
	end
	
	local mothercatlink
	local mothercat = args.mothercat
	local subject = args[1] or args.subject
	if mothercat then
		local mothercattrans = args.mothercattrans or '<bdi lang="en" dir="ltr">' .. mothercat .. '</bdi>'
		mothercatlink = '[[:Category:' .. mothercat .. '|' .. mothercattrans .. ']]'
	elseif subject then
		local subjecttrans = args.subjecttrans or '<bdi lang="en" dir="ltr">' .. subject .. '</bdi>'
		local subjecttrans_zh = args.subjecttrans or '「<bdi lang="en" dir="ltr">' .. subject .. '</bdi>」'
		local subjectlabel_table = {
			['de'] = bdi_open('de') .. subjecttrans .. ' nach Jahrhundert' .. '</bdi>',
			['en'] = bdi_open('en') .. subjecttrans .. ' by century' .. '</bdi>',
			['es'] = bdi_open('es') .. subjecttrans .. ' por siglo' .. '</bdi>',
			['fr'] = bdi_open('fr') .. subjecttrans .. ' par siècle' .. '</bdi>',
			['is'] = bdi_open('is') .. subjecttrans .. ' eftir öld' .. '</bdi>',
			['it'] = bdi_open('it') .. subjecttrans .. ' per secolo' .. '</bdi>',
			['ja'] = bdi_open('ja') .. '世紀ごとの' .. subjecttrans .. '</bdi>',
			['mk'] = bdi_open('mk') .. subjecttrans .. ' по век' .. '</bdi>',
			['nb'] = bdi_open('nb') .. subjecttrans .. ' etter århundre' .. '</bdi>',
			['nl'] = bdi_open('nl') .. subjecttrans .. ' per eeuw' .. '</bdi>',
			['pt'] = bdi_open('pt') .. subjecttrans .. ' por século' .. '</bdi>',
			['ru'] = bdi_open('ru') .. subjecttrans .. ' по веку' .. '</bdi>',
			['uk'] = bdi_open('uk') .. subjecttrans .. ' за століттям' .. '</bdi>',
			['zh'] = bdi_open('zh') .. '世纪' .. subjecttrans_zh .. '</bdi>'
		}
		local subjectlabel = langSwitch(subjectlabel_table, lang)
		
		local cap_subject = mw.ustring.upper(mw.ustring.sub(subject, 1, 1)) .. mw.ustring.sub(subject, 2)
		if mw.site.stats.pagesInCategory(cap_subject .. ' by century', 'all') > 0 then
			mothercatlink = '[[:Category:' .. subject .. ' by century|' .. subjectlabel .. ']]'
		else
			mothercatlink = subjectlabel
		end
	else
		local subjectlabel_table = {
			['de'] = bdi_open('de') .. 'Jahrhunderte' .. '</bdi>',
			['en'] = bdi_open('en') .. 'Centuries' .. '</bdi>',
			['es'] = bdi_open('es') .. 'Siglos' .. '</bdi>',
			['fr'] = bdi_open('fr') .. 'Siècles' .. '</bdi>',
			['is'] = bdi_open('is') .. 'Aldir' .. '</bdi>',
			['it'] = bdi_open('it') .. 'Secoli' .. '</bdi>',
			['ja'] = bdi_open('ja') .. '世紀' .. '</bdi>',
			['mk'] = bdi_open('mk') .. 'Векови' .. '</bdi>',
			['nb'] = bdi_open('nb') .. 'Århundrer' .. '</bdi>',
			['nl'] = bdi_open('nl') .. 'Eeuwen' .. '</bdi>',
			['pt'] = bdi_open('pt') .. 'Séculos' .. '</bdi>',
			['ru'] = bdi_open('ru') .. 'Века' .. '</bdi>',
			['sv'] = bdi_open('sv') .. 'Århundraden' .. '</bdi>',
			['uk'] = bdi_open('uk') .. 'Століття' .. '</bdi>',
			['zh'] = bdi_open('zh') .. '百年' .. '</bdi>'
		}
		mothercatlink = langSwitch(subjectlabel_table, lang)
	end
	local colon = require('Module:Colon')._colon({['lang'] = lang})
	local mothercattext = "''" .. mothercatlink .. colon .. "'' "
	
	local showall = yesno(args.showall or args.all or false)
	local nobc = yesno(args.nobc or false) and not showall
	
	local bc_label = bc_ad_label(lang, 'BC', nobc)
	local ad_label = bc_ad_label(lang, 'AD', nobc)
	local incentury = args.incentury
	
	-- FIXME: doesn't check back far enough (better algorithm to determine start?)
	local bc_catlist = {}
	local bc_min = args.bc_min or -50
	for o = bc_min, -1, 1 do
		local century_catlink = ordinal_catlink(lang, subject, incentury, o)
		if century_catlink and (showall or mw.site.stats.pagesInCategory(century_catlink['text'], 'all') > 0) then
			bc_catlist[#bc_catlist + 1] = century_catlink['link']
		end
	end
	if #bc_catlist > 0 then
		bc_catlist[1] = bc_label .. bc_catlist[1]
		bc_catlist = '\n* ' .. table.concat(bc_catlist, '\n* ')
	else
		bc_catlist = ''
	end
	
	local ad_catlist = {}
	local ad_max
	if yesno(args.old or false) then
		ad_max = math.ceil((tonumber(os.date("%Y"))-70)/100) -- max of the century it was 70 years ago
	else
		ad_max = math.ceil(tonumber(os.date("%Y")/100)) + 4 -- max of 4 centuries from this one
	end
	for o = 1, ad_max, 1 do
		local century_catlink = ordinal_catlink(lang, subject, incentury, o)
		if century_catlink and (showall or mw.site.stats.pagesInCategory(century_catlink['text'], 'all') > 0) then
			ad_catlist[#ad_catlist + 1] = century_catlink['link']
		end
	end
	if #ad_catlist > 0 then
		ad_catlist[1] = ad_label .. ad_catlist[1]
		ad_catlist = '\n* ' .. table.concat(ad_catlist, '\n* ')
	else
		ad_catlist = ''
	end
	
	local century_catlist
	if bc_catlist ~= '' or ad_catlist ~= '' then
		century_catlist = flatlist({[1] = bc_catlist .. ad_catlist}) .. '\n'
	else
		century_catlist = ''
	end
	
	return catlinks_div_open .. image_span .. mothercattext .. century_catlist .. '</div>'
end

function p.subject_by_century(frame)
	return p._subject_by_century(getArgs(frame))
end

return p