Модуль:Песочница/Seryo93/Infobox military conflict multipart

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

local infoboxStyle = mw.loadData('Module:Песочница/Seryo93/Infobox military conflict/style')

local IMCframe

local IMC = {}
IMC.__index = IMC


function IMC:renderPerCombatant(builder, headerText, prefix, ruprefix, suffix)
	prefix = prefix or ''
	ruprefix = ruprefix or ''
    
    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(infoboxStyle.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 infoboxStyle.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 infoboxStyle.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 infoboxStyle.internal_border or nil)
				:newline()
				:wikitext(jointText)
	end
end


function IMC:render()
	local hascombatantargs
	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 = IMCframe:expandTemplate{ title = 'commonslink2', args = { self.args['викисклад'], 'Аудио, фото, видео', icon_size = '12px' } }
	local iboxp = {
		['стиль_вверху']=infoboxStyle.header_ibox,
		['стиль_вверху2'] = infoboxStyle.sub_header_raw,
		['стиль_заголовков'] = infoboxStyle.header_ibox,
		['стиль_внизу'] = 'text-align: left;',
		['стиль_внизу2'] = infoboxStyle.sub_header_raw,
		['вверху'] = IMCframe:expandTemplate{ title='Карточка/название', args = {(self.args['conflict'] or self.args['конфликт'])}}
	}
	if self.args.partof or self.args['часть'] then 
		iboxp['вверху2'] = 'Основной конфликт: '.. (self.args.partof or self.args['часть'])
	end
	if self.args.image or self.args['изображение'] then
		local imagestring=self.args.image or self.args['изображение']
		iboxp['изображение'] = IMCframe:expandTemplate{title='Форматирование изображения', args = {imagestring, '300px'}}
	end
	if self.args.caption or self.args['подпись'] or self.args['заголовок'] then
		iboxp['подпись'] = self.args.caption or self.args['подпись'] or self.args['заголовок']
	end
	if self.args.date or self.args['дата'] then
		iboxp['метка1'] = 'Дата'
		iboxp['текст1'] = self.args.date or self.args['дата']
	end
	if self.args.place or self.args['место'] then
		iboxp['метка2'] = 'Место'
		iboxp['текст2'] = self.args.place or self.args['место']
	end
	if self.args.casus or self.args['причина'] then
		iboxp['метка3'] = 'Причина'
		iboxp['текст3'] = self.args.casus or self.args['причина']
	end
	if self.args['статус'] or self.args.status or self.args['итог'] or self.args.result then
		if (self.args['статус']  or self.args.status) then iboxp['метка4'] = 'Статус' else iboxp['метка4'] = 'Итог' end
		iboxp['текст4'] = self.args['статус'] or self.args.status or self.args['итог'] or self.args.result
	end
	if self.args.territory or self.args['изменения'] then
		iboxp['метка5'] = 'Изменения'
		iboxp['текст5'] = self.args.territory or self.args['изменения']
	end
	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:renderPerCombatant(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:renderPerCombatantMultipart(builder, nil, 'combatant', 'противник', v)
		end
	
		self:renderPerCombatant(builder, 'Командующие', 'commander', 'командир')
		self:renderPerCombatant(builder, 'Силы сторон', 'strength', 'силы')
		self:renderPerCombatant(builder, 'Потери', 'casualties', 'потери')
		builder:done()
		iboxp['текст6'] = tostring(builder)
	end
	if self.args.overall_casualties or self.args['общие_потери'] then
		iboxp['заголовок7'] = 'Общие потери'
		iboxp['текст8'] = self.args.overall_casualties or self.args['общие_потери']
	end
	iboxp['внизу'] = IMCframe:expandTemplate{title='Примечания', args={['group']='~', ['2']='height'}}
	if Commonstext~=('' or nil) then
		iboxp['внизу2'] = Commonstext
	end
	local infobox = IMCframe:expandTemplate{ title='Карточка', args = iboxp}
	return infobox
end

function IMC.new(frame, args)
	if not args then
		args = require('Module:Arguments').getArgs(frame, {wrappers = {'Template:Вооружённый конфликт', 'Template:Вооружённый конфликт/temp'}})
		--return
	end
	IMCframe = frame
	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,4 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