Jump to content

Module:BattleHonour

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by BoonDock (talk | contribs) at 01:26, 17 April 2023 (Removed gumpf // Edit via Wikitext Extension for VSCode). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
require('strict')
local p = {}
local data = mw.loadData('Module:BattleHonour/data')
local getArgs = require('Module:Arguments').getArgs

function p.BattleHonourTable(frame)
	return "" -- Null function at the moment
end

function p.GetBattleHonour(BHCode, imgsize)
	
	local output = ''
	if not data[BHCode] then
		output = "Unknown: <b>" .. BHCode .. "</b>"
		output = output .. "<p> Please see the [[Template:BattleHonour]] for help</p>".. "\n|-\n"
	else
		if string.len(data[BHCode].Link) < 1 then
			output =  "[[File:" ..  data[BHCode].Image .. "|border|" .. imgsize .. "|"  .. data[BHCode].Name .."]]" .. "\n|-\n"
		else
			output = output .. "[[File:" ..  data[BHCode].Image .. "|border|" .. imgsize .. "|link=" .. data[BHCode].Link .. "|" .. data[BHCode].Name .."]]" .. "\n|-\n"
		end
	end
	return output
end

local function makeRow(battleHonours, country, imgsize)
    local row = {style = "text-align:center;"}
    for i, bh in ipairs(battleHonours) do
        if bh ~= "" then
			-- local rowoutput= "[[File:" .. data[bh].Image .. "|border|x20px|link=" .. data[bh].Link .. "|" ..data[bh].Name .. "]]\n|-\n"
			local rowoutput = p.GetBattleHonour(bh, imgsize)
			table.insert(row,string.format('|style="text-align: center"| %s',rowoutput) )
        end
    end
    return table.concat(row, "\n")
end

function p.BHTable(frame)
    local templateArgs = getArgs(frame)
    local classes = {"wikitable"}
    if templateArgs.float then
        table.insert(classes, templateArgs.float)
    end
    local caption = templateArgs.caption or "Battle Honours"
    local title = templateArgs.Title or "Awarded"
    local country = templateArgs.country or "ZAR"
	local imgsize = templateArgs.size or "x20px"
    local battleHonours = {}
    for i = 1, 40 do -- 40 is the max number of params that will be examined. 
        local bh = templateArgs[i] or ""
        if bh ~= "" then
            table.insert(battleHonours, bh)
        end
    end
    if templateArgs.list then
        battleHonours = mw.text.split(templateArgs.list, "%s*,%s*")
    end
    if templateArgs.unlinked then
        return table.concat(battleHonours, ", ")
    end
    local rows = {}
    table.insert(rows, string.format("|+ %s \n", caption))
    table.insert(rows, string.format("|-\n!style=\"width:20%%; background:lightgrey; text-align:center;\"|%s\n|-", title))
    table.insert(rows, makeRow(battleHonours, country, imgsize))
    local outputTable = string.format('{| class="%s" style="margin:0.5em auto; font-size:95%%; border-bottom:5px solid %s; border-top:5px solid %s; width:50%%;"\n%s\n|}', table.concat(classes, " "), templateArgs.borderColor or "#CF9C65", templateArgs.borderColor or "#CF9C65", table.concat(rows, "\n"))

	return outputTable
end

return p
-- Initial Code by John Dovey (13 April 2023) [[User:BoonDock]]