Модуль:Multifaction military conflict

Материал из Википедии — свободной энциклопедии
Перейти к навигации Перейти к поиску
Документация
require('strict')

local IBS = require('Module:Multifaction military conflict/style').IMC

local IBP = {
	['имя'] = 'Многосторонний вооружённый конфликт',
	['стиль_вверху'] = IBS.header_ibox,
	['стиль_вверху2'] = IBS.sub_header_raw,
	['стиль_заголовков'] = IBS.header_ibox,
	['стиль_внизу'] = 'text-align: left;',
	['стиль_внизу2'] = IBS.sub_header_raw,
}

local IMC = {}
IMC.__index = IMC


function IMC:renPC(builder, headerText, prefix, ruprefix, suffix)
	    prefix = prefix or ''
	    ruprefix = ruprefix or ''
        --end
	suffix = suffix or ''
	local colspans = {}
	
	-- This may result in colspans[1] getting set twice, but
	-- this is no big deal. The second set will be correct.
	local lastCombatant = 1
	
	for i = 1,self.combatants do
		if self.args[prefix .. i .. suffix] or self.args[ruprefix .. i .. suffix] then
			colspans[lastCombatant] = i - lastCombatant
			lastCombatant = i
		end
	end

	local jointText = self.args[prefix .. (self.combatants + 1) .. suffix] or self.args[ruprefix .. (self.combatants + 1) .. suffix]
	
	if headerText and (colspans[1] or jointText) then
		builder:tag('tr')
			:tag('th')
				:attr('colspan', self.combatants)
				:cssText(IBS.header_raw)
				:wikitext(headerText)
	end

	-- The only time colspans[1] wouldn't be set is if no
	-- combatant has a field with the given prefix and suffix.
	if colspans[1] then
		-- Since each found argument set the colspan for the previous
		-- one, the final one wasn't set above, so set it now.
		colspans[lastCombatant] = self.combatants - lastCombatant + 1
		builder = builder:tag('tr')
		for i = 1,self.combatants do
			-- At this point, colspans[i] will be set for i=1 unconditionally, and for
			-- any other value of i where self.args[prefix .. i .. suffix] is set.
			if colspans[i] then
				builder:tag('td')
					-- don't bother emitting colspan="1"
					:attr('colspan', colspans[i] ~= 1 and colspans[i] or nil)
					:css('width', math.floor(100 / self.combatants * colspans[i] + 0.5) .. '%')
					-- no border on the right of the rightmost column
					:css('border-right', i ~= lastCombatant and IBS.internal_border or nil)
					-- no padding on the left of the leftmost column
					:css('padding-left', i ~= 1 and '0.25em' or nil)
					-- don't show the border if we're directly under a header
					:css('border-top', not headerText and IBS.internal_border or nil)
					:newline()
					:wikitext(self.args[prefix .. i .. suffix] or self.args[ruprefix .. i .. suffix])
			end
		end
	end

	if jointText then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:css('text-align', 'center')
				-- don't show the border if we're directly under a header
				:css('border-top', (not headerText or colspans[1]) and IBS.internal_border or nil)
				:newline()
				:wikitext(jointText)
	end
end

function IMC:fillargs(ilabel, idata, label, endata, rudata)
	if self.args[endata] or self.args[rudata] then
		if (ilabel and label)~=nil then
		IBP[ilabel] = label
		end
		IBP[idata] = self.args[endata] or self.args[rudata]
	end
end

function IMC:xp(ttl, args)
	return self.frame:expandTemplate{ title=ttl, args = args}
end

