Wikipedia-style awards grid under ribbon rows
Infobox: drop certificate/arrive/depart/years; expand other_class codes
 
(28 intermediate revisions by the same user not shown)
Line 185: Line 185:
'physically unfit', 'unfit for', 'relieved prior', 'relieved of',
'physically unfit', 'unfit for', 'relieved prior', 'relieved of',
'kicked', 'eliminated from', 'not graduated', 'did not graduate',
'kicked', 'eliminated from', 'not graduated', 'did not graduate',
'excess absence', 'illness', 'sick call', 'medical board',
'psychiatr', 'neurot', 'homosexual', 'suicid', 'alcohol', 'drunk',
'intoxicat', 'venereal', 'syphili', 'gonorrh', 'mental case',
'mental illness', 'nervous breakdown',
'non-graduate', 'non graduate', 'nongraduate', 'certificate listed as ng',
}
}


Line 451: Line 456:
EE = 'Estonia',
EE = 'Estonia',
LA = 'Latvia',
LA = 'Latvia',
LAT = 'Latvia',
LI = 'Lithuania',
LI = 'Lithuania',
LT = 'Lithuania',
LT = 'Lithuania',
LV = 'Latvia',
LV = 'Latvia',
AL = 'Albania',
AL = 'Albania',
ALB = 'Albania',
BU = 'Bulgaria',
BU = 'Bulgaria',
BUL = 'Bulgaria',
BG = 'Bulgaria',
BG = 'Bulgaria',
LUX = 'Luxembourg',
BRA = 'Brazil',
IR = 'Ireland',
IR = 'Ireland',
IE = 'Ireland',
IE = 'Ireland',
PA = 'Palestine',
PA = 'Palestine',
PAL = 'Palestine',
EG = 'Egypt',
EG = 'Egypt',
JA = 'Japan',
JA = 'Japan',
Line 466: Line 477:
BR = 'Brazil',
BR = 'Brazil',
AR = 'Argentina',
AR = 'Argentina',
ARG = 'Argentina',
PE = 'Peru',
PE = 'Peru',
CO = 'Colombia',
CO = 'Colombia',
COL = 'Colombia',
LU = 'Luxembourg',
LU = 'Luxembourg',
ME = 'Mexico',
CHN = 'China',
LITH = 'Lithuania',
PRTG = 'Portugal',
PT = 'Portugal',
DR = 'Dominican Republic',
DO = 'Dominican Republic',
OTH = 'Other',
OTH = 'Other',
OTHER = 'Other',
OTHER = 'Other',
Line 494: Line 514:
ISLAM = 'Muslim',
ISLAM = 'Muslim',
Q = 'Quaker',
Q = 'Quaker',
QU = 'Quaker',
QUA = 'Quaker',
QUAKER = 'Quaker',
FRIENDS = 'Quaker',
N = 'None',
N = 'None',
X = 'Not stated',
X = 'Not stated',
Line 663: Line 687:
key = mw.ustring.gsub(key, '%s+', ' ')
key = mw.ustring.gsub(key, '%s+', ' ')
key = trim(key)
key = trim(key)
if RELIGION_NAME[key] then
local mapped = RELIGION_NAME[key]
return RELIGION_NAME[key]
if mapped then
-- Hide non-informative roster placeholders from the infobox.
if mapped == 'Not stated' or mapped == 'None' or mapped == 'Unknown' then
return ''
end
return mapped
end
end
local lower = mw.ustring.lower(s)
local lower = mw.ustring.lower(s)
if lower == 'not stated' or lower == 'none' or lower == 'unknown'
or lower == 'n/a' or lower == 'na' then
return ''
end
if lower == 'hebrew' or lower == 'jewish' or lower == 'jew'
if lower == 'hebrew' or lower == 'jewish' or lower == 'jew'
or mw.ustring.find(lower, 'jewish', 1, true)
or mw.ustring.find(lower, 'jewish', 1, true)
or mw.ustring.find(lower, 'hebrew', 1, true) then
or mw.ustring.find(lower, 'hebrew', 1, true) then
return 'Jewish'
return 'Jewish'
end
if lower == 'quaker' or lower == 'friends' then
return 'Quaker'
end
end
return prettyProse(s)
return prettyProse(s)
end
end


local function prettyNamePart(s)
-- Roster "other_class" codes → readable course names (By The Numbers / MITC charts).
return prettyNamePhrase(s, false)
local function expandOtherClassToken(tok)
end
tok = tidyDisplay(tok)
if tok == '' then
return ''
end
local u = mw.ustring.upper(tok)
u = mw.ustring.gsub(u, '%s+', ' ')
u = trim(u)


local function isNegativeClause(s)
local n = mw.ustring.match(u, '^SC%s*B%s*(%d+)$')
local lower = mw.ustring.lower(s)
if n then
if lower == 'failed' or mw.ustring.find(lower, '^failed%s') or mw.ustring.find(lower, '%sfailed%s') or mw.ustring.find(lower, '%sfailed$') then
return 'Special Course B ' .. n
return true
end
n = mw.ustring.match(u, '^SC%s*(%d+)$')
if n then
-- SC# aligns with AGF Photo Interpretation / “Special Course” numbering
-- (e.g. SC8 = Special Course 8 in PI).
return 'Special Course ' .. n
end
n = mw.ustring.match(u, '^DC%s*(%d+)$')
if n then
return 'Division Class ' .. n
end
n = mw.ustring.match(u, '^MITU%s*(%d+)$')
if n then
return 'Mobile Intelligence Training Unit ' .. n
end
n = mw.ustring.match(u, '^RDC%s*(%d+)$')
if n then
return 'Redeployment Class ' .. n
end
n = mw.ustring.match(u, '^RCM%s*(%d+)$')
if n then
return 'Radio Countermeasures Class ' .. n
end
n = mw.ustring.match(u, '^OB%s*(%d+)$')
if n then
return 'Order of Battle Class ' .. n
end
end
for _, phrase in ipairs(NEGATIVE_PHRASES) do
if u == 'SIG' or u == 'SIG-SIG' or mw.ustring.match(u, '^SIG[%s%-]') then
if mw.ustring.find(lower, phrase, 1, true) then
return 'Special Intelligence Group–Signal'
return true
end
end
end
return false
end


local function normalizeNoteKey(s)
local unitNum, unitKind = mw.ustring.match(u, '^(%d+)(?:ST|ND|RD|TH)%s+(.+)$')
s = mw.ustring.lower(trim(tidyDisplay(s)))
if unitNum and unitKind then
s = mw.ustring.gsub(s, '^also%s+', '')
local kind = trim(unitKind)
s = mw.ustring.gsub(s, '[^%w%s%-/]', ' ')
if kind == 'INF' then
s = mw.ustring.gsub(s, '%s+', ' ')
kind = 'Infantry'
return trim(s)
elseif kind == 'ARMD' then
end
kind = 'Armored'
 
else
local function dateOnlyFragment(s)
kind = prettyProse(kind)
return mw.ustring.match(trim(s), '^(%d%d?/%d%d?/%d%d%d?%d?)%.?$')
end
end
local ord = unitNum
 
