Module:MasterGunner

in Wikipedia, die vrye ensiklopedie

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 separated 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 gunnercode then
		output = '<span style="color:#d33">Error: No code specified</span>'
		return output
	end
	if data[gunnercode] then
		output = output .. gunnercode .. ": "
		output = output ..  data[gunnercode].Code .. " Meesterkanonieer van die artillerie" .. "<br />"
		output = output .. data[gunnercode].FirstName .. " " .. data[gunnercode].Surname .. " Year: " .. data[gunnercode].Year .. "<br />"
		return output
	else
		output = '<span style="color:#d33">Error: code '.. gunnercode .. ' not found</span>'
	end
	return output
end

function p.GunnerBox(frame)
	local output = ''
	local templateArgs = getArgs(frame)
	local gunnercode = templateArgs["code"] or nil
	local imagesize = templateArgs["imagesize"] or '150px'
		if not gunnercode then
			output = '<span style="color:#d33">Error: No "code" specified. Use the Gunner number preceded by an A for example, code=A100</span>[[Category:Master Gunner Error]]'
		return output
		end

	if data[gunnercode] then
		-- Send Info
		-- Generate wiki table code for the Master Gunner

		local float = templateArgs.float or 'none'
    	if float == "left" then 
        	float = "floatleft"
    	else
        	if float == "right" then
            	float = "floatright"
        	end
    	end
    	if float == "none" then float = '' end

		local tableCode = '{| class="wikitable ' .. float .. '" style="text-align:center" \n'
--		tableCode = tableCode .. "|+ Meesterkanonnier van die artillerie \n" -- Caption
		tableCode = tableCode .. "|-\n"
		tableCode = tableCode .. '!colspan=2 style="background:red; color:white;"| Meesterkanonieer van die artillerie: ' .. data[gunnercode].Code .. '\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| Meesterkanonnier van die artillerie]]\n"
		tableCode = tableCode .. "|-\n"
		tableCode = tableCode .. "|colspan=2|" .. p.GetGunner("A" .. data[gunnercode].Code)
		if string.len(data[gunnercode].Post) > 1 then
			tableCode = tableCode .. mw.getCurrentFrame():expandTemplate{ title = 'Efn', args = {"Post occupied when award was made: " .. data[gunnercode].Post} }
		end
		if string.len(data[gunnercode].Note) > 1 then
			tableCode = tableCode .. mw.getCurrentFrame():expandTemplate{ title = 'Efn', args = {data[gunnercode].Note} }
		end
		tableCode = tableCode .. "\n"
		tableCode = tableCode .. "|-\n "
		tableCode = tableCode .. "|colspan=2|[[Meesterkanonnier van die artillerie (Suid-Afrika)|Year]]: '''".. data[gunnercode].Year .. "'''\n"
		tableCode = tableCode .. "|-\n"
		local prevgunner="A" .. data[gunnercode].Code -1 .. ""
		tableCode = tableCode .. "| ←" .. data[gunnercode].Code -1 .. ": " .. p.GetGunner(prevgunner) .. "\n"
		local nextgunner="A" .. data[gunnercode].Code +1 ..""
		tableCode = tableCode .. "| " .. p.GetGunner(nextgunner) .. " :".. data[gunnercode].Code +1 .. "→\n"
		tableCode = tableCode .. "|}"
		return tableCode
	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 .. "<br />".. data[gunnercode].FirstName .. " " .. data[gunnercode].Surname
		else
			output = data[gunnercode].Rank .. "<br />" .. "[[" .. data[gunnercode].WikiPage .. "|" ..  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 .. "|+ Lys van Meesterkanonieere van die artillerie \n" -- Caption
	tableCode = tableCode .. "|-\n"
	tableCode = tableCode .. "! Kode !!Nommer !! Rang !! Van !! Voorname !! Pos !! Jaar !! Nota \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 .. "|" ..code .. "\n"
		tableCode = tableCode .. "| " .. record.Code .. "\n"
		tableCode = tableCode .. "| " .. record.Rank .. "\n"
		if string.len(record.WikiPage) < 1 then
			tableCode = tableCode .. "| " .. record.Surname 
		else
			tableCode = tableCode .. "| " .. "[[" .. record.WikiPage .. "|" .. record.Surname .. "]]"
		end
	--	if string.len(record.Post)>1 then
	--		tableCode = tableCode .. mw.getCurrentFrame():expandTemplate{ title = 'Efn', args = {"Post occupied when award was made: " .. record.Post} }
	--	end
		tableCode = tableCode .. "\n"
		tableCode = tableCode .. "| " .. record.FirstName .. "\n"
		tableCode = tableCode .. "| " .. string.gsub(record.Post, ",", " - <br />") .. "\n"
		tableCode = tableCode .. "| " .. record.Year .. "\n"
		tableCode = tableCode .. "| <small>" .. record.Note .. "</small>\n"
		
	end
	tableCode = tableCode .. "|}" -- End the table
	return tableCode
end

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