function IMC:render()
	local HasCombatantArgs = false
	for _,v in ipairs{'', 'a', 'b', 'c', 'd', 'а', 'б', 'в', 'г'} do
		for i=1,6 do 
			for _,p in ipairs {'combatant', 'commander', 'strength', 'casualties', 'противник', 'командир', 'силы', 'потери'} do
				if self.args[p .. i .. v] then
					HasCombatantArgs=true
					break
				end
			
			end
			end
	end
	local Commonstext = self:xp('Карточка/Викисклад', { self.args['викисклад'], 'Аудио, фото, видео', icon_size = '12px' } )
	IBP['вверху'] = self:xp('Карточка/название', {(self.args['conflict'] or self.args['конфликт'])})
	if self.args['partof'] or self.args['часть'] then
		IBP['вверху2'] = 'Основной конфликт: '..(self.args['partof'] or self.args['часть'])
	end
	if self.args.image or self.args['изображение'] then
		local imagestring=self.args.image or self.args['изображение']
		IBP['изображение'] = self:xp('Форматирование изображения', {imagestring, '300px'})
	end
	if self.args.caption or self.args['подпись'] or self.args['заголовок'] then
		IBP['подпись'] = self.args.caption or self.args['подпись'] or self.args['заголовок']
	end
	self:fillargs('метка1', 'текст1', 'Дата', 'date', 'дата')
	self:fillargs('метка2', 'текст2', 'Место', 'place', 'место')
	self:fillargs('метка3', 'текст3', 'Причина', 'casus', 'причина')
	if self.args['статус'] or self.args.status or self.args['итог'] or self.args.result then
		if (self.args['статус']  or self.args.status) then IBP['метка4'] = 'Статус' else IBP['метка4'] = 'Итог' end
		IBP['текст4'] = self.args['статус'] or self.args.status or self.args['итог'] or self.args.result
	end
	self:fillargs('метка5', 'текст5', 'Изменения', 'territory', 'изменения')
	if HasCombatantArgs==true then 
		local builder = mw.html.create()
		builder = builder:tag('table')
			:css('width', '100%')
			:css('background', '#f9f9f9')
			:css('text-align', 'left')
			--:css('border', 0)

	
		self:renPC(builder, self.args.combatants_header or self.args['заголовок противников'] or 'Противники', 'combatant', 'противник')
	-- can be un-hardcoded once gerrit:165108 is merged
		for _,v in ipairs{'a','b','c','d'} do
				self:renPC(builder, nil, 'combatant', 'противник', v)
		end
	
		self:renPC(builder, 'Командующие', 'commander', 'командир')
		self:renPC(builder, 'Силы сторон', 'strength', 'силы')
		self:renPC(builder, 'Потери', 'casualties', 'потери')
		builder:done()
		IBP['текст6'] = tostring(builder)
	end
	if self.args.overall_casualties or self.args['общие_потери'] then
		IBP['заголовок7'] = 'Общие потери'
		IBP['текст8'] = self.args.overall_casualties or self.args['общие_потери']
	end
	IBP['внизу'] = self:xp('Примечания', {['group']='~'})
	if Commonstext~=('' or nil) then
		IBP['внизу2'] = Commonstext
	end
	local IB  = self:xp('Карточка', IBP)
	return IB 
end

function IMC.new(frame, args)
	if not args then
		args = require('Module:Arguments').getArgs(frame, {wrappers = {'Template:Многосторонний вооружённый конфликт', 'Template:Вооружённый конфликт/temp'}})
		--return
	end
	local obj = {
		frame = frame,
		args = args
	}
    
	-- until gerrit:165108 is merged, there's still a cap on combatants, but as soon as it merges, we can update this little bit of code to uncap it
	-- also, don't try to make this more efficient, or references could be in the wrong order
	obj.combatants = 2
	for _,v in ipairs{'', 'a', 'b', 'c', 'd'} do
		for i = 1,6 do
			for _,p in ipairs {'противник', 'командир', 'силы', 'потери'} do
				args[p..i..'a'] = args[p..i..'а'] or nil
				args[p..i..'b'] = args[p..i..'б'] or nil
				args[p..i..'c'] = args[p..i..'в'] or nil
				args[p..i..'d'] = args[p..i..'в'] or nil	
			end
			if args['combatant' .. i .. v] or args['противник' .. i .. v] then
					obj.combatants = math.max(obj.combatants, i)
			end
			end
		end
	return setmetatable(obj, IMC)
end

local p = {}

function p.main(frame)
	return IMC.new(frame):render()
	
end

return p