پودمان:سرساز رده دولت‌های ایران

توضیحات پودمان[نمایش] [ویرایش] [تاریخچه] [پاکسازی]

این پودمان، الگو:سرساز رده دولت‌های ایران را پیاده‌سازی می‌کند.

در زمان تشکیل دولت جدید نیازی به اعمال تغییر در این پودمان و الگوی مبتنی بر آن نیست، اما زیرصفحهٔ /داده این پودمان باید با داده‌های دولت جدید به‌روز شود.

برای اطلاع از چگونگی به‌روزرسانی داده‌ها، مستندات زیرصفحهٔ داده‌ها را ببینید.

require('strict')

local p = {}

local mData = require('پودمان:سرساز رده دولت‌های ایران/داده')

local function linkCat(name, label)
	return '[[:رده:' .. name .. '|' .. label .. ']]'
end

local function createNavigationTable(frame, data)
	local nav
	local links = {}
	local gov = data['ordinal_name']
	local others = {
		['-2'] = gov ~= 'اول' and mData[data['prev_gov'][2]]['prev_gov'] or nil,
		['-1'] = data['prev_gov'],
		['+1'] = data['next_gov'],
		['+2'] = data['next_gov'][2] and mData[data['next_gov'][2]]['next_gov'] or nil,
	}
	
	local div = mw.html.create('div')
	div
		:addClass('toc')
		:css('margin', '0.5em auto')
		:css('align', 'center')
	
	if gov == 'اول' then
		table.insert(links, linkCat(others['-1'][1], others['-1'][2]))
	else
		table.insert(links, linkCat(others['-2'][1], others['-2'][2]))
		table.insert(links, linkCat(others['-1'][1], others['-1'][2]))
	end
	table.insert(links, "'''" .. gov .. "'''")
	if type(others['+1']) == 'string' then
		table.insert(links, '<span style="color: grey;">' .. others['+1'] .. '</span>')
	elseif type(others['+2']) == 'string' then
		table.insert(links, linkCat(others['+1'][1], others['+1'][2]))
		table.insert(links, '<span style="color: grey;">' .. others['+2'] .. '</span>')
	else
		table.insert(links, linkCat(others['+1'][1], others['+1'][2]))
		table.insert(links, linkCat(others['+2'][1], others['+2'][2]))
	end
	
	nav = frame:expandTemplate{
		title = 'فهرست یک‌دست',
		args = {'\n*' .. table.concat(links, '\n*')}
	}
	
	div:wikitext(nav)
	
	return tostring(div) .. '\n'
end

local function createHeader (data)
	local gov = data['ordinal_name']
	local years
	if data['start_year'] == data['end_year'] then
		years = data['start_date'] .. '–' .. data['end_date']
	else
		years = data['start_year'] .. '–' .. data['end_year']
	end
	local pres
	if data['president'] == '-' then
		 pres = false
	else
		pres = data['president']
	end
	local primeMinister
	if data['prime_minister'] then
		primeMinister = data['prime_minister']
	else
		primeMinister = false
	end
	local formatString = 'این [[راهنما:رده|رده]] حاوی موضوعات مرتبط با'
		.. " '''[[%s]]''' "
		.. ' (%s) %s%sاست.'
	local out = mw.ustring.format(formatString,
		gov == 'موقت' and 'دولت موقت محمدرضا مهدوی کنی' or 'دولت ' .. gov .. ' جمهوری اسلامی ایران',
		years,
		pres and 'به ریاست [[' .. pres .. ']] ' or '',
		primeMinister
			and (pres and 'و' or 'به')  .. ' نخست‌وزیری [[' .. primeMinister .. ']] '
			or ''
	)
	return out
end

local function categorize (data)
	local out = '\n\n[[رده:دولت‌های جمهوری اسلامی ایران|' .. data['sortkey'] .. ']]'
	return out
end

function p.main (frame)
	local out = ''
	
	local titleObj = mw.title.getCurrentTitle()
	local ns = titleObj.nsText
	local title = titleObj.text
	local gov = mw.ustring.match(title, '^دولت (.*) جمهوری اسلامی ایران')
	if not gov and title == 'دولت موقت محمدرضا مهدوی کنی' then
		gov = 'موقت'
	end
	local data = mData[gov]
	
	out = out ..
		createNavigationTable(frame, data) ..
		createHeader(data) ..
		categorize(data)
		
	return out
end

return p