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 14:02, 16 April 2023 (Debug // 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.BattleHonour(frame)
	local output = ''
	local templateArgs = getArgs(frame)
	local BHCode = templateArgs[1] or '' -- here we take the first unnamed parameter and assign it to BHCode
	if templateArgs["code"] then -- if there is a named parameter called "code" then assign THAT to BHCode and over-write what's there
		BHCode=templateArgs["code"]
	end
	if not BHCode then --if there is no value in BHCode it means that neither the named param norr the firs unanamed ones were provided
		output = '<span style="color:#d33">Error: No BattleHonour code specified</span>'
		return output
	end
	if data[BHCode] then -- Now we check the data table to see whether the code provided is in the table at all
		output = p.GetBattleHonour(BHCode)
		return output
	else
		-- Code not found in the table, raise an error
		output = '<span style="color:#d33">Error: The BattleHonour code specified (' .. BHCode .. ') was not in the data table.</span>'
		return output 
	end
	-- Should never get heere because it either returns an error message or the Battle Honour itself, but 
	-- for safety's sake, here we tell it to return output...
	return output
end

function p.BattleHonourTable(frame)
	local output = ''
	local templateArgs = getArgs(frame)
	local BHCode = templateArgs["code"] or nil
	if not BHCode then
		output = '<span style="color:#d33">Error: No "code" specified</span>[[Category:Battle Honour Error]]'
		return output
	end

	local tableCode = '{| class="wikitable" \n'
			tableCode = tableCode .. "|+ Battle Honours \n" -- Caption
			tableCode = tableCode .. "|-\n"
	-- Iterate through the data in the table "BattleHonour -> Data"
	for GHcode, record in pairs(data) do
        -- Check if the Code is valid ie in the database
        if templateArgs[BHCode] then
            -- If it is, send the BH
			tableCode = tableCode .. "|-\n"
			tableCode = tableCode .. "|" .. p.getBattleHonour(record.Code) .. "\n" 
        end
    end
	tableCode = tableCode .. "|}"
	return output
end

function p.GetBattleHonour(BHCode)
	local output = ''
	if not data[BHCode] then
		output = "Unknown"
	else
		if string.len(data[BHCode].Link) < 1 then
			output =  "[[File:" ..  data[BHCode].Image .. "|border|x20px|"  .. data[BHCode].Name .."]]"
			output =  data[BHCode].Rank .. " ".. data[BHCode].Name .. "\n|-\n"
		else
			output = output .. "[[File:" ..  data[BHCode].Image .. "|border|x20px|link=" .. data[BHCode].Link .. "|" .. data[BHCode].Name .."]]" .. "\n|-\n"
		end
	end
	return output
end

local function makeRow(battleHonours, country)
    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)
			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 battleHonours = {}
    for i = 1, 23 do
        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))
    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]]