모듈:Infobox military conflict

From 더 스토리즈 대피소
Jump to navigation Jump to search

{{전쟁 정보}}를 구현합니다.


require('모듈:No globals')

local infoboxStyle = mw.loadData('모듈:WPMILHIST Infobox style')

local IMC = {}
IMC.__index = IMC

function IMC:renderPerCombatant(builder, headerText, prefix, suffix)
	prefix = prefix 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] then
			colspans[lastCombatant] = i - lastCombatant
			lastCombatant = i
		end
	end

	local jointText = self.args[prefix .. (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])
			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:renderHeaderTable(builder)
	builder = builder:tag('table')
		:css('width', '100%')
		:css('margin', 0)
		:css('padding', 0)
		:css('border', 0)

	if self.args['날짜'] then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext('날짜')
			:done()
			:tag('td')
				:wikitext(self.args['날짜'])
	end

	builder = builder:tag('tr')
		:tag('th')
			:css('padding-right', '1em')
			:wikitext('장소')
		:done()
		:tag('td')
			:tag('span')
				:addClass('location')
				:wikitext(self.args['장소'] or '{{{place}}}') -- hack so that people who don't know Lua know that this parameter is required
			:done()
	if self.args['좌표'] then
		builder:wikitext('<br />' .. self.args['좌표'])
	end
	builder = builder:done():done()

	-- only for "Putsch"
    if self.args['배경'] then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext(self.args.action and '배경')
			:done()
			:tag('td')
				:wikitext(self.args['배경'])
	end

	if self.args['상태'] or self.args['결과'] then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext(self.args['상태'] and '상태' or '결과')
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args['상태'] or self.args['결과'])
	end

	if self.args['영토'] then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext('영토 변화')
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args['영토'])
	end
end

function IMC:render()
	local builder = mw.html.create()
	if self.args['전역상자'] then
		builder = builder:tag('table')
			:css('float', 'right')
			:css('clear', 'right')
			:css('background', 'transparent')
			:css('margin', 0)
			:css('padding', 0)
			:tag('tr'):tag('td')
	end
	builder = builder:tag('table')
		:addClass('infobox vevent')
		:cssText(infoboxStyle.main_box_raw_auto_width)
		:css('width', self.args.width or '315px')

	builder:tag('tr')
		:tag('th')
			:addClass('summary')
			:attr('colspan', self.combatants)
			:cssText(infoboxStyle.header_raw)
			:wikitext(self.args['분쟁'] or mw.title.getCurrentTitle().text)
	if self.args['전체'] then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:cssText(infoboxStyle.sub_header_raw)
				:wikitext(self.args['전체'] .. '의 일부')
	end
	if self.args['그림'] then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:cssText(infoboxStyle.image_box_raw)
				:wikitext(string.format('%s%s%s',
					require('모듈:InfoboxImage').InfoboxImage{args = {
						["그림"] = self.args['그림'],
						["크기"] = self.args['그림크기'] or '300px',
						["기본"] = 'frameless',
						upright = 1,
						alt = self.args['대체설명']
					}},
					self.args['설명'] and '<br />' or '',
					self.args['설명'] or ''
				))
	end
	if self.args['그림2'] then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:cssText(infoboxStyle.image_box_raw)
				:wikitext(string.format('%s%s%s',
					require('모듈:InfoboxImage').InfoboxImage{args = {
						["그림"] = self.args['그림2'],
						["크기"] = self.args['크기2'] or '300px',
						["기본"] = 'frameless',
						upright = 1,
						alt = self.args['대체설명2']
					}},
					self.args['설명2'] and '<br />' or '',
					self.args['설명2'] or ''
				))
	end
	self:renderHeaderTable(builder:tag('tr'):tag('td'):attr('colspan', self.combatants))
	self:renderPerCombatant(builder, '교전국', '교전국')
	-- can be un-hardcoded once gerrit:165108 is merged
	for _,v in ipairs{'a','b','c','d'} do
		self:renderPerCombatant(builder, nil, '교전국', v)
	end
	
	self:renderPerCombatant(builder, '지휘관', '지휘관')
	self:renderPerCombatant(builder, '군대', '군대')
	self:renderPerCombatant(builder, '병력', '병력')
	self:renderPerCombatant(builder, '정치적 지원', 'polstrength')
	self:renderPerCombatant(builder, '군사적 지원', 'milstrength')
	self:renderPerCombatant(builder, '피해 규모', '사상자')

	if self.args['기타'] then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:css('font-size', '90%')
				:css('border-top', infoboxStyle.section_border)
				:newline()
				:wikitext(self.args['기타'])
	end
	if self.args['지도'] then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:cssText(infoboxStyle.image_box_raw)
				:wikitext(string.format('%s%s%s',
					require('모듈:InfoboxImage').InfoboxImage{args = {
						["그림"] = self.args['지도'],
						["크기"] = self.args['지도크기'] or self.args['그림크기'] or '300px',
						["기본"] = 'frameless',
						upright = 1,
						alt = self.args['대체지도설명']
					}},
					self.args['지도설명'] and '<br />' or '',
					self.args['지도설명'] or ''
					))
	end
	builder = builder:done()
	if self.args['전역상자'] then
		builder = builder:done():done():tag('tr')
			:tag('td')
				:wikitext(self.args['전역상자'])
			:done()
		:done()
	end
	return builder
end

function IMC.new(frame, args)
	if not args then
		args = require('모듈:Arguments').getArgs(frame, {wrappers = '틀:전쟁 정보'})
	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,5 do
			if 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