Jump to content

Module:MasterGunner

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by BoonDock (talk | contribs) at 17:18, 13 April 2023 (Create MasterGunner). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

require('strict')
local p = {}
local data = mw.loadData('Module:MasterGunner/data')
local getArgs = require('Module:Arguments').getArgs
-- South African Master Gunner Data
-- Data Fields
--      Code: This is just a duplicate of the lookup code, but expressed as a number instead of a string
--      Rank: Person's rank. Not wikilinked
---     FirstName: Initials or first name(s)
--      Surname: Person's Surname
--      Year:   Year appointed Master Gunner
--      WikiPage: If the person has a Wiki article, name of the article here
--      PostNoms: list of Post Nominal Titles. Stored in Lua table format ie Curly braces with values seperated by commas
--      Post: The name of the post that they held when awarded. Commas will be turned into line breaks
--      Note: Any additional info about this Gunner.
--      RecipCat: Category for recipients. "List of Master Gunners (South Africa)"

function p.Gunner(frame)
	local output = ''
	local templateArgs = getArgs(frame)
	local gunnercode = templateArgs["code"] or nil
	local pagename=templateArgs["pagename"] or nil
	if not pagename then
		if not gunnercode then
			output = '<span style="color:#d33">Error: No pagename/person specified</span>'
			return output
		end
	end

	-- Iterate through the data in the table "GunnerData"
	for code, record in pairs(data) do
		-- Check if the PageName is found
		if pagename == record.Name or gunnercode == code then
			-- If it is, send Info
			output = output ..  record.Code .. " Master Gunner" .. "\n"
			output = output .. record.FirstName .. " " .. record.Surname .. "Year: " .. record.Year .. "\n"
			return output
		else
			-- If it isn't, ignore it

		end
	end

	return output
end

function p.GunnerBox(frame)
	local output = ''
	local templateArgs = getArgs(frame)
	local pagename=templateArgs["pagename"] or nil
	local gunnercode = templateArgs["code"] or nil
	local imagesize = templateArgs["imagesize"] or '100px'
	if not pagename then
		if not gunnercode then
			output = '<span style="color:#d33">Error: No "pagename" nor "code" specified</span>[[Category:Master Gunner Error]]'
		return output
		end
	end
	-- Iterate through the data in the table "GunnerData"
	for code, record in pairs(data) do
		-- Check if the PageName is found
		if pagename == record.Name or code == gunnercode then
			-- If either is true, send Info
			 -- Generate wiki table code for the Master Gunner
			local localfloat = templateArgs["float"] or ''
			if not localfloat then
				localfloat = "left"  -- NOTE TODO: Make the table float according to the value of locafloat ;-)
			end
			local tableCode = '{| class="wikitable" \n'
			tableCode = tableCode .. "|+ Master Gunner \n" -- Caption
			tableCode = tableCode .. "|-\n"
			tableCode = tableCode .. "! " .. record.Code .. "!! Master Gunner \n"
			tableCode = tableCode .. "|-\n"
			tableCode = tableCode .. "| colspan=2||[[file:SANDF Arty Master Gunner badge embossed.png|border|" .. imagesize .. "|link=List of badges of the South African Army#Proficiency: Master Gunner|Master Gunner]]\n"
			tableCode = tableCode .. "| colspan=2||" .. p.GetGunner(record.Code) .. "\n"
			tableCode = tableCode .. "|-\n | Year: \n"
			tableCode = tableCode .. "| " .. record.Year .. "\n"
			tableCode = tableCode .. "|-\n"
			local prevgunner="" .. record.code -1 .. ""
			tableCode = tableCode .. "| Prev: " .. p.GetGunner(prevgunner) .. "\n"
			local nextgunner="" .. record.code -1 ..""
			tableCode = tableCode .. "| Next: " .. p.GetGunner(nextgunner) .. "\n"
			tableCode = tableCode .. "|}"
			return tableCode
		else
			-- If it isn't, ignore it

		end
	end

	return output
end
function p.GetGunner(gunnercode)
	local output = ''
	if not data[gunnercode] then
		output = "Unknown"
	else
		if string.len(data[gunnercode].WikiPage) < 1 then
			output =  data[gunnercode].Rank .. " ".. data[gunnercode].FirstName .. " " .. data[gunnercode].Surname
		else
			output = "[[" .. data[gunnercode].WikiPage .. "|" .. data[gunnercode].Rank .. " " .. data[gunnercode].FirstName .. " " .. data[gunnercode].Surname .. "]]"
		end
	end
	return output -- Should never get here, but belts-and -braces... just to be sure
end

function p.GunnerTable(frame)
	-- Data Fields
--      Code: This is just a duplicate of the lookup code, but expressed as a number instead of a string
--      Rank: Person's rank. Not wikilinked
---     FirstName: Initials or first name(s)
--      Surname: Person's Surname
--      Year:   Year appointed Master Gunner
--      WikiPage: If the person has a Wiki article, name of the article here
--      PostNoms: list of Post Nominal Titles. Stored in Lua table format ie Curly braces with values seperated by commas
--      Post: The name of the post that they held when awarded. Commas will be turned into line breaks
--      Note: Any additional info about this Gunner.
--      RecipCat: Category for recipients. "List of Master Gunners (South Africa)"
	local output = ''
	local templateArgs = getArgs(frame)
	local localfloat = templateArgs["float"] or ''
	if not localfloat then
		localfloat = "left"  -- NOTE TODO: Make the table float according to the value of locafloat ;-)
	end
	local tableCode = '{| class="wikitable sortable" \n'
	tableCode = tableCode .. "|+ Master Gunner \n" -- Caption
	tableCode = tableCode .. "|-\n"
	tableCode = tableCode .. "! Number !! Rank !! Surname !! First Names !! PostNoms !! Year !! Note \n"
	-- Iterate through the data in the table "GunnerData"
	for code, record in pairs(data) do
		-- Generate wiki table code for the Master Gunner
		-- TODO: If record.Surname == "Unknown" make the row distinctive somehow.
		tableCode = tableCode .. "|-\n"
		tableCode = tableCode .. "| " .. record.Code .. "\n"
		tableCode = tableCode .. "| " .. record.Rank .. "\n"
		tableCode = tableCode .. "| " .. record.Surname .. "\n"
		tableCode = tableCode .. "| " .. record.FirstName .. "\n"
		tableCode = tableCode .. "| " .. record.PostNoms .. "\n"
		tableCode = tableCode .. "| " .. record.Year .. "\n"
		tableCode = tableCode .. "| <small>" .. record.Note .. "</small>\n"
		
	end
	tableCode = tableCode .. "|}"
	return tableCode
end

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