Module:Infobox military conflict

require('Module:No globals')

local p = {}

local getArgs = require('Module:Arguments').getArgs local InfoboxImage = require('Module:InfoboxImage').InfoboxImage

local function hasAtLeastOne(args, combatants, prefix, suffix) if combatants then for i = 1,combatants do if args[prefix .. i .. suffix] then return true end end end return false end

local function renderHeadingRow(builder, colspan, header_bar, text) builder:tag('tr') :wikitext(string.format('%s ', colspan, header_bar, text )) end

local function renderPerCombatant(builder, args, combatants, prefix, suffix, header_bar, headerText, internal_border) if hasAtLeastOne(args, combatants, prefix, suffix) then if headerText then renderHeadingRow(builder, combatants or 2, header_bar, headerText) end local combatantWidth = (100/(combatants or 2)) .. '%'		builder = builder:tag('tr') for i = 1,combatants do			builder:tag('td') :css('width', combatantWidth) -- no border on the right of the rightmost column :css('border-right', i ~= combatants and internal_border or nil) -- no padding on the left of the leftmost column :css('padding-left', i ~= 1 and '0.25em' or nil) :css('border-top', suffix ~= '' and internal_border or nil) :wikitext(args[prefix .. i .. suffix]) end end end

local function renderHeaderTable(builder, args) builder = builder:tag('table') :css('width', '100%') :css('margin', 0) :css('padding', 0) :css('border', 0)

if args.date then builder:tag('tr') :tag('th') :css('padding-right', '1em') :wikitext('Date') :done :tag('td') :wikitext(args.date) end

builder = builder:tag('tr') :tag('th') :css('padding-right', '1em') :wikitext('Location') :done :tag('td') :tag('span') :addClass('location') :wikitext(args.place) :done if args.coordinates then builder:wikitext(' ' .. args.coordinates) end builder = builder:done:done

if args.status or args.result then builder:tag('tr') :tag('th') :css('padding-right', '1em') :wikitext(args.status and 'Status' or 'Result') :done :tag('td') :wikitext(args.status or args.result) end

if args.territory then builder:tag('tr') :tag('th') :css('padding-right', '1em') :wikitext('Territorial changes') :done :tag('td') :wikitext(args.territory) end end

function p.main(frame) local args = getArgs(frame, {wrappers = 'Template:Infobox military conflict'})

-- 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 local combatants = args.combatant1 and 1 combatants = args.combatant2 and 2 or combatants combatants = args.combatant3 and 3 or combatants combatants = args.combatant4 and 4 or combatants local combatantsColspan = combatants or 2 local combatantWidth = (100/combatantsColspan) .. '%'	local header_bar = frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'header_bar'}} local internal_border = frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'internal_border'}}

local builder = mw.html.create if args.campaignbox 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:wikitext('') builder:tag('tr') :wikitext(string.format('%s ', combatantsColspan, header_bar, args.conflict or mw.title.getCurrentTitle.text ))	if args.partof then builder:tag('tr') :wikitext(string.format('Part of %s ', combatantsColspan, frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'sub_header_bar'}}, args.partof ))	end if args.image then builder:tag('tr') :wikitext(string.format('%s%s%s ', combatantsColspan, frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'image_box'}}, InfoboxImage{args = { image = args.image, size = args.image_size, sizedefault = 'frameless', upright = 1, alt = args.alt }},				args.caption and ' ' or '', args.caption or '' ))	end renderHeaderTable(		builder:tag('tr'):tag('td'):attr('colspan', combatantsColspan),		args	)

renderPerCombatant(builder, args, combatants, 'combatant', '', header_bar, args.combatants_header or 'Belligerents', internal_border) -- can be un-hardcoded once gerrit:165108 is merged for _,v in ipairs{'a','b','c','d'} do		renderPerCombatant(builder, args, combatants, 'combatant', v, header_bar, nil, internal_border) end renderPerCombatant(builder, args, combatants, 'commander', '', header_bar, 'Commanders and leaders', internal_border) return builder:allDone end

return p