Модуль:Infobox civil conflict

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

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

local IBP = {
	['стиль_вверху']=IBS.main_header_raw ,
	['стиль_вверху2'] = IBS.sub_header_raw,
	['стиль_заголовков'] = IBS.header_raw,
	['стиль_внизу'] = 'text-align: left;',
	['стиль_внизу2'] = IBS.sub_header_raw,
	['стиль_изображения'] = 'text-align: center;'
}

local ICC = {}
ICC.__index = ICC


function ICC:renPS(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 ~= 1 and self.combatants or nil)
				: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')
					:addClass('noplainlist')
					-- 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 ICC:xp(ttl, args)
	return self.frame:expandTemplate{ title=ttl, args = args}
end

function ICC:checkargs(enarg, ruarg)
	local argcheckresult = false
		for _,v in ipairs{'', 'a', 'b', 'c', 'd', 'а', 'б', 'в', 'г'} do
			for i=1,6 do 
				if self.args[enarg..i..v] or self.args[ruarg..i..v] then
					argcheckresult = true
					break
				end
			
			end
		end
	return argcheckresult
end

function ICC:cas(ea, ra)
	local CAR = nil
	if self.args[ea] or self.args[ra] then
		CAR = true
	end
	return CAR
end

function ICC:renderLowerTable(builder)
	builder = builder:tag('table')
		:css('width', '100%')
		:css('margin', 0)
		:css('padding', 0)
		:css('border', 0)
	if self:cas('fatalities', 'погибло') then
				builder:tag('tr')
			:tag('th')
				:wikitext('Погибло')
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args.fatalities or self.args['погибло'])
	end
	if self:cas('injuries', 'ранено') then
				builder:tag('tr')
			:tag('th')
				:wikitext('Ранено')
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args.injuries or self.args['ранено'])
	end
	if self:cas('arrests', 'арестовано') then
				builder:tag('tr')
			:tag('th')
				:wikitext('Арестовано')
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args.arrests or self.args['арестовано'])
	end
end

function ICC:renderHeaderTable(builder)
	builder = builder:tag('table')
		:css('width', '100%')
		:css('margin', 0)
		:css('padding', 0)
		:css('border', 0)
	if self:cas('date', 'дата') then
				builder:tag('tr')
			:tag('th')
				:wikitext('Date')
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args.date or self.args['дата'])
	end
	if self:cas('place', 'место') then
			builder:tag('tr')
			:tag('th')
				:wikitext('Место')
			:done()
			:tag('td')
			:tag('span')
				:addClass('location')
				:newline()
				:wikitext(self.args.place or self.args['место'])
				:done()
	if self:cas('coordinates', 'координаты') and (self:cas('coordinates', 'координаты')~=nil) then
		builder:wikitext('<br />' .. self.args['coordinates'] or self.args['координаты'])
	end
	end
	if self:cas('causes', 'причины') then
				builder:tag('tr')
			:tag('th')
				:wikitext('Причины')
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args.causes or self.args['причины'])
	end
	if self:cas('goals', 'цели') then
				builder:tag('tr')
			:tag('th')
				:wikitext('Цели')
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args.goals or self.args['цели'])
	end	
	if self:cas('methods', 'методы') then
				builder:tag('tr')
			:tag('th')
				:wikitext('Характеристика')
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args.methods or self.args['методы'])
	end
	if self:cas('status', 'статус') or self:cas('result', 'итог') then
				builder:tag('tr')
			:tag('th')
				:wikitext(self:cas('status', 'статус') and 'Статус' or 'Итог')
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args.status or self.args['статус'] or self.args.result or self.args['итог'])
	end
	if self:cas('concessions', 'уступки') then
				builder:tag('tr')
			:tag('th')
				:wikitext('Уступки<br>данные')
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args.concessions or self.args['уступки'])
	end
	if self:cas('territory', 'изменения') then
				builder:tag('tr')
			:tag('th')
				:wikitext('Изменения')
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args.territory or self.args['изменения'])
	end
end

function ICC:render()
	local builder = mw.html.create()
	builder = builder:tag('table')
		:addClass('infobox vevent')
	builder:tag('tr')
		:tag('th')
			:addClass('summary')
			:attr('colspan', self.combatants ~= 1 and self.combatants or nil)
			:cssText(IBS.main_header_raw)
			:wikitext(self:xp('Карточка/название', {(self.args['title'] or self.args['заголовок'])}))
	if self:cas('partof', 'часть') then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants ~= 1 and self.combatants or nil)
				:cssText(IBS.sub_header_raw)
				:wikitext(self.args ['partof'] or self.args['часть'])
	end
	if self:cas('image', 'изображение') then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants ~= 1 and self.combatants or nil)
				:css('text-align', 'center')
				:css('border-bottom', not self:cas('caption', 'подпись') and IBS.internal_border or nil)
				:wikitext(self.args['image'] or self.args['caption'])
	end
	if self:cas('caption', 'подпись') then
			builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants ~= 1 and self.combatants or nil)
				:css('text-align', 'center')
				:css('border-bottom', IBS.internal_border or nil)
				:newline()
				:wikitext(self.args['caption'] 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
	self:renderHeaderTable(builder:tag('tr'):tag('td'):attr('colspan', self.combatants ~= 1 and self.combatants or nil))
	self:renPS(builder, self.args.parties_header or self.args['заголовок сторон конфликта'] or 'Стороны конфликта', 'side', 'сторона')
	for _,v in ipairs{'a', 'b', 'c', 'd'} do
		self:renPS(builder, nil, 'side', 'сторона', v)
	end
	self:renPS(builder, 'Ключевые фигуры', 'leadfigures', 'ключевыефигуры')
	self:renPS(builder, 'Участвующие силы', 'units', 'силы')
	self:renPS(builder, 'Число участников', 'howmany', 'сколько')
	self:renPS(builder, self.args['casualties_header'] or self.args['потери_название'] or 'Потери', 'casualties', 'потери')
	if self:checkargs('casualties', 'потери')==true then
	self:renderLowerTable(builder:tag('tr'):tag('td'):attr('colspan', self.combatants ~= 1 and self.combatants or nil))
	end
	if self:cas('notes', 'заметки') then
		builder:tag('tr')
		:tag('td')
		:attr('colspan', self.combatants ~= 1 and self.combatants or nil)
		:css('border-top', IBS.internal_border)
		:wikitext(self.args.notes or self.args['заметки'])
	end
	if self:cas('map_type', 'тип_карты') then
		builder:tag('tr')
		:tag('td')
		:attr(colspan, self.combatants ~= 1 and self.combatants or nil)
		:css('border-top', IBS.internal_border)
		:css('text-align', 'center')
		:wikitext(self:xp('ПозКарта', {
			(self.args['map_type'] or self.args['тип_карты']), 
			lat=(self.args['latitude'] or self.args['широта']), 
			lon=(self.args['longitude'] or self.args['долгота']), 
			width=(self.args['map_size'] or self.args['размер_карты'] or '220'),
			float='center',
			border='infobox',
			label=(self.args['map_label'] or self.args['метка_карты']),
			caption = self.args['map_caption'] or self.args['описание карты'] or ('Расположено в '.. (self.args['map_type'] or self.args['тип_карты']))
	}))
	end
	builder = builder:done()
	return builder 
end

function ICC.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 = 1
	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['side' .. i .. v] or args['сторона' .. i .. v] then
					obj.combatants = math.max(obj.combatants, i)
			end
			end
		end
	return setmetatable(obj, ICC)
end

local p = {}

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

return p