Module:Soldier

Revision as of 02:00, 18 August 2026 by Admin (talk | contribs) (Import soldier lead module)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Soldier/doc

local p = {}

local function trim(s)
	return mw.text.trim(tostring(s or ''))
end

local function parentArg(frame, name)
	local parent = frame:getParent()
	if parent and parent.args[name] then
		return trim(parent.args[name])
	end
	return trim(frame.args[name])
end

local function titleCase(s)
	s = trim(s)
	if s == '' then
		return s
	end
	s = mw.ustring.lower(s)
	return mw.ustring.gsub(s, "(%a)([%w']*)", function(first, rest)
		return mw.ustring.upper(first) .. rest
	end)
end

local function displayName(frame)
	local first = parentArg(frame, 'first')
	local middle = parentArg(frame, 'middle')
	local surname = parentArg(frame, 'surname')
	local suffix = parentArg(frame, 'suffix')
	local parts = {}
	if first ~= '' then
		table.insert(parts, first)
	end
	if middle ~= '' then
		table.insert(parts, middle)
	end
	if surname ~= '' then
		table.insert(parts, surname)
	end
	local name = table.concat(parts, ' ')
	if suffix ~= '' then
		if name ~= '' then
			name = name .. ', ' .. suffix
		else
			name = suffix
		end
	end
	if name == '' then
		name = 'This soldier'
	end
	return name
end

function p.displayName(frame)
	return displayName(frame)
end

function p.classLinks(frame)
	local raw = parentArg(frame, 'class_number')
	local out = {}
	for part in mw.text.gsplit(raw, ';', true) do
		part = trim(part)
		if part ~= '' then
			table.insert(out, string.format('[[Class %s|%s]]', part, part))
		end
	end
	return table.concat(out, ', ')
end

function p.lead(frame)
	local name = displayName(frame)
	local surname = parentArg(frame, 'surname')
	if surname == '' then
		surname = name
	end
	local birth = parentArg(frame, 'birth_year')
	local death = parentArg(frame, 'death_year')
	local dates = ''
	if birth ~= '' and death ~= '' then
		dates = string.format(' (%s – %s)', birth, death)
	elseif birth ~= '' then
		dates = string.format(' (born %s)', birth)
	elseif death ~= '' then
		dates = string.format(' (died %s)', death)
	end

	local rank = parentArg(frame, 'rank_final')
	if rank == '' then
		rank = parentArg(frame, 'class_rank')
	end

	local role = 'was a United States Army'
	if rank ~= '' then
		role = role .. ' ' .. rank
	end
	role = role .. ' soldier'

	local classRaw = parentArg(frame, 'class_number')
	local classLinks = {}
	for part in mw.text.gsplit(classRaw, ';', true) do
		part = trim(part)
		if part ~= '' then
			table.insert(classLinks, string.format('[[Class %s]]', part))
		end
	end
	if #classLinks == 1 then
		role = role .. ' who trained at [[Camp Ritchie]], Maryland, in ' .. classLinks[1]
	elseif #classLinks > 1 then
		role = role .. ' who trained at [[Camp Ritchie]], Maryland, in classes ' .. table.concat(classLinks, ', ')
	else
		role = role .. ' associated with Camp Ritchie, Maryland'
	end

	local sentences = {
		string.format("'''%s'''%s %s.", name, dates, role)
	}

	local enlist = parentArg(frame, 'enlistment_place')
	local state = parentArg(frame, 'state')
	if enlist ~= '' then
		local place = titleCase(enlist)
		if state ~= '' then
			place = place .. ', ' .. titleCase(state)
		end
		table.insert(sentences, surname .. ' enlisted at ' .. place .. '.')
	elseif state ~= '' then
		table.insert(sentences, 'Records list ' .. titleCase(state) .. ' as the state of enlistment or residence.')
	end

	local assignment = parentArg(frame, 'assignment')
	if assignment ~= '' then
		table.insert(sentences, surname .. ' was assigned to ' .. assignment .. '.')
	end

	local ipw = {}
	local ge = parentArg(frame, 'ipw_ge')
	local it = parentArg(frame, 'ipw_it')
	if ge ~= '' then
		table.insert(ipw, 'German-language IPW team ' .. ge)
	end
	if it ~= '' then
		table.insert(ipw, 'Italian-language IPW team ' .. it)
	end
	if #ipw > 0 then
		table.insert(sentences, 'Interrogation duty included ' .. table.concat(ipw, ' and ') .. '.')
	end

	table.insert(sentences, 'Add a fuller biography, a photograph, and sources below.')

	return table.concat(sentences, ' ')
end

return p