local last = mw.ustring.sub(unitNum, -1)
local function joinDateFollowons(items)
local last2 = mw.ustring.len(unitNum) >= 2 and mw.ustring.sub(unitNum, -2) or ''
local out = {}
if last2 ~= '11' and last2 ~= '12' and last2 ~= '13' then
for _, clause in ipairs(items) do
if last == '1' then
local date = dateOnlyFragment(clause)
ord = unitNum .. 'st'
if date and #out > 0 then
elseif last == '2' then
local prev = mw.ustring.gsub(out[#out], '[ ,;]+$', '')
ord = unitNum .. 'nd'
if mw.ustring.sub(prev, -mw.ustring.len(date)) ~= date then
elseif last == '3' then
out[#out] = prev .. ', ' .. date
ord = unitNum .. 'rd'
else
ord = unitNum .. 'th'
end
end
elseif clause ~= '' then
else
table.insert(out, clause)
ord = unitNum .. 'th'
end
end
return ord .. ' ' .. kind
end
end
return out
 
return prettyProse(tok)
end
end


local function sanitizeNote(s)
local function expandOtherClass(raw)
s = tidyDisplay(s)
raw = tidyDisplay(raw)
if s == '' then
if raw == '' then
return ''
return ''
end
end
local lower = mw.ustring.lower(s)
local parts = {}
if mw.ustring.find(lower, 'no notes were in the source', 1, true) then
for piece in mw.ustring.gmatch(raw, '[^;,]+') do
return ''
local expanded = expandOtherClassToken(piece)
end
if expanded ~= '' then
local kept = {}
table.insert(parts, expanded)
for clause in mw.text.gsplit(s, ';', true) do
clause = trim(clause)
if clause ~= '' then
for sent in mw.text.gsplit(clause, '. ', true) do
sent = trim(sent)
sent = mw.ustring.gsub(sent, '%.$', '')
if sent ~= '' and not isNegativeClause(sent) then
table.insert(kept, prettyProse(sent))
end
end
end
end
end
end
return table.concat(kept, '; ')
return table.concat(parts, '; ')
end
end


local function containsIgnoreCase(haystack, needle)
local function prettyNamePart(s)
return mw.ustring.find(mw.ustring.lower(haystack), mw.ustring.lower(needle), 1, true) ~= nil
return prettyNamePhrase(s, false)
end
end


local function displayName(frame)
local function isNegativeClause(s)
local first = prettyNamePart(parentArg(frame, 'first'))
local lower = mw.ustring.lower(s)
local middle = prettyNamePart(parentArg(frame, 'middle'))
if lower == 'failed' or mw.ustring.find(lower, '^failed%s') or mw.ustring.find(lower, '%sfailed%s') or mw.ustring.find(lower, '%sfailed$') then
local surname = prettyNamePart(parentArg(frame, 'surname'))
return true
local suffix = prettyNamePart(parentArg(frame, 'suffix'))
local parts = {}
if first ~= '' then
table.insert(parts, first)
end
end
if middle ~= '' then
-- Bare class-chart non-graduate marker (e.g. "NG Class 10 OB", trailing "NG")
table.insert(parts, middle)
if lower == 'ng' or mw.ustring.find(lower, '^ng%s') or mw.ustring.find(lower, '%sng%s') or mw.ustring.find(lower, '%sng$') or mw.ustring.find(lower, 'nongrad') then
return true
end
end
if surname ~= '' then
for _, phrase in ipairs(NEGATIVE_PHRASES) do
table.insert(parts, surname)
if mw.ustring.find(lower, phrase, 1, true) then
end
return true
local name = table.concat(parts, ' ')
if suffix ~= '' then
if name ~= '' then
name = name .. ', ' .. suffix
else
name = suffix
end
end
end
end
if name == '' then
return false
name = 'This soldier'
end
end
 
return name
local function normalizeNoteKey(s)
s = mw.ustring.lower(trim(tidyDisplay(s)))
s = mw.ustring.gsub(s, '^also%s+', '')
s = mw.ustring.gsub(s, '[^%w%s%-/]', ' ')
s = mw.ustring.gsub(s, '%s+', ' ')
return trim(s)
end
end


local function classLinkList(raw, withPrefix)
local function dateOnlyFragment(s)
local out = {}
return mw.ustring.match(trim(s), '^(%d%d?/%d%d?/%d%d%d?%d?)%.?$')
for part in mw.text.gsplit(raw, ';', true) do
part = trim(part)
if part ~= '' then
if withPrefix then
table.insert(out, string.format('[[Class %s]]', part))
else
table.insert(out, string.format('[[Class %s|%s]]', part, part))
end
end
end
return out
end
end


local function isJunkIpwItalianLabel(part)
local function joinDateFollowons(items)
local lower = mw.ustring.lower(trim(part))
local out = {}
local spaced = mw.ustring.gsub(lower, '%s+', ' ')
for _, clause in ipairs(items) do
local junk = {
local date = dateOnlyFragment(clause)
['16063'] = true, ['16095'] = true, ['16302'] = true,
if date and #out > 0 then
['16320'] = true, ['16695'] = true,
local prev = mw.ustring.gsub(out[#out], '[ ,;]+$', '')
['1st army'] = true, ['2 day'] = true, ['2/23/44'] = true,
if mw.ustring.sub(prev, -mw.ustring.len(date)) ~= date then
['468th cic'] = true, ['5/7/45'] = true, ['8/24/44'] = true, ['8/7/43'] = true,
out[#out] = prev .. ', ' .. date
['apid'] = true, ['i-32'] = true, ['mis-eto'] = true, ['mitc'] = true,
end
['pac'] = true, ['sc7'] = true, ['shaef'] = true, ['v-831'] = true,
elseif clause ~= '' then
}
table.insert(out, clause)
return junk[spaced] == true
end
end
return out
end
end


local function normalizeIpwItalianCode(part)
local function sanitizeNote(s)
part = trim(part)
s = tidyDisplay(s)
if part == '' or isJunkIpwItalianLabel(part) then
if s == '' then
return nil
return ''
end
local lower = mw.ustring.lower(s)
if mw.ustring.find(lower, 'no notes were in the source', 1, true) then
return ''
end
end
local spaced = mw.ustring.lower(mw.ustring.gsub(part, '%s+', ' '))
local kept = {}
if spaced == '5th class' or spaced == 'italian 5th class' then
for clause in mw.text.gsplit(s, ';', true) do
return '5'
clause = trim(clause)
if clause ~= '' then
for sent in mw.text.gsplit(clause, '. ', true) do
sent = trim(sent)
sent = mw.ustring.gsub(sent, '%.$', '')
if sent ~= '' and not isNegativeClause(sent) then
table.insert(kept, prettyProse(sent))
end
end
end
end
end
return part
return table.concat(kept, '; ')
end
end


local function isJunkIpwLabel(part)
local function containsIgnoreCase(haystack, needle)
local lower = mw.ustring.lower(trim(part))
return mw.ustring.find(mw.ustring.lower(haystack), mw.ustring.lower(needle), 1, true) ~= nil
lower = mw.ustring.gsub(lower, '%s+', ' ')
end
lower = mw.ustring.gsub(lower, '%s*%?+%s*$', '')
 
lower = trim(lower)
local function displayName(frame)
if lower == 'csu' or lower == 'fh' or lower == 'fid' or lower == 'illness'
local first = prettyNamePart(parentArg(frame, 'first'))
or lower == 'mfiu-5' or lower == 'mfiu 5' or lower == 'mitu 5' or lower == 'mitu-5'
local middle = prettyNamePart(parentArg(frame, 'middle'))
or lower == 'mrbc4' or lower == 'mrbc 4' or lower == 't force' or lower == 'to cic' then
local surname = prettyNamePart(parentArg(frame, 'surname'))
return true
local suffix = prettyNamePart(parentArg(frame, 'suffix'))
local parts = {}
if first ~= '' then
table.insert(parts, first)
end
end
local compact = mw.ustring.gsub(lower, '[%s%-]+', '')
if middle ~= '' then
if compact == 'docteam' or compact == 'mfiu5' or compact == 'mitu5'
table.insert(parts, middle)
or compact == 'mrbc4' or compact == 'tforce' or compact == 'tocic' then
return true
end
end
return false
if surname ~= '' then
end
table.insert(parts, surname)
 
local function normalizeIpwTeamCode(part)
part = trim(part)
if part == '' then
return nil, nil
end
end
local original = part
local name = table.concat(parts, ' ')
part = mw.ustring.gsub(part, '%s*%?+%s*$', '')
if suffix ~= '' then
part = trim(part)
if name ~= '' then
if part == '' then
name = name .. ', ' .. suffix
return nil, original
else
name = suffix
end
end
end
local lower = mw.ustring.lower(part)
if name == '' then
if mw.ustring.find(lower, 'armd', 1, true) or mw.ustring.find(lower, 'armored', 1, true) then
name = 'This soldier'
return nil, original, nil
end
end
local miiNum, miiLetter = mw.ustring.match(part, '^(%d+)%s*[%-–—]%s*([A-Za-z])$')
return name
if miiNum then
end
miiLetter = mw.ustring.upper(miiLetter)
 
local mii = miiNum .. '-' .. miiLetter
local function classLinkList(raw, withPrefix)
if miiLetter == 'R' and (miiNum == '540' or miiNum == '545' or miiNum == '546') then
local ClassDates = require('Module:ClassDates')
mii = miiNum
end
return nil, mii, mii
end
local num = mw.ustring.match(part, '^(%d+)[Tt]$')
if num then
return num, num, nil
end
if mw.ustring.match(lower, '^%d+(st|nd|rd|th)$') then
return nil, original, nil
end
if isJunkIpwLabel(part) then
return nil, original, nil
end
return part, part, nil
end
 
local function teamLinkList(raw, pagePrefix, label)
local out = {}
local out = {}
local isIpw = mw.ustring.find(pagePrefix, 'IPW', 1, true) == 1
for part in mw.text.gsplit(raw, ';', true) do
for part in mw.text.gsplit(raw, ';', true) do
part = trim(part)
part = mw.ustring.gsub(part, '[#<>%[%]|{}]', '')
part = trim(part)
part = trim(part)
if part ~= '' then
if part ~= '' then
if pagePrefix == 'IPW Italian ' then
local info = ClassDates.lookup(part)
local code = normalizeIpwItalianCode(part)
local dates = info and info.dates or ''
if code then
if withPrefix then
table.insert(out, string.format('[[Team %s%s|%s %s]]', pagePrefix, code, label, code))
if dates ~= '' then
table.insert(out, string.format('[[:Category:Class %s|Class %s]] (%s)', part, part, dates))
else
else
table.insert(out, part)
table.insert(out, string.format('[[:Category:Class %s|Class %s]]', part, part))
end
end
elseif isIpw then
else
local code, shown, mii = normalizeIpwTeamCode(part)
if dates ~= '' then
if mii then
table.insert(out, string.format('[[:Category:Class %s|%s]] (%s)', part, part, dates))
table.insert(out, string.format('[[Team MII %s|MII %s]]', mii, mii))
else
elseif code then
table.insert(out, string.format('[[:Category:Class %s|%s]]', part, part))
table.insert(out, string.format('[[Team %s%s|%s %s]]', pagePrefix, code, label, shown))
elseif shown then
table.insert(out, shown)
end
end
else
table.insert(out, string.format('[[Team %s%s|%s %s]]', pagePrefix, part, label, part))
end
end
end
end
end
end
return table.concat(out, ', ')
return out
end
end


local function addHeader(tbl, text)
local function isJunkIpwItalianLabel(part)
tbl:tag('tr'):tag('th')
local lower = mw.ustring.lower(trim(part))
:attr('colspan', '2')
local spaced = mw.ustring.gsub(lower, '%s+', ' ')
:addClass('infobox-header')
local junk = {
:wikitext(text)
['16063'] = true, ['16095'] = true, ['16302'] = true,
end
['16320'] = true, ['16695'] = true,
 
['1st army'] = true, ['2 day'] = true, ['2/23/44'] = true,
local function realCertificate(s)
['468th cic'] = true, ['5/7/45'] = true, ['8/24/44'] = true, ['8/7/43'] = true,
s = trim(s)
['apid'] = true, ['i-32'] = true, ['mis-eto'] = true, ['mitc'] = true,
if s == '' then
['pac'] = true, ['sc7'] = true, ['shaef'] = true, ['v-831'] = true,
return ''
}
return junk[spaced] == true
end
 
local function normalizeIpwItalianCode(part)
part = trim(part)
if part == '' or isJunkIpwItalianLabel(part) then
return nil
end
end
-- Placeholders: *, A, 5, #, and any other single character.
local spaced = mw.ustring.lower(mw.ustring.gsub(part, '%s+', ' '))
if mw.ustring.len(s) == 1 then
if spaced == '5th class' or spaced == 'italian 5th class' then
return ''
return '5'
end
end
return s
return part
end
end


local function addRow(tbl, label, value, dataClass)
local function isJunkIpwLabel(part)
if trim(value) == '' then
local lower = mw.ustring.lower(trim(part))
return
lower = mw.ustring.gsub(lower, '%s+', ' ')
lower = mw.ustring.gsub(lower, '%s*%?+%s*$', '')
lower = trim(lower)
if lower == 'csu' or lower == 'fh' or lower == 'fid' or lower == 'illness'
or lower == 'mfiu-5' or lower == 'mfiu 5' or lower == 'mitu 5' or lower == 'mitu-5'
or lower == 'mrbc4' or lower == 'mrbc 4' or lower == 't force' or lower == 'to cic' then
return true
end
end
local tr = tbl:tag('tr')
local compact = mw.ustring.gsub(lower, '[%s%-]+', '')
tr:tag('th')
if compact == 'docteam' or compact == 'mfiu5' or compact == 'mitu5'
:addClass('infobox-label')
or compact == 'mrbc4' or compact == 'tforce' or compact == 'tocic' then
:css('display', 'table-cell')
return true
:css('width', '38%')
:css('font-weight', 'bold')
:css('text-align', 'left')
:css('vertical-align', 'top')
:wikitext(label)
local td = tr:tag('td')
:addClass('infobox-data')
:css('display', 'table-cell')
:css('vertical-align', 'top')
if dataClass and dataClass ~= '' then
td:addClass(dataClass)
end
end
td:wikitext(value)
return false
end
end


local function yearOnly(s)
local function normalizeIpwTeamCode(part)
s = trim(s)
part = trim(part)
if s == '' or s == '?' then
if part == '' then
return ''
return nil, nil
end
local original = part
part = mw.ustring.gsub(part, '%s*%?+%s*$', '')
part = trim(part)
if part == '' then
return nil, original
end
end
local year = mw.ustring.match(s, '(%d%d%d%d)')
local lower = mw.ustring.lower(part)
if year then
if mw.ustring.find(lower, 'armd', 1, true) or mw.ustring.find(lower, 'armored', 1, true) then
return year
return nil, original, nil
end
end
return ''
local miiNum, miiLetter = mw.ustring.match(part, '^(%d+)%s*[%-–—]%s*([A-Za-z])$')
end
if miiNum then
 
miiLetter = mw.ustring.upper(miiLetter)
local RANK_MAP = {
local mii = miiNum .. '-' .. miiLetter
['PVT'] = 'Private',
if miiLetter == 'R' and (miiNum == '540' or miiNum == '545' or miiNum == '546') then
['PVTE'] = 'Private',
mii = miiNum
['PRIVATE'] = 'Private',
end
['PFC'] = 'Private First Class',
return nil, mii, mii
['P F C'] = 'Private First Class',
end
['PRIVATE FIRST CLASS'] = 'Private First Class',
local num = mw.ustring.match(part, '^(%d+)[Tt]$')
['CPL'] = 'Corporal',
if num then
['CORPORAL'] = 'Corporal',
return num, num, nil
['T/5'] = 'Technician Fifth Grade',
end
['TEC5'] = 'Technician Fifth Grade',
if mw.ustring.match(lower, '^%d+(st|nd|rd|th)$') then
['TEC 5'] = 'Technician Fifth Grade',
return nil, original, nil
['T5'] = 'Technician Fifth Grade',
end
['TECH 5'] = 'Technician Fifth Grade',
if isJunkIpwLabel(part) then
['SGT'] = 'Sergeant',
return nil, original, nil
['SERGEANT'] = 'Sergeant',
end
['T/4'] = 'Technician Fourth Grade',
return part, part, nil
['TEC4'] = 'Technician Fourth Grade',
end
['TEC 4'] = 'Technician Fourth Grade',
 
['T4'] = 'Technician Fourth Grade',
local function teamLinkList(raw, pagePrefix, label)
['TECH 4'] = 'Technician Fourth Grade',
local out = {}
['S/SGT'] = 'Staff Sergeant',
local isIpw = mw.ustring.find(pagePrefix, 'IPW', 1, true) == 1
['SSGT'] = 'Staff Sergeant',
for part in mw.text.gsplit(raw, ';', true) do
['S SGT'] = 'Staff Sergeant',
part = trim(part)
['STAFF SERGEANT'] = 'Staff Sergeant',
part = mw.ustring.gsub(part, '[#<>%[%]|{}]', '')
['T/3'] = 'Technician Third Grade',
part = trim(part)
['TEC3'] = 'Technician Third Grade',
if part ~= '' then
['TEC 3'] = 'Technician Third Grade',
if pagePrefix == 'IPW Italian ' then
['T3'] = 'Technician Third Grade',
local code = normalizeIpwItalianCode(part)
['TECH 3'] = 'Technician Third Grade',
if code then
['T/SGT'] = 'Technical Sergeant',
table.insert(out, string.format('[[Team %s%s|%s %s]]', pagePrefix, code, label, code))
['TSGT'] = 'Technical Sergeant',
else
['T SGT'] = 'Technical Sergeant',
table.insert(out, part)
['TECHNICAL SERGEANT'] = 'Technical Sergeant',
end
['1ST SGT'] = 'First Sergeant',
elseif isIpw then
['1SGT'] = 'First Sergeant',
local code, shown, mii = normalizeIpwTeamCode(part)
['FIRST SERGEANT'] = 'First Sergeant',
if mii then
['M/SGT'] = 'Master Sergeant',
table.insert(out, string.format('[[Team MII %s|MII %s]]', mii, mii))
['MSGT'] = 'Master Sergeant',
elseif code then
['MASTER SERGEANT'] = 'Master Sergeant',
table.insert(out, string.format('[[Team %s%s|%s %s]]', pagePrefix, code, label, shown))
['WO'] = 'Warrant Officer',
elseif shown then
['WOJG'] = 'Warrant Officer',
table.insert(out, shown)
['WARRANT OFFICER'] = 'Warrant Officer',
end
['CWO'] = 'Chief Warrant Officer',
else
['2ND LT'] = 'Second Lieutenant',
table.insert(out, string.format('[[Team %s%s|%s %s]]', pagePrefix, part, label, part))
['2D LT'] = 'Second Lieutenant',
end
['2LT'] = 'Second Lieutenant',
end
['SECOND LIEUTENANT'] = 'Second Lieutenant',
end
['1ST LT'] = 'First Lieutenant',
return table.concat(out, ', ')
['1LT'] = 'First Lieutenant',
end
['FIRST LIEUTENANT'] = 'First Lieutenant',
 
['LT'] = 'Lieutenant',
local function addHeader(tbl, text)
['LIEUTENANT'] = 'Lieutenant',
tbl:tag('tr'):tag('th')
['CAPT'] = 'Captain',
:attr('colspan', '2')
['CPT'] = 'Captain',
:addClass('infobox-header')
['CAPTAIN'] = 'Captain',
:wikitext(text)
['MAJ'] = 'Major',
end
['MAJOR'] = 'Major',
['LT COL'] = 'Lieutenant Colonel',
['LTCOL'] = 'Lieutenant Colonel',
['LTC'] = 'Lieutenant Colonel',
['LIEUTENANT COLONEL'] = 'Lieutenant Colonel',
['COL'] = 'Colonel',
['COLONEL'] = 'Colonel',
['BRIG GEN'] = 'Brigadier General',
['BG'] = 'Brigadier General',
['BRIGADIER GENERAL'] = 'Brigadier General',
['MAJ GEN'] = 'Major General',
['MG'] = 'Major General',
['MAJOR GENERAL'] = 'Major General',
['LT GEN'] = 'Lieutenant General',
['LIEUTENANT GENERAL'] = 'Lieutenant General',
['GEN'] = 'General',
['GENERAL'] = 'General',
['TECH'] = 'Technician',
['TEC'] = 'Technician',
}


local function expandRank(raw)
local function realCertificate(s)
raw = trim(raw)
s = trim(s)
if raw == '' then
if s == '' then
return ''
return ''
end
end
local key = mw.ustring.upper(raw)
-- Placeholders: *, A, 5, #, and any other single character.
key = mw.ustring.gsub(key, '%.', '')
if mw.ustring.len(s) == 1 then
key = mw.ustring.gsub(key, '%s+', ' ')
return ''
key = trim(key)
end
return RANK_MAP[key] or raw
-- NG = non-graduate marker on class charts, not a certificate number.
local upper = mw.ustring.upper(s)
if upper == 'NG' or upper == 'N/G' or upper == 'N.G.' then
return ''
end
return s
end
end


local BRANCH_MAP = {
local function addRow(tbl, label, value, dataClass)
['INF'] = 'Infantry',
if trim(value) == '' then
['INFANTRY'] = 'Infantry',
return
['FA'] = 'Field Artillery',
end
['FIELD ARTILLERY'] = 'Field Artillery',
local tr = tbl:tag('tr')
['CAC'] = 'Coast Artillery Corps',
tr:tag('th')
['CA'] = 'Coast Artillery',
:addClass('infobox-label')
['COAST ARTILLERY'] = 'Coast Artillery',
:css('display', 'table-cell')
['AC'] = 'Air Corps',
:css('width', '38%')
['AAF'] = 'Army Air Forces',
:css('font-weight', 'bold')
['AIR CORPS'] = 'Air Corps',
:css('text-align', 'left')
['QMC'] = 'Quartermaster Corps',
:css('vertical-align', 'top')
['QC'] = 'Quartermaster Corps',
:wikitext(label)
['QUARTERMASTER'] = 'Quartermaster Corps',
local td = tr:tag('td')
['CAV'] = 'Cavalry',
:addClass('infobox-data')
['CAVALRY'] = 'Cavalry',
:css('display', 'table-cell')
['CE'] = 'Corps of Engineers',
:css('vertical-align', 'top')
['ENG'] = 'Corps of Engineers',
if dataClass and dataClass ~= '' then
['ENGINEERS'] = 'Corps of Engineers',
td:addClass(dataClass)
['CMP'] = 'Corps of Military Police',
end
['MP'] = 'Military Police',
td:wikitext(value)
['CWS'] = 'Chemical Warfare Service',
end
['AUS'] = 'Army of the United States',
 
['SC'] = 'Signal Corps',
local function yearOnly(s)
['SIG'] = 'Signal Corps',
s = trim(s)
['SIG C'] = 'Signal Corps',
if s == '' or s == '?' then
['SIGC'] = 'Signal Corps',
return ''
['SIGNAL CORPS'] = 'Signal Corps',
end
['ORD'] = 'Ordnance Department',
local year = mw.ustring.match(s, '(%d%d%d%d)')
['ORDNANCE'] = 'Ordnance Department',
if year then
['MAC'] = 'Medical Administrative Corps',
return year
['MC'] = 'Medical Corps',
end
['MD'] = 'Medical Department',
return ''
['TC'] = 'Transportation Corps',
end
['MI'] = 'Military Intelligence',
 
['MIS'] = 'Military Intelligence Service',
local RANK_MAP = {
['INT'] = 'Intelligence',
['PVT'] = 'Private',
['CIC'] = 'Counter Intelligence Corps',
['PVTE'] = 'Private',
['AGD'] = "Adjutant General's Department",
['PRIVATE'] = 'Private',
['USMC'] = 'United States Marine Corps',
['PFC'] = 'Private First Class',
['USMCR'] = 'United States Marine Corps Reserve',
['P F C'] = 'Private First Class',
['AA'] = 'Antiaircraft',
['PRIVATE FIRST CLASS'] = 'Private First Class',
['AAA'] = 'Antiaircraft Artillery',
['CPL'] = 'Corporal',
['TD'] = 'Tank Destroyer',
['CORPORAL'] = 'Corporal',
['FD'] = 'Finance Department',
['T/5'] = 'Technician Fifth Grade',
['GSC'] = 'General Staff Corps',
['TEC5'] = 'Technician Fifth Grade',
['ARMD F'] = 'Armored Force',
['TEC 5'] = 'Technician Fifth Grade',
['ARMD'] = 'Armored',
['T5'] = 'Technician Fifth Grade',
['ARMOR'] = 'Armor',
['TECH 5'] = 'Technician Fifth Grade',
['SSF'] = 'Special Service Force',
['SGT'] = 'Sergeant',
['PA'] = 'Philippine Army',
['SERGEANT'] = 'Sergeant',
['RCE'] = 'Royal Canadian Engineers',
['T/4'] = 'Technician Fourth Grade',
['RCA'] = 'Royal Canadian Artillery',
['TEC4'] = 'Technician Fourth Grade',
['CHC'] = 'Chaplain Corps',
['TEC 4'] = 'Technician Fourth Grade',
['CHP'] = 'Chaplain Corps',
['T4'] = 'Technician Fourth Grade',
['CAO'] = 'Civil Affairs',
['TECH 4'] = 'Technician Fourth Grade',
['CAU'] = 'Civil Affairs',
['S/SGT'] = 'Staff Sergeant',
}
['SSGT'] = 'Staff Sergeant',
 
['S SGT'] = 'Staff Sergeant',
local function expandBranchToken(raw)
['STAFF SERGEANT'] = 'Staff Sergeant',
raw = trim(raw)
['T/3'] = 'Technician Third Grade',
if raw == '' then
['TEC3'] = 'Technician Third Grade',
return ''
['TEC 3'] = 'Technician Third Grade',
end
['T3'] = 'Technician Third Grade',
local key = mw.ustring.upper(raw)
['TECH 3'] = 'Technician Third Grade',
key = mw.ustring.gsub(key, '%.', '')
['T/SGT'] = 'Technical Sergeant',
key = mw.ustring.gsub(key, '%s+', ' ')
['TSGT'] = 'Technical Sergeant',
key = trim(key)
['T SGT'] = 'Technical Sergeant',
if BRANCH_MAP[key] then
['TECHNICAL SERGEANT'] = 'Technical Sergeant',
return BRANCH_MAP[key]
['1ST SGT'] = 'First Sergeant',
end
['1SGT'] = 'First Sergeant',
local left, right = mw.ustring.match(key, '^(.+) %((.+)%)$')
['FIRST SERGEANT'] = 'First Sergeant',
if left and right then
['M/SGT'] = 'Master Sergeant',
left = trim(left)
['MSGT'] = 'Master Sergeant',
right = trim(right)
['MASTER SERGEANT'] = 'Master Sergeant',
local leftExp = BRANCH_MAP[left] or left
['WO'] = 'Warrant Officer',
local rightExp = BRANCH_MAP[right] or right
['WOJG'] = 'Warrant Officer',
if BRANCH_MAP[left] or BRANCH_MAP[right] then
['WARRANT OFFICER'] = 'Warrant Officer',
return leftExp .. ' (' .. rightExp .. ')'
['CWO'] = 'Chief Warrant Officer',
end
['2ND LT'] = 'Second Lieutenant',
end
['2D LT'] = 'Second Lieutenant',
return raw
['2LT'] = 'Second Lieutenant',
end
['SECOND LIEUTENANT'] = 'Second Lieutenant',
 
['1ST LT'] = 'First Lieutenant',
-- Small public-domain / Commons emblems uploaded as File:Branch emblem *.png
['1LT'] = 'First Lieutenant',
local BRANCH_EMBLEM = {
['FIRST LIEUTENANT'] = 'First Lieutenant',
['United States Army'] = 'Branch emblem US Army.png',
['LT'] = 'Lieutenant',
['United States Marine Corps'] = 'Branch emblem US Marine Corps.png',
['LIEUTENANT'] = 'Lieutenant',
['United States Navy'] = 'Branch emblem US Navy.png',
['CAPT'] = 'Captain',
['United States Army Air Forces'] = 'Branch emblem US Army Air Forces.png',
['CPT'] = 'Captain',
['United States Air Force'] = 'Branch emblem US Air Force.png',
['CAPTAIN'] = 'Captain',
['United States Coast Guard'] = 'Branch emblem US Coast Guard.png',
['MAJ'] = 'Major',
}
['MAJOR'] = 'Major',
['LT COL'] = 'Lieutenant Colonel',
['LTCOL'] = 'Lieutenant Colonel',
['LTC'] = 'Lieutenant Colonel',
['LIEUTENANT COLONEL'] = 'Lieutenant Colonel',
['COL'] = 'Colonel',
['COLONEL'] = 'Colonel',
['BRIG GEN'] = 'Brigadier General',
['BG'] = 'Brigadier General',
['BRIGADIER GENERAL'] = 'Brigadier General',
['MAJ GEN'] = 'Major General',
['MG'] = 'Major General',
['MAJOR GENERAL'] = 'Major General',
['LT GEN'] = 'Lieutenant General',
['LIEUTENANT GENERAL'] = 'Lieutenant General',
['GEN'] = 'General',
['GENERAL'] = 'General',
['TECH'] = 'Technician',
['TEC'] = 'Technician',
}


local function branchWithEmblem(name)
local function expandRank(raw)
name = trim(name)
raw = trim(raw)
if name == '' then
if raw == '' then
return ''
return ''
end
end
local file = BRANCH_EMBLEM[name]
local key = mw.ustring.upper(raw)
if file then
key = mw.ustring.gsub(key, '%.', '')
return '[[File:' .. file .. '|18px|link=|alt=]] ' .. name
key = mw.ustring.gsub(key, '%s+', ' ')
end
key = trim(key)
-- Fuzzy contains
return RANK_MAP[key] or raw
local low = mw.ustring.lower(name)
if mw.ustring.find(low, 'marine', 1, true) then
return '[[File:Branch emblem US Marine Corps.png|18px|link=|alt=]] ' .. name
end
if mw.ustring.find(low, 'navy', 1, true) then
return '[[File:Branch emblem US Navy.png|18px|link=|alt=]] ' .. name
end
if mw.ustring.find(low, 'air force', 1, true) or mw.ustring.find(low, 'air forces', 1, true) then
return '[[File:Branch emblem US Army Air Forces.png|18px|link=|alt=]] ' .. name
end
if mw.ustring.find(low, 'coast guard', 1, true) then
return '[[File:Branch emblem US Coast Guard.png|18px|link=|alt=]] ' .. name
end
if mw.ustring.find(low, 'army', 1, true) then
return '[[File:Branch emblem US Army.png|18px|link=|alt=]] ' .. name
end
return name
end
end


local function expandBranch(raw)
local BRANCH_MAP = {
raw = trim(raw)
['INF'] = 'Infantry',
if raw == '' then
['INFANTRY'] = 'Infantry',
return ''
['FA'] = 'Field Artillery',
end
['FIELD ARTILLERY'] = 'Field Artillery',
local parts = {}
['CAC'] = 'Coast Artillery Corps',
for part in mw.text.gsplit(raw, ';', true) do
['CA'] = 'Coast Artillery',
part = expandBranchToken(part)
['COAST ARTILLERY'] = 'Coast Artillery',
if part ~= '' then
['AC'] = 'Air Corps',
table.insert(parts, branchWithEmblem(part))
['AAF'] = 'Army Air Forces',
end
['AIR CORPS'] = 'Air Corps',
end
['QMC'] = 'Quartermaster Corps',
return table.concat(parts, '; ')
['QC'] = 'Quartermaster Corps',
end
['QUARTERMASTER'] = 'Quartermaster Corps',
 
['CAV'] = 'Cavalry',
local function formatBattles(raw)
['CAVALRY'] = 'Cavalry',
raw = trim(raw)
['CALVALRY'] = 'Cavalry',
if raw == '' then
['CE'] = 'Corps of Engineers',
return ''
['ENG'] = 'Corps of Engineers',
end
['ENGINEERS'] = 'Corps of Engineers',
-- Allow HTML <br /> or semicolon / newline separated campaigns
['CMP'] = 'Corps of Military Police',
raw = mw.ustring.gsub(raw, '\r', '')
['MP'] = 'Military Police',
if mw.ustring.find(raw, '<br', 1, true) or mw.ustring.find(raw, '\n', 1, true) then
['CWS'] = 'Chemical Warfare Service',
return raw
['AUS'] = 'Army of the United States',
end
['SC'] = 'Signal Corps',
local parts = {}
['SIG'] = 'Signal Corps',
for part in mw.text.gsplit(raw, ';', true) do
['SIG C'] = 'Signal Corps',
part = trim(part)
['SIGC'] = 'Signal Corps',
if part ~= '' then
['SIGNAL CORPS'] = 'Signal Corps',
table.insert(parts, part)
['ORD'] = 'Ordnance Department',
end
['ORDNANCE'] = 'Ordnance Department',
end
['MAC'] = 'Medical Administrative Corps',
if #parts <= 1 then
['MC'] = 'Medical Corps',
return raw
['MD'] = 'Medical Department',
end
['TC'] = 'Transportation Corps',
return table.concat(parts, '<br />')
['MI'] = 'Military Intelligence',
end
['MIS'] = 'Military Intelligence Service',
 
['INT'] = 'Intelligence',
local function splitAwardRows(raw)
['CIC'] = 'Counter Intelligence Corps',
raw = trim(raw)
['AGD'] = "Adjutant General's Department",
if raw == '' then
['USMC'] = 'United States Marine Corps',
return {}
['USMCR'] = 'United States Marine Corps Reserve',
end
['AA'] = 'Antiaircraft',
local rows = {}
['AAA'] = 'Antiaircraft Artillery',
for row in mw.text.gsplit(raw, '|||', true) do
['TD'] = 'Tank Destroyer',
row = trim(row)
['FD'] = 'Finance Department',
if row ~= '' then
['GSC'] = 'General Staff Corps',
local cells = {}
['ARMD F'] = 'Armored Force',
for cell in mw.text.gsplit(row, ';;', true) do
['ARMD'] = 'Armored',
cell = trim(cell)
['ARMOR'] = 'Armor',
if cell ~= '' then
['SSF'] = 'Special Service Force',
table.insert(cells, cell)
['PA'] = 'Philippine Army',
end
['RCE'] = 'Royal Canadian Engineers',
end
['RCA'] = 'Royal Canadian Artillery',
if #cells > 0 then
['CHC'] = 'Chaplain Corps',
table.insert(rows, cells)
['CHP'] = 'Chaplain Corps',
end
['CAO'] = 'Civil Affairs',
end
['CAU'] = 'Civil Affairs',
end
}
return rows
 
local function expandBranchToken(raw)
raw = trim(raw)
if raw == '' then
return ''
end
local key = mw.ustring.upper(raw)
key = mw.ustring.gsub(key, '%.', '')
key = mw.ustring.gsub(key, '%s+', ' ')
key = trim(key)
if BRANCH_MAP[key] then
return BRANCH_MAP[key]
end
local left, right = mw.ustring.match(key, '^(.+) %((.+)%)$')
if left and right then
left = trim(left)
right = trim(right)
local leftExp = BRANCH_MAP[left] or left
local rightExp = BRANCH_MAP[right] or right
if BRANCH_MAP[left] or BRANCH_MAP[right] then
return leftExp .. ' (' .. rightExp .. ')'
end
end
return raw
end
end


local function formatAwardsRibbons(raw, rowRaw)
local function keyEqualsAAF(part)
-- Prefer explicit Wikipedia row layout when present
local key = mw.ustring.upper(trim(part))
local rows = splitAwardRows(rowRaw or '')
key = mw.ustring.gsub(key, '%.', '')
if #rows == 0 then
key = mw.ustring.gsub(key, '%s+', ' ')
raw = trim(raw)
return key == 'AAF' or key == 'ARMY AIR FORCES'
if raw == '' then
return ''
end
local files = {}
for part in mw.text.gsplit(raw, ';', true) do
part = trim(part)
part = mw.ustring.gsub(part, '^[Ff]ile:', '')
if part ~= '' then
table.insert(files, part)
end
end
if #files == 0 then
return ''
end
-- Fallback: pack ~4 across
local perRow = 4
rows = {}
local cur = {}
for _, file in ipairs(files) do
table.insert(cur, file)
if #cur == perRow then
table.insert(rows, cur)
cur = {}
end
end
if #cur > 0 then
table.insert(rows, cur)
end
end
local out = {}
table.insert(out, '{| class="soldier-ribbon-rack"')
for _, cells in ipairs(rows) do
table.insert(out, '|-')
for _, file in ipairs(cells) do
file = mw.ustring.gsub(file, '^[Ff]ile:', '')
table.insert(out, '| [[File:' .. file .. '|106px|link=]]')
end
end
table.insert(out, '|}')
return table.concat(out, '\n')
end
end


local function formatAwardsGrid(raw)
-- True when a BRANCH_MAP / class_branch value is an Army arm/service, not Army/Marines/Navy.
local rows = splitAwardRows(raw)
local SERVICE_BRANCH_KEY = {
if #rows == 0 then
['AUS'] = true,
return ''
['USA'] = true,
['ARMY'] = true,
['UNITED STATES ARMY'] = true,
['US ARMY'] = true,
['USMC'] = true,
['USMCR'] = true,
['MARINES'] = true,
['MARINE CORPS'] = true,
['UNITED STATES MARINE CORPS'] = true,
['USN'] = true,
['NAVY'] = true,
['UNITED STATES NAVY'] = true,
['USCG'] = true,
['COAST GUARD'] = true,
['UNITED STATES COAST GUARD'] = true,
['AAF'] = true,
['ARMY AIR FORCES'] = true,
['UNITED STATES ARMY AIR FORCES'] = true,
['PA'] = true,
['PHILIPPINE ARMY'] = true,
['RCE'] = true,
['RCA'] = true,
}
 
local function isServiceBranchName(name)
name = trim(name)
if name == '' then
return false
end
local key = mw.ustring.upper(name)
key = mw.ustring.gsub(key, '%.', '')
key = mw.ustring.gsub(key, '%s+', ' ')
key = trim(key)
if SERVICE_BRANCH_KEY[key] then
return true
end
local low = mw.ustring.lower(name)
if mw.ustring.find(low, 'marine', 1, true)
or mw.ustring.find(low, 'navy', 1, true)
or mw.ustring.find(low, 'coast guard', 1, true) then
return true
end
if mw.ustring.find(low, 'air force', 1, true)
or mw.ustring.find(low, 'air forces', 1, true) then
return true
end
end
local out = {}
if mw.ustring.find(low, 'united states army', 1, true)
table.insert(out, '{| class="wikitable soldier-awards-grid"')
or low == 'army'
for _, cells in ipairs(rows) do
or low == 'u.s. army'
table.insert(out, '|-')
or low == 'us army' then
for _, cell in ipairs(cells) do
return true
table.insert(out, '| ' .. cell)
end
end
end
table.insert(out, '|}')
return false
return table.concat(out, '\n')
end
end


local function formatAwardsLegend(raw)
local function isArmOfServiceToken(raw)
raw = prettyProse(trim(raw))
raw = trim(raw)
if raw == '' then
if raw == '' then
return ''
return false
end
local key = mw.ustring.upper(raw)
key = mw.ustring.gsub(key, '%.', '')
key = mw.ustring.gsub(key, '%s+', ' ')
key = trim(key)
if SERVICE_BRANCH_KEY[key] then
return false
end
if BRANCH_MAP[key] then
return true
end
local low = mw.ustring.lower(raw)
if mw.ustring.find(low, 'calvalry', 1, true) then
return true
end
end
-- Split semicolon / <br> lists into bullets
for _, needle in ipairs({
local parts = {}
'artillery', 'signal', 'cavalry', 'infantry', 'engineer',
raw = mw.ustring.gsub(raw, '<br%s*/?>', ';')
'ordnance', 'quartermaster', 'medical', 'police', 'intelligence',
for part in mw.text.gsplit(raw, ';', true) do
'armor', 'tank destroyer', 'chemical', 'chaplain', 'transportation',
part = trim(part)
'adjutant',
part = mw.ustring.gsub(part, '^%*%s*', '')
}) do
if part ~= '' then
if mw.ustring.find(low, needle, 1, true) then
table.insert(parts, '* ' .. part)
return true
end
end
end
end
if #parts == 0 then
return false
return ''
end
return table.concat(parts, '\n')
end
end


local function lifespan(birth, death)
local function splitBranchAndArm(branchRaw, classBranchRaw, armRaw)
birth = yearOnly(birth)
branchRaw = trim(branchRaw)
death = yearOnly(death)
classBranchRaw = trim(classBranchRaw)
if birth ~= '' and death ~= '' then
armRaw = trim(armRaw)
return ' (' .. birth .. '&ndash;' .. death .. ')'
 
local service = ''
local arms = {}
 
local function consider(raw, preferArm)
raw = trim(raw)
if raw == '' then
return
end
for part in mw.text.gsplit(raw, ';', true) do
part = trim(part)
if part ~= '' then
local expanded = expandBranchToken(part)
if isServiceBranchName(expanded) or isServiceBranchName(part) then
if service == '' then
service = expandBranchToken(part)
if not isServiceBranchName(service) then
-- expandBranchToken may not rewrite "Army"
local low = mw.ustring.lower(part)
if mw.ustring.find(low, 'marine', 1, true) then
service = 'United States Marine Corps'
elseif mw.ustring.find(low, 'navy', 1, true) then
service = 'United States Navy'
elseif mw.ustring.find(low, 'air force', 1, true)
or mw.ustring.find(low, 'air forces', 1, true)
or keyEqualsAAF(part) then
service = 'United States Army Air Forces'
else
service = 'United States Army'
end
end
end
elseif preferArm or isArmOfServiceToken(part) or isArmOfServiceToken(expanded) then
local armName = expanded
if mw.ustring.find(mw.ustring.lower(part), 'calvalry', 1, true) then
armName = 'Cavalry'
end
local dup = false
for _, existing in ipairs(arms) do
if existing == armName then
dup = true
break
end
end
if not dup then
table.insert(arms, armName)
end
elseif preferArm then
table.insert(arms, expanded)
elseif service == '' and not preferArm then
service = expanded
end
end
end
end
end
if birth ~= '' then
 
return ' (born ' .. birth .. ')'
-- Explicit arm_or_service first
end
consider(armRaw, true)
if death ~= '' then
-- Roster class_branch is almost always arm/service within the Army
return ' (died ' .. death .. ')'
consider(classBranchRaw, true)
end
-- |branch= may be either
return ''
consider(branchRaw, false)
 
local armJoined = table.concat(arms, '; ')
return service, armJoined
end
end


local function preferredRank(frame)
-- Small public-domain / Commons emblems uploaded as File:Branch emblem *.png
local rank = expandRank(parentArg(frame, 'rank_final'))
local BRANCH_EMBLEM = {
if rank == '' then
['United States Army'] = 'Branch emblem US Army.png',
rank = expandRank(parentArg(frame, 'class_rank'))
['United States Marine Corps'] = 'Branch emblem US Marine Corps.png',
end
['United States Navy'] = 'Branch emblem US Navy.png',
return rank
['United States Army Air Forces'] = 'Branch emblem US Army Air Forces.png',
end
['United States Air Force'] = 'Branch emblem US Air Force.png',
['United States Coast Guard'] = 'Branch emblem US Coast Guard.png',
}


function p.displayName(frame)
local function branchWithEmblem(name)
return displayName(frame)
name = trim(name)
end
if name == '' then
 
return ''
local function displayNameFrom(getArg)
local first = prettyNamePart(getArg('first'))
local middle = prettyNamePart(getArg('middle'))
local surname = prettyNamePart(getArg('surname'))
local suffix = prettyNamePart(getArg('suffix'))
local parts = {}
if first ~= '' then
table.insert(parts, first)
end
end
if middle ~= '' then
local file = BRANCH_EMBLEM[name]
table.insert(parts, middle)
if file then
return '[[File:' .. file .. '|18px|link=|alt=]] ' .. name
end
end
if surname ~= '' then
-- Fuzzy contains
table.insert(parts, surname)
local low = mw.ustring.lower(name)
if mw.ustring.find(low, 'marine', 1, true) then
return '[[File:Branch emblem US Marine Corps.png|18px|link=|alt=]] ' .. name
end
end
local name = table.concat(parts, ' ')
if mw.ustring.find(low, 'navy', 1, true) then
if suffix ~= '' then
return '[[File:Branch emblem US Navy.png|18px|link=|alt=]] ' .. name
if name ~= '' then
name = name .. ', ' .. suffix
else
name = suffix
end
end
end
if name == '' then
if mw.ustring.find(low, 'air force', 1, true) or mw.ustring.find(low, 'air forces', 1, true) then
name = 'This soldier'
return '[[File:Branch emblem US Army Air Forces.png|18px|link=|alt=]] ' .. name
end
end
return name
if mw.ustring.find(low, 'coast guard', 1, true) then
end
return '[[File:Branch emblem US Coast Guard.png|18px|link=|alt=]] ' .. name
 
end
local function preferredRankFrom(getArg)
if mw.ustring.find(low, 'army', 1, true) then
local rank = expandRank(getArg('rank_final'))
return '[[File:Branch emblem US Army.png|18px|link=|alt=]] ' .. name
if rank == '' then
rank = expandRank(getArg('class_rank'))
end
end
return rank
return name
end
end


local function buildLead(getArg)
local function expandBranch(raw)
local name = displayNameFrom(getArg)
raw = trim(raw)
local rank = preferredRankFrom(getArg)
if raw == '' then
local role = 'United States Army soldier'
return ''
if rank ~= '' then
end
role = 'United States Army ' .. rank
local parts = {}
end
for part in mw.text.gsplit(raw, ';', true) do
 
part = expandBranchToken(part)
local classLinks = classLinkList(getArg('class_number'), true)
if part ~= '' then
local trained
table.insert(parts, branchWithEmblem(part))
if #classLinks == 1 then
end
trained = ' who trained at [[Camp Ritchie]], Maryland, in ' .. classLinks[1]
elseif #classLinks == 2 then
trained = ' who trained at [[Camp Ritchie]], Maryland, in ' .. classLinks[1] .. ' and ' .. classLinks[2]
elseif #classLinks > 2 then
trained = ' who trained at [[Camp Ritchie]], Maryland, in '
.. table.concat(classLinks, ', ', 1, #classLinks - 1)
.. ', and ' .. classLinks[#classLinks]
else
trained = ' associated with Camp Ritchie, Maryland'
end
end
return table.concat(parts, '; ')
end


local lead = string.format(
local function formatBattles(raw)
"'''%s'''%s was a %s%s.",
raw = trim(raw)
name,
if raw == '' then
lifespan(getArg('birth_year'), getArg('death_year')),
return ''
role,
trained
)
local who = prettyNamePart(getArg('first'))
if who == '' then
who = prettyNamePart(getArg('surname'))
end
end
if who == '' then
-- Allow HTML <br /> or semicolon / newline separated campaigns
who = 'He'
raw = mw.ustring.gsub(raw, '\r', '')
if mw.ustring.find(raw, '<br', 1, true) or mw.ustring.find(raw, '\n', 1, true) then
return raw
end
local parts = {}
for part in mw.text.gsplit(raw, ';', true) do
part = trim(part)
if part ~= '' then
table.insert(parts, part)
end
end
end
local enlist = prettyPlace(getArg('enlistment_place'))
if #parts <= 1 then
if enlist ~= '' then
return raw
lead = lead .. ' ' .. who .. ' enlisted at ' .. enlist .. '.'
end
end
return lead
return table.concat(parts, '<br />')
end
end


function p.classLinks(frame)
local function splitAwardRows(raw)
return table.concat(classLinkList(parentArg(frame, 'class_number'), false), ', ')
raw = trim(raw)
end
if raw == '' then
 
return {}
local PHOTO_EXTENSIONS = { 'jpg', 'jpeg', 'png', 'webp', 'gif' }
end
 
-- Prefer @@ row separator (safe inside {{Soldier}}); also accept legacy |||
local function photoFileExists(filename)
local sep = '@@'
filename = trim(filename)
if not mw.ustring.find(raw, '@@', 1, true) and mw.ustring.find(raw, '|||', 1, true) then
if filename == '' then
sep = '|||'
return false
end
local rows = {}
for row in mw.text.gsplit(raw, sep, true) do
row = trim(row)
if row ~= '' then
local cells = {}
for cell in mw.text.gsplit(row, ';;', true) do
cell = trim(cell)
-- Unescape {{!}} used if a label ever needed a pipe
cell = mw.ustring.gsub(cell, '{{!}}', '|')
if cell ~= '' then
table.insert(cells, cell)
end
end
if #cells > 0 then
table.insert(rows, cells)
end
end
end
end
local title = mw.title.new('File:' .. filename)
return rows
return title ~= nil and title.exists
end
end


local function pushPhotoCandidate(candidates, seen, value)
local function awardRibbonRows(raw, rowRaw)
value = trim(value)
-- Prefer explicit Wikipedia row layout when present
if value == '' or seen[value] then
local rows = splitAwardRows(rowRaw or '')
return
if #rows > 0 then
return rows
end
raw = trim(raw)
if raw == '' then
return {}
end
local files = {}
for part in mw.text.gsplit(raw, ';', true) do
part = trim(part)
part = mw.ustring.gsub(part, '^[Ff]ile:', '')
if part ~= '' then
table.insert(files, part)
end
end
if #files == 0 then
return {}
end
-- Fallback: pack ~4 across
local perRow = 4
rows = {}
local cur = {}
for _, file in ipairs(files) do
table.insert(cur, file)
if #cur == perRow then
table.insert(rows, cur)
cur = {}
end
end
if #cur > 0 then
table.insert(rows, cur)
end
end
seen[value] = true
return rows
table.insert(candidates, value)
end
end


local function resolvePhoto(frame)
local function awardRowsMatch(ribbonRows, labelRows)
local explicit = trim(parentArg(frame, 'photo'))
if #ribbonRows == 0 or #ribbonRows ~= #labelRows then
if explicit ~= '' then
return false
return explicit
end
end
 
for i, cells in ipairs(ribbonRows) do
local first = trim(parentArg(frame, 'first'))
if #cells ~= #(labelRows[i] or {}) then
local surname = trim(parentArg(frame, 'surname'))
return false
local asn = trim(parentArg(frame, 'asn'))
end
if first == '' or surname == '' then
return ''
end
end
return true
end


local candidates = {}
local function formatAwardsRibbons(raw, rowRaw, labelRaw)
local seen = {}
local rows = awardRibbonRows(raw, rowRaw)
local lowerFirst = mw.ustring.lower(first)
if #rows == 0 then
local lowerSurname = mw.ustring.lower(surname)
return '', false
local bases = {
first .. ' ' .. surname,
first .. ' ' .. lowerSurname,
lowerFirst .. ' ' .. lowerSurname,
first .. '_' .. surname,
first .. '_' .. lowerSurname,
lowerFirst .. '_' .. lowerSurname,
surname .. ', ' .. first,
surname .. '_' .. first,
}
if asn ~= '' then
table.insert(bases, surname .. ', ' .. first .. ' (' .. asn .. ')')
end
end
for _, base in ipairs(bases) do
local labelRows = splitAwardRows(labelRaw or '')
pushPhotoCandidate(candidates, seen, base)
local labeled = awardRowsMatch(rows, labelRows)
-- One table per row so short rows (e.g. 2 ribbons) center like full rows.
-- When labels match ribbon cells, put captions in the same table so each
-- name sits centered under its ribbon (equal column widths).
local out = {}
for i, cells in ipairs(rows) do
if labeled then
table.insert(out, '{| class="soldier-ribbon-rack soldier-awards-labeled"')
else
table.insert(out, '{| class="soldier-ribbon-rack"')
end
table.insert(out, '|-')
for _, file in ipairs(cells) do
file = mw.ustring.gsub(file, '^[Ff]ile:', '')
table.insert(out, '| [[File:' .. file .. '|106px|link=]]')
end
if labeled then
table.insert(out, '|-')
for _, label in ipairs(labelRows[i]) do
table.insert(out, '| class="soldier-award-caption" | ' .. label)
end
end
table.insert(out, '|}')
end
end
return table.concat(out, '\n'), labeled
end


for _, base in ipairs(candidates) do
local function awardGridColspanUnits(rows)
for _, ext in ipairs(PHOTO_EXTENSIONS) do
-- LCM of row lengths so 2-cell and 3-cell rows share one table width
local filename = base .. '.' .. ext
local function gcd(a, b)
if photoFileExists(filename) then
while b ~= 0 do
return filename
a, b = b, a % b
end
end
local upperExt = mw.ustring.upper(ext)
return a
if upperExt ~= ext then
end
filename = base .. '.' .. upperExt
local function lcm(a, b)
if photoFileExists(filename) then
if a == 0 or b == 0 then
return filename
return a + b
end
end
end
return (a / gcd(a, b)) * b
end
local units = 0
for _, cells in ipairs(rows) do
local n = #cells
if n > 0 then
units = lcm(units, n)
end
end
end
end
return ''
return units > 0 and units or 1
end
end


function p.infobox(frame)
local function formatAwardsGrid(raw)
local name = displayName(frame)
local rows = splitAwardRows(raw)
local tbl = mw.html.create('table')
if #rows == 0 then
tbl:addClass('infobox'):addClass('vcard'):addClass('soldier-infobox')
return ''
end
local units = awardGridColspanUnits(rows)
local out = { '{| class="wikitable soldier-awards-grid"' }
for _, cells in ipairs(rows) do
local n = #cells
if n == 0 then
-- skip
else
local span = units / n
table.insert(out, '|-')
for _, cell in ipairs(cells) do
-- Allow <br /> from Wikipedia-style multi-line labels
table.insert(out, '| colspan="' .. tostring(span) .. '" | ' .. cell)
end
end
end
table.insert(out, '|}')
return table.concat(out, '\n')
end


tbl:tag('tr'):tag('th')
local function formatAwardsLegend(raw)
:attr('colspan', '2')
-- Do not run prettyProse/prettyPlace — commas in prose (e.g. statue note)
:addClass('infobox-above fn')
-- get misread as place names and Title-Cased / rewrapped.
:wikitext(name)
raw = trim(raw)
 
if raw == '' then
local photo = resolvePhoto(frame)
return ''
if photo ~= '' then
end
local size = parentArg(frame, 'image_size')
local parts = {}
if size == '' then
raw = mw.ustring.gsub(raw, '<br%s*/?>', ';')
size = '220px'
for part in mw.text.gsplit(raw, ';', true) do
end
part = trim(part)
local alt = parentArg(frame, 'alt')
part = mw.ustring.gsub(part, '^%*%s*', '')
if alt == '' then
if part ~= '' then
alt = name
if isAllCapsPhrase(part) then
end
part = prettyTitle(part, true)
local td = tbl:tag('tr'):tag('td')
end
:attr('colspan', '2')
table.insert(parts, '* ' .. part)
:addClass('infobox-image')
td:wikitext(string.format('[[File:%s|%s|alt=%s]]', photo, size, alt))
local caption = parentArg(frame, 'caption')
if caption ~= '' then
td:tag('div'):addClass('infobox-caption'):wikitext(caption)
end
end
end
end
if #parts == 0 then
return ''
end
return table.concat(parts, '\n')
end


local alias = prettyNamePart(parentArg(frame, 'alias'))
local function lifespan(birth, death)
local birthYear = yearOnly(parentArg(frame, 'birth_year'))
birth = trim(birth or '')
local deathYear = yearOnly(parentArg(frame, 'death_year'))
death = trim(death or '')
local birthDate = prettyProse(parentArg(frame, 'birth_date'))
if birth ~= '' and death ~= '' then
local deathDate = prettyProse(parentArg(frame, 'death_date'))
return ' (' .. birth .. ' &ndash; ' .. death .. ')'
local birthPlace = prettyProse(parentArg(frame, 'birth_place'))
end
local deathPlace = prettyProse(parentArg(frame, 'death_place'))
if birth ~= '' then
local nativity = expandNativity(parentArg(frame, 'nativity'))
return ' (born ' .. birth .. ')'
local religion = expandReligion(parentArg(frame, 'religion'))
end
local marital = prettyProse(parentArg(frame, 'marital'))
if death ~= '' then
local education = prettyProse(parentArg(frame, 'education'))
return ' (died ' .. death .. ')'
local occupation = prettyProse(parentArg(frame, 'occupation'))
local knownFor = prettyProse(parentArg(frame, 'known_for'))
local awards = prettyProse(parentArg(frame, 'awards'))
local ribbonsRaw = parentArg(frame, 'awards_ribbons')
-- Full medal list belongs under the body ribbon rack; keep infobox short
if ribbonsRaw ~= '' and mw.ustring.len(awards) > 90 then
local first = mw.ustring.match(awards, '^([^;]+)')
awards = trim(first or awards)
end
end
local state = prettyPlace(parentArg(frame, 'state'))
return ''
end


local function datePlaceCell(date, place, year)
--- Prefer full birth/death dates (Wikipedia style); fall back to years.
local bits = {}
local function lifespanFromArgs(getArg)
if date ~= '' then
local birth = prettyProse(getArg('birth_date'))
table.insert(bits, date)
local death = prettyProse(getArg('death_date'))
elseif year ~= '' then
if birth == '' then
table.insert(bits, year)
birth = yearOnly(getArg('birth_year'))
end
end
if place ~= '' then
if death == '' then
table.insert(bits, place)
death = yearOnly(getArg('death_year'))
end
return table.concat(bits, '<br />')
end
end
return lifespan(birth, death)
end


local birth = datePlaceCell(birthDate, birthPlace, birthYear)
local function preferredRank(frame)
local death = datePlaceCell(deathDate, deathPlace, deathYear)
local rank = expandRank(parentArg(frame, 'rank_final'))
 
if rank == '' then
if alias ~= '' or birth ~= '' or death ~= '' or nativity ~= ''
rank = expandRank(parentArg(frame, 'class_rank'))
or religion ~= '' or marital ~= '' or education ~= ''
or occupation ~= '' or knownFor ~= '' or awards ~= '' or state ~= '' then
addHeader(tbl, 'Personal details')
end
end
return rank
end


addRow(tbl, 'Birth name', alias, 'nickname')
function p.displayName(frame)
addRow(tbl, 'Born', birth)
return displayName(frame)
addRow(tbl, 'Died', death)
end
addRow(tbl, 'Nativity', nativity)
 
addRow(tbl, 'Religion', religion)
local function displayNameFrom(getArg)
addRow(tbl, 'Spouse / marital', marital)
local first = prettyNamePart(getArg('first'))
addRow(tbl, 'Education', education)
local middle = prettyNamePart(getArg('middle'))
addRow(tbl, 'Occupation', occupation)
local surname = prettyNamePart(getArg('surname'))
addRow(tbl, 'Known for', knownFor)
local suffix = prettyNamePart(getArg('suffix'))
addRow(tbl, 'Awards', awards)
local parts = {}
if state ~= '' then
if first ~= '' then
addRow(tbl, 'State', state)
table.insert(parts, first)
end
end
 
if middle ~= '' then
addHeader(tbl, 'Military service')
table.insert(parts, middle)
addRow(tbl, 'Allegiance', '[[File:Flag of the United States.png|20px|link=|alt=]] United States')
local branchRaw = parentArg(frame, 'class_branch')
if branchRaw == '' then
branchRaw = parentArg(frame, 'branch')
end
end
local branch = expandBranch(branchRaw)
if surname ~= '' then
if branch == '' then
table.insert(parts, surname)
branch = branchWithEmblem('United States Army')
end
end
addRow(tbl, 'Branch', branch)
local name = table.concat(parts, ' ')
addRow(tbl, 'Years of service', prettyProse(parentArg(frame, 'years_of_service')))
if suffix ~= '' then
addRow(tbl, 'ASN', parentArg(frame, 'asn'), 'serial')
if name ~= '' then
 
name = name .. ', ' .. suffix
local rankFinal = expandRank(parentArg(frame, 'rank_final'))
else
local classRank = expandRank(parentArg(frame, 'class_rank'))
name = suffix
if rankFinal ~= '' and classRank ~= '' and classRank ~= rankFinal then
addRow(tbl, 'Rank', rankFinal)
addRow(tbl, 'Rank at Ritchie', classRank)
else
local rank = rankFinal
if rank == '' then
rank = classRank
end
end
addRow(tbl, 'Rank', rank)
end
end
if name == '' then
name = 'This soldier'
end
return name
end


local classRaw = parentArg(frame, 'class_number')
local function preferredRankFrom(getArg)
local classBits = classLinkList(classRaw, false)
local rank = expandRank(getArg('rank_final'))
if #classBits > 0 then
if rank == '' then
addRow(tbl, 'Camp Ritchie', 'Class ' .. table.concat(classBits, ', '))
rank = expandRank(getArg('class_rank'))
end
end
addRow(tbl, 'Section', parentArg(frame, 'class_section'))
return rank
addRow(tbl, 'Other class', parentArg(frame, 'other_class'))
end


local enlist = prettyPlace(parentArg(frame, 'enlistment_place'))
local function buildLead(getArg)
if enlist ~= '' then
local name = displayNameFrom(getArg)
addRow(tbl, 'Enlisted', enlist)
local rank = preferredRankFrom(getArg)
local role = 'United States Army soldier'
if rank ~= '' then
role = 'United States Army ' .. rank
end
end
addRow(tbl, 'Specialty', prettyProse(parentArg(frame, 'specialty')))
addRow(tbl, 'Unit / division', prettyProse(parentArg(frame, 'division')))
addRow(tbl, 'Battles / wars', formatBattles(parentArg(frame, 'battles')))
addRow(tbl, 'IPW (German)', teamLinkList(parentArg(frame, 'ipw_ge'), 'IPW ', 'IPW'))
addRow(tbl, 'IPW (Italian)', teamLinkList(parentArg(frame, 'ipw_it'), 'IPW Italian ', 'IPW Italian'))
addRow(tbl, 'Arrived', parentArg(frame, 'date_arrive'))
addRow(tbl, 'Departed', parentArg(frame, 'date_depart'))
addRow(tbl, 'Certificate', realCertificate(parentArg(frame, 'certificate')))


-- Awards ribbons render in the article body (p.awardsSection), not this
local classLinks = classLinkList(getArg('class_number'), true)
-- narrow infobox — matching Wikipedia's main-column ribbon racks.
local trained
if #classLinks == 1 then
trained = ' who trained at [[Camp Ritchie]], Maryland, in ' .. classLinks[1]
elseif #classLinks == 2 then
trained = ' who trained at [[Camp Ritchie]], Maryland, in ' .. classLinks[1] .. ' and ' .. classLinks[2]
elseif #classLinks > 2 then
trained = ' who trained at [[Camp Ritchie]], Maryland, in '
.. table.concat(classLinks, ', ', 1, #classLinks - 1)
.. ', and ' .. classLinks[#classLinks]
else
trained = ' associated with Camp Ritchie, Maryland'
end


return tostring(tbl)
local lead = string.format(
end
"'''%s'''%s was a %s%s.",
 
name,
function p.awardsSection(frame)
lifespanFromArgs(getArg),
local ribbons = formatAwardsRibbons(
role,
parentArg(frame, 'awards_ribbons'),
trained
parentArg(frame, 'awards_ribbon_rows')
)
)
local grid = formatAwardsGrid(parentArg(frame, 'awards_grid'))
local who = prettyNamePart(getArg('first'))
local extras = formatAwardsLegend(parentArg(frame, 'awards_extra'))
if who == '' then
-- Fallback: flat awards list when no grid was extracted
who = prettyNamePart(getArg('surname'))
if grid == '' and extras == '' then
extras = formatAwardsLegend(parentArg(frame, 'awards'))
end
end
if ribbons == '' and grid == '' and extras == '' then
if who == '' then
return ''
who = 'He'
end
end
-- Use a styled div heading (not == wiki heading) so MediaWiki does not place
local enlist = prettyPlace(getArg('enlistment_place'))
-- the Contents box inside this block — JS moves the block after Biography.
if enlist ~= '' then
local parts = {
lead = lead .. ' ' .. who .. ' enlisted at ' .. enlist .. '.'
'<div class="soldier-awards-section">',
'<div class="soldier-awards-heading">Awards and honors</div>',
}
if ribbons ~= '' then
table.insert(parts, ribbons)
end
end
if grid ~= '' then
return lead
table.insert(parts, grid)
end
if extras ~= '' then
table.insert(parts, '<div class="soldier-awards-list">')
table.insert(parts, extras)
table.insert(parts, '</div>')
end
table.insert(parts, '</div>')
return table.concat(parts, '\n')
end
end


function p.lead(frame)
function p.classLinks(frame)
return buildLead(function(key)
return table.concat(classLinkList(parentArg(frame, 'class_number'), false), ', ')
return parentArg(frame, key)
end)
end
end


function p.leadFrom(frame)
local PHOTO_EXTENSIONS = { 'jpg', 'jpeg', 'png', 'webp', 'gif' }
return buildLead(function(key)
return trim(frame.args[key] or '')
end)
end


local function researchNotesSection(content)
local function photoFileExists(filename)
if not content or content == '' then
filename = trim(filename)
return ''
if filename == '' then
return false
end
end
local startPos = mw.ustring.find(content, '== Research notes ==', 1, true)
local title = mw.title.new('File:' .. filename)
if not startPos then
return title ~= nil and title.exists
return ''
end
local after = mw.ustring.sub(content, startPos + 20)
local nextHead = mw.ustring.find(after, '\n== ', 1, true)
if nextHead then
return mw.ustring.sub(after, 1, nextHead - 1)
end
return after
end
end


local function miiPreferredLetter(num)
local function pushPhotoCandidate(candidates, seen, value)
local ok, canon = pcall(require, 'Module:TeamCanon')
value = trim(value)
if ok and type(canon) == 'table' and type(canon.letter) == 'table' then
if value == '' or seen[value] then
return canon.letter[tostring(num)]
return
end
end
return nil
seen[value] = true
table.insert(candidates, value)
end
end


local function noteTeamNames(text)
local function resolvePhoto(frame)
text = mw.ustring.upper(tostring(text or ''))
local explicit = trim(parentArg(frame, 'photo'))
text = mw.ustring.gsub(text, mw.ustring.char(0x2013), '-')
if explicit ~= '' then
text = mw.ustring.gsub(text, mw.ustring.char(0x2014), '-')
return explicit
text = mw.ustring.gsub(text, mw.ustring.char(0x2212), '-')
local seen = {}
local names = {}
local letteredNums = {}
local function add(name)
if name ~= '' and not seen[name] then
seen[name] = true
table.insert(names, name)
end
end
end
local work = text
 
for num, letter in string.gmatch(work, 'MII[%s%-]+(%d+)%s*%-%s*([A-Z])') do
local first = trim(parentArg(frame, 'first'))
if letter == 'R' and (num == '540' or num == '545' or num == '546') then
local surname = trim(parentArg(frame, 'surname'))
add('MII ' .. num)
local asn = trim(parentArg(frame, 'asn'))
else
if first == '' or surname == '' then
add('MII ' .. num .. '-' .. letter)
return ''
letteredNums[num] = true
end
end
end
work = string.gsub(work, 'MII[%s%-]+%d+%s*%-%s*[A-Z]', ' ')
 
for num, letter in string.gmatch(' ' .. text .. ' ', '[^%d]([45]%d%d)%s*%-%s*([A-Z])[^A-Z]') do
local candidates = {}
if letter == 'R' and (num == '540' or num == '545' or num == '546') then
local seen = {}
add('MII ' .. num)
local lowerFirst = mw.ustring.lower(first)
else
local lowerSurname = mw.ustring.lower(surname)
add('MII ' .. num .. '-' .. letter)
local bases = {
letteredNums[num] = true
first .. ' ' .. surname,
end
first .. ' ' .. lowerSurname,
lowerFirst .. ' ' .. lowerSurname,
first .. '_' .. surname,
first .. '_' .. lowerSurname,
lowerFirst .. '_' .. lowerSurname,
surname .. ', ' .. first,
surname .. '_' .. first,
}
if asn ~= '' then
table.insert(bases, surname .. ', ' .. first .. ' (' .. asn .. ')')
end
end
for num in string.gmatch(work, 'MII[%s%-]+(%d+)') do
for _, base in ipairs(bases) do
if not letteredNums[num] then
pushPhotoCandidate(candidates, seen, base)
local preferred = miiPreferredLetter(num)
end
if preferred then
 
add('MII ' .. num .. '-' .. preferred)
for _, base in ipairs(candidates) do
else
for _, ext in ipairs(PHOTO_EXTENSIONS) do
add('MII ' .. num)
local filename = base .. '.' .. ext
if photoFileExists(filename) then
return filename
end
local upperExt = mw.ustring.upper(ext)
if upperExt ~= ext then
filename = base .. '.' .. upperExt
if photoFileExists(filename) then
return filename
end
end
end
end
end
end
end
for num, letter in string.gmatch(text, 'IPW%s+(%d+)%s*%-%s*([A-Z])') do
return ''
if letter == 'R' and (num == '540' or num == '545' or num == '546') then
end
add('MII ' .. num)
 
else
-- Filled after noteTeamNames (local forward declare; used by p.infobox).
add('MII ' .. num .. '-' .. letter)
local addTeamsInfoboxRow
 
function p.infobox(frame)
local name = displayName(frame)
local tbl = mw.html.create('table')
tbl:addClass('infobox'):addClass('vcard'):addClass('soldier-infobox')
 
tbl:tag('tr'):tag('th')
:attr('colspan', '2')
:addClass('infobox-above fn')
:wikitext(name)
 
local photo = resolvePhoto(frame)
if photo ~= '' then
local size = parentArg(frame, 'image_size')
if size == '' then
size = '220px'
end
local alt = parentArg(frame, 'alt')
if alt == '' then
alt = name
end
local td = tbl:tag('tr'):tag('td')
:attr('colspan', '2')
:addClass('infobox-image')
td:wikitext(string.format('[[File:%s|%s|alt=%s]]', photo, size, alt))
local caption = parentArg(frame, 'caption')
if caption ~= '' then
td:tag('div'):addClass('infobox-caption'):wikitext(caption)
end
end
end
end
for num in string.gmatch(text, 'IPW%s+(%d+)[Tt]?%s*%??%f[^%a%d%-]') do
 
add('IPW ' .. num)
local alias = prettyNamePart(parentArg(frame, 'alias'))
local birthYear = yearOnly(parentArg(frame, 'birth_year'))
local deathYear = yearOnly(parentArg(frame, 'death_year'))
local birthDate = prettyProse(parentArg(frame, 'birth_date'))
local deathDate = prettyProse(parentArg(frame, 'death_date'))
local birthPlace = prettyProse(parentArg(frame, 'birth_place'))
local deathPlace = prettyProse(parentArg(frame, 'death_place'))
local nativity = expandNativity(parentArg(frame, 'nativity'))
local religion = expandReligion(parentArg(frame, 'religion'))
-- education / marital / occupation stay in Technical record (NARA snapshot);
-- curated life facts belong in Biography / Timeline, not the infobox.
local knownFor = prettyProse(parentArg(frame, 'known_for'))
local awards = prettyProse(parentArg(frame, 'awards'))
local ribbonsRaw = parentArg(frame, 'awards_ribbons')
-- Full medal list belongs under the body ribbon rack; keep infobox short
if ribbonsRaw ~= '' and mw.ustring.len(awards) > 90 then
local first = mw.ustring.match(awards, '^([^;]+)')
awards = trim(first or awards)
end
end
return names
end


function p.noteTeamCategories(frame)
local function datePlaceCell(date, place, year)
local title = mw.title.getCurrentTitle()
local bits = {}
if not title or not title.getContent then
if date ~= '' then
return ''
table.insert(bits, date)
elseif year ~= '' then
table.insert(bits, year)
end
if place ~= '' then
table.insert(bits, place)
end
return table.concat(bits, '<br />')
end
end
local ok, content = pcall(function()
 
return title:getContent()
local birth = datePlaceCell(birthDate, birthPlace, birthYear)
end)
local death = datePlaceCell(deathDate, deathPlace, deathYear)
if not ok or not content then
 
return ''
if alias ~= '' or birth ~= '' or death ~= '' or nativity ~= ''
end
or religion ~= '' or knownFor ~= '' or awards ~= '' then
local bits = {}
addHeader(tbl, 'Personal details')
for _, name in ipairs(noteTeamNames(researchNotesSection(content))) do
table.insert(bits, '[[Category:Team ' .. name .. ']]')
end
end
return table.concat(bits)
end


function p.researchNotes(frame)
addRow(tbl, 'Birth name', alias, 'nickname')
local title = mw.title.getCurrentTitle()
addRow(tbl, 'Born', birth)
if not title or not title.getContent then
addRow(tbl, 'Died', death)
return ''
addRow(tbl, 'Nativity', nativity)
end
addRow(tbl, 'Religion', religion)
local ok, content = pcall(function()
addRow(tbl, 'Known for', knownFor)
return title:getContent()
addRow(tbl, 'Awards', awards)
end)
 
if not ok or not content or content == '' then
addHeader(tbl, 'Military service')
return ''
addRow(tbl, 'Allegiance', '[[File:Flag of the United States.png|20px|link=|alt=]] United States')
local serviceName, armName = splitBranchAndArm(
parentArg(frame, 'branch'),
parentArg(frame, 'class_branch'),
parentArg(frame, 'arm_or_service')
)
if serviceName == '' then
serviceName = 'United States Army'
end
end
local section = researchNotesSection(content)
addRow(tbl, 'Branch', branchWithEmblem(serviceName))
if section == '' then
if armName ~= '' then
return ''
-- Roster "branch" codes (FA, Signal Corps, Cavalry, …) are arm/service, not Army/Marines.
addRow(tbl, 'Arm / service', prettyProse(armName))
end
end
local items = {}
-- years_of_service / date_arrive / date_depart / certificate stay in Technical
for line in mw.text.gsplit(section, '\n', true) do
-- record only (rarely curated; certificate is a cryptic chart number).
line = trim(line)
addRow(tbl, 'ASN', parentArg(frame, 'asn'), 'serial')
if mw.ustring.sub(line, 1, 2) == '* ' then
 
local rankFinal = expandRank(parentArg(frame, 'rank_final'))
local classRank = expandRank(parentArg(frame, 'class_rank'))
if rankFinal ~= '' and classRank ~= '' and classRank ~= rankFinal then
addRow(tbl, 'Rank', rankFinal)
addRow(tbl, 'Rank at Ritchie', classRank)
else
local rank = rankFinal
if rank == '' then
rank = classRank
end
addRow(tbl, 'Rank', rank)
end
 
local classRaw = parentArg(frame, 'class_number')
local classBits = classLinkList(classRaw, false)
if #classBits > 0 then
addRow(tbl, 'Camp Ritchie', 'Class ' .. table.concat(classBits, ', '))
end
addRow(tbl, 'Section', mw.ustring.upper(parentArg(frame, 'class_section')))
addRow(tbl, 'Other class', expandOtherClass(parentArg(frame, 'other_class')))
 
local enlist = prettyPlace(parentArg(frame, 'enlistment_place'))
if enlist ~= '' then
addRow(tbl, 'Enlisted', enlist)
end
addRow(tbl, 'Specialty', prettyProse(parentArg(frame, 'specialty')))
addRow(tbl, 'Unit / division', prettyProse(parentArg(frame, 'division')))
addRow(tbl, 'Battles / wars', formatBattles(parentArg(frame, 'battles')))
addTeamsInfoboxRow(tbl, frame)
 
-- Awards ribbons render in the article body (p.awardsSection), not this
-- narrow infobox — matching Wikipedia's main-column ribbon racks.
 
return tostring(tbl)
end
 
function p.awardsSection(frame)
local gridRaw = parentArg(frame, 'awards_grid')
local ribbons, labelsInRack = formatAwardsRibbons(
parentArg(frame, 'awards_ribbons'),
parentArg(frame, 'awards_ribbon_rows'),
gridRaw
)
-- Skip the separate label table when captions already sit under ribbons
local grid = ''
if not labelsInRack then
grid = formatAwardsGrid(gridRaw)
end
local extras = formatAwardsLegend(parentArg(frame, 'awards_extra'))
-- Fallback: flat awards list when no grid was extracted
if grid == '' and extras == '' and not labelsInRack then
extras = formatAwardsLegend(parentArg(frame, 'awards'))
end
if ribbons == '' and grid == '' and extras == '' then
return ''
end
-- Use a styled div heading (not == wiki heading) so MediaWiki does not place
-- the Contents box inside this block — JS moves the block after Biography.
local parts = {
'<div class="soldier-awards-section">',
'<div class="soldier-awards-heading">Awards and honors</div>',
}
if ribbons ~= '' then
table.insert(parts, ribbons)
end
if grid ~= '' then
table.insert(parts, grid)
end
if extras ~= '' then
table.insert(parts, '<div class="soldier-awards-list">')
table.insert(parts, extras)
table.insert(parts, '</div>')
end
table.insert(parts, '</div>')
return table.concat(parts, '\n')
end
 
function p.lead(frame)
return buildLead(function(key)
return parentArg(frame, key)
end)
end
 
function p.leadFrom(frame)
return buildLead(function(key)
return trim(frame.args[key] or '')
end)
end
 
local function researchNotesSection(content)
if not content or content == '' then
return ''
end
local startPos = mw.ustring.find(content, '== Research notes ==', 1, true)
if not startPos then
return ''
end
local after = mw.ustring.sub(content, startPos + 20)
local nextHead = mw.ustring.find(after, '\n== ', 1, true)
if nextHead then
return mw.ustring.sub(after, 1, nextHead - 1)
end
return after
end
 
local function miiPreferredLetter(num)
local ok, canon = pcall(require, 'Module:TeamCanon')
if ok and type(canon) == 'table' and type(canon.letter) == 'table' then
return canon.letter[tostring(num)]
end
return nil
end
 
local function noteTeamNames(text)
text = mw.ustring.upper(tostring(text or ''))
text = mw.ustring.gsub(text, mw.ustring.char(0x2013), '-')
text = mw.ustring.gsub(text, mw.ustring.char(0x2014), '-')
text = mw.ustring.gsub(text, mw.ustring.char(0x2212), '-')
local seen = {}
local names = {}
local letteredNums = {}
local function add(name)
if name ~= '' and not seen[name] then
seen[name] = true
table.insert(names, name)
end
end
local work = text
for num, letter in string.gmatch(work, 'MII[%s%-]+(%d+)%s*%-%s*([A-Z])') do
if letter == 'R' and (num == '540' or num == '545' or num == '546') then
add('MII ' .. num)
else
add('MII ' .. num .. '-' .. letter)
letteredNums[num] = true
end
end
work = string.gsub(work, 'MII[%s%-]+%d+%s*%-%s*[A-Z]', ' ')
for num, letter in string.gmatch(' ' .. text .. ' ', '[^%d]([45]%d%d)%s*%-%s*([A-Z])[^A-Z]') do
if letter == 'R' and (num == '540' or num == '545' or num == '546') then
add('MII ' .. num)
else
add('MII ' .. num .. '-' .. letter)
letteredNums[num] = true
end
end
for num in string.gmatch(work, 'MII[%s%-]+(%d+)') do
if not letteredNums[num] then
local preferred = miiPreferredLetter(num)
if preferred then
add('MII ' .. num .. '-' .. preferred)
else
add('MII ' .. num)
end
end
end
for num, letter in string.gmatch(text, 'IPW%s+(%d+)%s*%-%s*([A-Z])') do
if letter == 'R' and (num == '540' or num == '545' or num == '546') then
add('MII ' .. num)
else
add('MII ' .. num .. '-' .. letter)
end
end
for num in string.gmatch(text, 'IPW%s+(%d+)[Tt]?%s*%??%f[^%a%d%-]') do
add('IPW ' .. num)
end
return names
end
 
local function appendUniqueTeamName(names, seen, name)
name = trim(tostring(name or ''))
if name == '' or seen[name] then
return
end
seen[name] = true
table.insert(names, name)
end
 
local function namesFromIpwGerman(raw)
local names = {}
local seen = {}
for part in mw.text.gsplit(raw or '', ';', true) do
part = trim(part)
part = mw.ustring.gsub(part, '[#<>%[%]|{}]', '')
part = trim(part)
if part ~= '' then
local code, shown, mii = normalizeIpwTeamCode(part)
if mii then
appendUniqueTeamName(names, seen, 'MII ' .. mii)
elseif code then
appendUniqueTeamName(names, seen, 'IPW ' .. shown)
end
end
end
return names
end
 
local function namesFromIpwItalian(raw)
local names = {}
local seen = {}
for part in mw.text.gsplit(raw or '', ';', true) do
part = trim(part)
part = mw.ustring.gsub(part, '[#<>%[%]|{}]', '')
part = trim(part)
if part ~= '' then
local code = normalizeIpwItalianCode(part)
if code then
appendUniqueTeamName(names, seen, 'IPW Italian ' .. code)
end
end
end
return names
end
 
--- Explicit |teams= values: "MII 439-G; IPW 12" or shorthand "439-G".
local function namesFromTeamsParam(raw)
local names = {}
local seen = {}
for part in mw.text.gsplit(raw or '', ';', true) do
part = trim(part)
if part ~= '' then
local fromScan = noteTeamNames(part)
if #fromScan > 0 then
for _, name in ipairs(fromScan) do
appendUniqueTeamName(names, seen, name)
end
else
local upper = mw.ustring.upper(part)
local itRest = mw.ustring.match(upper, '^IPW%s+ITALIAN%s+(.+)$')
if itRest then
local code = normalizeIpwItalianCode(itRest)
if code then
appendUniqueTeamName(names, seen, 'IPW Italian ' .. code)
end
else
local ipwRest = mw.ustring.match(upper, '^IPW%s+(.+)$')
if ipwRest then
local code, shown, mii = normalizeIpwTeamCode(ipwRest)
if mii then
appendUniqueTeamName(names, seen, 'MII ' .. mii)
elseif code then
appendUniqueTeamName(names, seen, 'IPW ' .. shown)
end
else
local code, shown, mii = normalizeIpwTeamCode(part)
if mii then
appendUniqueTeamName(names, seen, 'MII ' .. mii)
elseif code and mw.ustring.match(tostring(shown or code), '^%d') then
appendUniqueTeamName(names, seen, 'IPW ' .. shown)
end
end
end
end
end
end
return names
end
 
local function teamNameLink(name)
return string.format('[[Team %s|%s]]', name, name)
end
 
local function pageResearchNotesText()
local title = mw.title.getCurrentTitle()
if not title or not title.getContent then
return ''
end
local ok, content = pcall(function()
return title:getContent()
end)
if not ok or not content then
return ''
end
return researchNotesSection(content)
end
 
--- Roster IPW fields + optional |teams= + research-note team codes, one linked row.
addTeamsInfoboxRow = function(tbl, frame)
local names = {}
local seen = {}
local function addAll(list)
for _, name in ipairs(list) do
appendUniqueTeamName(names, seen, name)
end
end
addAll(namesFromIpwGerman(parentArg(frame, 'ipw_ge')))
addAll(namesFromIpwItalian(parentArg(frame, 'ipw_it')))
addAll(namesFromTeamsParam(parentArg(frame, 'teams')))
addAll(noteTeamNames(pageResearchNotesText()))
if #names == 0 then
return
end
local links = {}
for _, name in ipairs(names) do
table.insert(links, teamNameLink(name))
end
local label = 'Team'
if #names > 1 then
label = 'Teams'
end
addRow(tbl, label, table.concat(links, ', '))
end
 
function p.noteTeamCategories(frame)
local title = mw.title.getCurrentTitle()
if not title or not title.getContent then
return ''
end
local ok, content = pcall(function()
return title:getContent()
end)
if not ok or not content then
return ''
end
local seen = {}
local bits = {}
local function addCat(name)
name = trim(tostring(name or ''))
if name == '' or seen[name] then
return
end
seen[name] = true
table.insert(bits, '[[Category:Team ' .. name .. ']]')
end
for _, name in ipairs(noteTeamNames(researchNotesSection(content))) do
addCat(name)
end
for _, name in ipairs(namesFromTeamsParam(parentArg(frame, 'teams'))) do
addCat(name)
end
return table.concat(bits)
end
 
function p.researchNotes(frame)
local title = mw.title.getCurrentTitle()
if not title or not title.getContent then
return ''
end
local ok, content = pcall(function()
return title:getContent()
end)
if not ok or not content or content == '' then
return ''
end
local section = researchNotesSection(content)
if section == '' then
return ''
end
local items = {}
for line in mw.text.gsplit(section, '\n', true) do
line = trim(line)
if mw.ustring.sub(line, 1, 2) == '* ' then
local note = sanitizeNote(mw.ustring.sub(line, 3))
local note = sanitizeNote(mw.ustring.sub(line, 3))
if note ~= '' then
if note ~= '' then
for clause in mw.text.gsplit(note, ';', true) do
for clause in mw.text.gsplit(note, ';', true) do
clause = trim(clause)
clause = trim(clause)
if clause ~= '' and not isNegativeClause(clause) then
if clause ~= '' and not isNegativeClause(clause) then
table.insert(items, prettyProse(clause))
table.insert(items, prettyProse(clause))
end
end
end
end
end
end
end
end
items = joinDateFollowons(items)
local unique = {}
local seen = {}
for _, clause in ipairs(items) do
local key = normalizeNoteKey(clause)
if key ~= '' and not seen[key] then
seen[key] = true
table.insert(unique, clause)
end
end
items = unique
if #items == 0 then
return ''
end
local wrap = mw.html.create('div')
wrap:addClass('soldier-research-notes')
wrap:tag('div'):addClass('soldier-research-notes-heading'):wikitext('Research notes')
local ul = wrap:tag('ul')
for _, note in ipairs(items) do
ul:tag('li'):wikitext(note)
end
return tostring(wrap)
end
 
local function cargoRows(tables, fields, where, limit, orderBy)
if not mw.ext or not mw.ext.cargo or not mw.ext.cargo.query then
return nil
end
local opts = {
where = where,
limit = limit or 8,
}
if orderBy and orderBy ~= '' then
opts.orderBy = orderBy
end
local ok, result = pcall(mw.ext.cargo.query, tables, fields, opts)
if not ok or type(result) ~= 'table' then
return nil
end
return result
end
 
local function rowField(row, ...)
for i = 1, select('#', ...) do
local key = select(i, ...)
local v = row[key]
if v ~= nil and trim(tostring(v)) ~= '' then
return trim(tostring(v))
end
end
return ''
end
 
local function titleToCaption(title)
title = trim(title)
local surname, given = mw.ustring.match(title, '^([^,]+),%s*(.-)%s*%(%d')
if surname and given then
return trim(given .. ' ' .. surname)
end
return title
end
 
local function displayFromCargoRow(row)
local page = rowField(row, 'pageName', '_pageName')
local surname = rowField(row, 'Surname')
local first = rowField(row, 'First_name', 'First name')
local middle = rowField(row, 'Middle_name', 'Middle name')
local given = trim(first .. (middle ~= '' and (' ' .. middle) or ''))
local caption
if given ~= '' and surname ~= '' then
caption = given .. ' ' .. surname
elseif page ~= '' then
caption = titleToCaption(page)
else
caption = surname
end
if page == '' and surname ~= '' then
local asn = rowField(row, 'ASN')
if asn ~= '' then
page = surname .. ', ' .. (given ~= '' and given or first) .. ' (' .. asn .. ')'
end
end
return page, caption
end
 
local function linkList(rows, skipAsn, skipPage, maxLinks)
local links = {}
local seen = {}
for _, row in ipairs(rows or {}) do
local asn = rowField(row, 'ASN')
local page, caption = displayFromCargoRow(row)
if caption == '' and page ~= '' then
caption = titleToCaption(page)
end
if page ~= '' and page ~= skipPage and asn ~= skipAsn and not seen[page] then
seen[page] = true
table.insert(links, string.format('[[%s|%s]]', page, caption))
if #links >= maxLinks then
break
end
end
end
return links
end
 
local function classFullWhere(classNum)
-- Prefer Class_number__full: the Class_number HOLDS index is corrupted on some hosts.
local n = tostring(classNum or '')
n = mw.ustring.gsub(n, '[^0-9A-Za-z%-]', '')
if n == '' then
return nil
end
return string.format(
'Class_number__full="%s" OR Class_number__full LIKE "%s;%%" OR Class_number__full LIKE "%%;%s" OR Class_number__full LIKE "%%;%s;%%"',
n,
n,
n,
n
)
end
 
function p.storyCue(frame)
local story = parentArg(frame, 'story')
if story == '' then
return ''
end
story = mw.ustring.gsub(story, '^%[%[', '')
story = mw.ustring.gsub(story, '%]%]$', '')
local storyTitle, storyLabel = mw.ustring.match(story, '^([^|]+)|(.+)$')
if not storyTitle then
storyTitle = story
storyLabel = nil
end
storyTitle = trim(storyTitle)
if storyLabel then
storyLabel = trim(storyLabel)
end
if not storyLabel or storyLabel == '' then
storyLabel = mw.ustring.match(storyTitle, '([^/]+)$') or storyTitle
end
local name = displayName(frame)
return string.format(
'<div class="soldier-story-cue">'
.. '<span class="soldier-story-cue-label">Full story</span> '
.. 'Read more about %s in [[%s|%s]] on \'\'[[The Stories]]\'\'.'
.. '</div>',
name,
storyTitle,
storyLabel
)
end
 
-- related-rail: live team rosters via category members (filled by Common.js)
function p.relatedSoldiers(frame)
local pageTitle = mw.title.getCurrentTitle().prefixedText
 
-- Same sources as the infobox Teams row: roster IPW, |teams=, research notes.
local teamNames = {}
local seenTeams = {}
local function addTeams(list)
for _, name in ipairs(list) do
appendUniqueTeamName(teamNames, seenTeams, name)
end
end
addTeams(namesFromIpwGerman(parentArg(frame, 'ipw_ge')))
addTeams(namesFromIpwItalian(parentArg(frame, 'ipw_it')))
addTeams(namesFromTeamsParam(parentArg(frame, 'teams')))
addTeams(noteTeamNames(pageResearchNotesText()))
 
if #teamNames == 0 then
return ''
end
 
local wrap = mw.html.create('div')
:addClass('soldier-related')
:attr('data-current-title', pageTitle)
wrap:tag('div'):addClass('soldier-related-heading'):wikitext('Teammates')
 
for _, teamName in ipairs(teamNames) do
local cat = 'Category:Team ' .. teamName
local block = wrap:tag('div')
:addClass('soldier-related-block')
:attr('data-team-cat', cat)
block:tag('div')
:addClass('soldier-related-label')
:wikitext('Team — ' .. teamNameLink(teamName))
-- Noscript / pre-JS fallback: category link. Common.js replaces with live names.
block:tag('div')
:addClass('soldier-related-links')
:addClass('soldier-related-links-live')
:wikitext('[[' .. cat .. '|Browse team roster]]')
end
 
wrap:tag('div')
:addClass('soldier-related-note')
:wikitext(
'These are other soldiers listed on the same team roster. '
.. 'People moved between teams; overlapping service dates are not always documented.'
)
return tostring(wrap)
end
 
local function seqCount(t)
-- mw.loadData tables do not always report # correctly.
local n = 0
if type(t) ~= 'table' then
return 0
end
for _ in ipairs(t) do
n = n + 1
end
return n
end
 
function p.mediaCue(frame)
local pageTitle = mw.title.getCurrentTitle().prefixedText
local ok, data = pcall(mw.loadData, 'Module:Interviews/data')
if not ok or type(data) ~= 'table' or type(data.by_title) ~= 'table' then
return ''
end
local rec = data.by_title[pageTitle]
if type(rec) ~= 'table' then
return ''
end
-- Prefer interviews; accept legacy `items` key from hand-edited cues.
local interviews = rec.interviews
if type(interviews) ~= 'table' then
interviews = rec.items
end
local films = rec.films
local nInterviews = seqCount(interviews)
local nFilms = seqCount(films)
if nInterviews == 0 and nFilms == 0 then
return ''
end
local name = displayName(frame)
local blocks = {}
if nInterviews > 0 then
local links = {}
local anyAudio = false
for _, item in ipairs(interviews) do
local heading = item.heading or 'Interview'
local short = item.short or heading
if item.kind == 'audio' then
anyAudio = true
end
table.insert(
links,
string.format('[[Ritchie Boy Interviews#%s|%s]]', heading, short)
)
end
local label = nInterviews == 1 and 'Interview' or 'Interviews'
local verb = 'Watch'
if nInterviews == 1 and interviews[1].kind == 'audio' then
verb = 'Listen to'
elseif anyAudio and nInterviews > 1 then
verb = 'Hear'
end
local body
if nInterviews == 1 then
body = string.format(
'%s %s on [[Ritchie Boy Interviews#%s|%s]].',
verb,
name,
interviews[1].heading,
interviews[1].short or interviews[1].heading
)
else
body = string.format(
'%s %s in %d recordings on [[Ritchie Boy Interviews]]: %s.',
verb,
name,
nInterviews,
table.concat(links, ' · ')
)
end
end
table.insert(
blocks,
'<div class="soldier-interview-cue">'
.. '<span class="soldier-interview-cue-label">'
.. label
.. '</span> '
.. body
.. '</div>'
)
end
end
items = joinDateFollowons(items)
if nFilms > 0 then
local unique = {}
local links = {}
local seen = {}
for _, item in ipairs(films) do
for _, clause in ipairs(items) do
local heading = item.heading or 'Documentaries/Movies'
local key = normalizeNoteKey(clause)
local filmLabel = item.label or heading
if key ~= '' and not seen[key] then
table.insert(
seen[key] = true
links,
table.insert(unique, clause)
string.format('[[Documentaries/Movies#%s|%s]]', heading, filmLabel)
)
end
end
local body = string.format(
'%s appears in %s.',
name,
table.concat(links, '; ')
)
table.insert(
blocks,
'<div class="soldier-film-cue">'
.. '<span class="soldier-film-cue-label">On film</span> '
.. body
.. '</div>'
)
end
end
items = unique
return '<div class="soldier-media-cue">' .. table.concat(blocks, '') .. '</div>'
if #items == 0 then
return ''
end
local wrap = mw.html.create('div')
wrap:addClass('soldier-research-notes')
wrap:tag('div'):addClass('soldier-research-notes-heading'):wikitext('Research notes')
local ul = wrap:tag('ul')
for _, note in ipairs(items) do
ul:tag('li'):wikitext(note)
end
return tostring(wrap)
end
end


return p
return p