Jump to content

Module:Infobox AFL biography/convert

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Renamed user mou89p43twvqcvm8ut9w3 (talk | contribs) at 05:55, 20 June 2016 (testing something to fix arithmetic error). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- [SublimeLinter luacheck-globals:mw]

-- This module serves to convert <br>-delimited teams/years parameters in
-- [[Template:Infobox AFL biography]] to their equivalent numbered pairs. Simply
-- replace "{{Infobox AFL biography" with
-- "{{subst:#invoke:Infobox AFL biography/convert|main" and press "Save".

local p = {}
local getBuffer, print = require("Module:OutputBuffer")()

local function printfd(formatString, ...)
	local default = ""

	local args, argsNum = {...}, select("#", ...)
	local newArgs = {}
	for i = 1, argsNum do
		if args[i] ~= nil then
			table.insert(newArgs, args[i])
		else
			table.insert(newArgs, default)
		end
	end
	print(string.format(formatString, unpack(newArgs)))
end

local function processTeamsYears(teams, years, gamesGoals, teamsParam, yearsParam, gamesGoalsParam)
	local function extractItems(s, t)
		local sentinel = "😂"	-- WTF, Lua?
		string.gsub(string.gsub(s, "<[Bb][Rr] */?>", " " .. sentinel .. " "),
			"[^" .. sentinel .. "]+",
			function(c) table.insert(t, string.match(c, "^%s*(.-)%s*$")) end)
	end
	local newTeams = {}
	extractItems(teams, newTeams)
	local newYears = {}
	extractItems(years, newYears)
	local newGamesGoals = {}
	extractItems(gamesGoals, newGamesGoals)

	if #newTeams ~= #newYears or #newYears ~= #newGamesGoals or #newGamesGoals ~= #newTeams then
		printfd("<!-- Template:Infobox AFL biography conversion error: " ..
			"Parameters not of equal length. -->")
		printfd("| %ss = %s", teamsParam, teams)
		printfd("| %s = %s", yearsParam, years)
		printfd("| %s = %s", gamesGoalsParam, gamesGoals)
		return
	end

	c = 1
	for i = 1, #newTeams do
		if newYears[i] ~= "" or newTeams[i] ~= "" then
			printfd("| %s%s = %s", yearsParam, c,
				newYears[i] ~= "" and newYears[i] or
				"<!-- Template:Infobox AFL biography conversion error: " ..
				"years missing. -->")
			printfd("| %s%s = %s", teamsParam, c,
				newTeams[i] ~= "" and newTeams[i] or
				"<!-- Template:Infobox AFL biography conversion error: " ..
				"team missing. -->")
			printfd("| %s%s = %s", gamesGoalsParam, c,
				newGamesGoals[i] ~= "" and newGamesGoals[i] or
				"<!-- Template:Infobox AFL biography conversion error: " ..
				"games(goals) missing. -->")
			c = c + 1
		end
	end
end

function p._main(args)
	print("{{Infobox AFL biography")
	if args.embed then
		printfd("| embed = %s", args.embed)
	end
	if args.headercolor then
		printfd("| header-color = %s", args.headercolor)
	end
	printfd("| name = %s", args.name or args.playername)
	printfd("| image = %s", args.image)
	if args.image_size or args.imagesize then
		printfd("| image_size = %s", args.image_size or args.imagesize)
	end
	if args.image or args.alt then
		printfd("| alt = %s", args.alt)
	end
	if args.image or args.caption then
		printfd("| caption = %s", args.caption)
	end
	if args.fullname then
		printfd("| fullname = %s", args.fullname)
	end
	if args.nickname then
		printfd("| nickname = %s", args.nickname)
	end
	printfd("| birth_date = %s", args.birth_date)
	printfd("| birth_place = %s", args.birth_place)
	printfd("| death_date = %s", args.death_date)
	printfd("| death_place = %s", args.death_place)
	if args.originalteam then
		printfd("| originalteam = %s", args.originalteam)
	end
	if args.draftpick then
		printfd("| draftpick = %s", args.draftpick)
	end
	if args.debutdate then
		printfd("| debutdate = %s", args.debutdate)
	end
	if args.debutteam then
		printfd("| debutteam = %s", args.debutteam)
	end
	if args.debutopponent then
		printfd("| debutopponent = %s", args.debutopponent)
	end
	if args.debutstadium then
		printfd("| debutstadium = %s", args.debutstadium)
	end
	printfd("| heightweight = %s", args.heightweight)
	printfd("| position = %s", args.position)
	if args.otheroccupation then
		printfd("| otheroccupation = %s", args.otheroccupation)
	end
	if args.currentclub then
		printfd("| currentclub = %s", args.currentclub)
	end
	if args.guernsey then
		printfd("| guernsey = %s", args.guernsey)
	end
	if args.statsend then
		printfd("| statsend = %s", args.statsend)
	end
	if args.coachstatsend then
		printfd("| coachstatsend = %s", args.coachstatsend)
	end
	if args.repstatsend then
		printfd("| repstatsend = %s", args.repstatsend)
	end
	if args.playingteams then
		printfd("| playingteams = %s", args.playingteams)
	end
	if args.coachingteams then
		printfd("| coachingteams = %s", args.coachingteams)
	end
	if args.clubs and args.years and args.games(goals) then
		processTeamsYears(args.clubs, args.years, args.games(goals),
			"club", "years", "games_goals")
	elseif args.clubs or args.years or args.games(goals) then
		print("<!-- Template:Infobox AFL biography conversion error: " ..
		      "Template is missing expected parameters. -->")
		printfd("| clubs = %s", args.clubs)
		printfd("| years = %s", args.years)
		printfd("| games(goals) = %s", args.games(goals))
	end
	if args.sooteams and args.sooyears and args.soogames(goals) then
		processTeamsYears(args.sooteams, args.sooyears, args.soogames(goals),
			"sooteam", "sooyears", "soogames_goals")
	elseif args.sooteams or args.sooyears or args.soogames(goals) then
		print("<!-- Template:Infobox AFL biography conversion error: " ..
		      "Template is missing expected parameters. -->")
		printfd("| sooteams = %s", args.sooteams)
		printfd("| sooyears = %s", args.sooyears)
		printfd("| soogames(goals) = %s", args.soogames(goals))
	end
	if args.nationalteams and args.nationalyears and args.nationalgames(goals) then
		processTeamsYears(args.nationalteams, args.nationalyears, args.nationalgames(goals),
			"nationalteam", "nationalyears", "nationalgames_goals")
	elseif args.nationalteams or args.nationalyears or args.nationalgames(goals) then
		print("<!-- Template:Infobox AFL biography conversion error: " ..
		      "Template is missing expected parameters. -->")
		printfd("| nationalteams = %s", args.nationalteams)
		printfd("| nationalyears = %s", args.nationalyears)
		printfd("| nationalgames(goals) = %s", args.nationalgames(goals))
	end
	if args.coachclubs and args.coachyears and args.coachgames(wins) then
		processTeamsYears(args.coachclubs, args.coachyears, args.coachgames(wins),
			"coachclub", "coachyears", "coachgames_wins")
	elseif args.coachclubs or args.coachyears or args.coachgames(wins) then
		print("<!-- Template:Infobox AFL biography conversion error: " ..
		      "Template is missing expected parameters. -->")
		printfd("| coachclubs = %s", args.coachclubs)
		printfd("| coachyears = %s", args.coachyears)
		printfd("| coachgames(wins) = %s", args.coachgames(wins))
	end
	if args.umpireyears1 then
		printfd("| umpireyears1 = %s", args.umpireyears1)
	end
	if args.umpireleague1 then
		printfd("| umpireleague1 = %s", args.umpireleague1)
	end
	if args.umpirerole1 then
		printfd("| umpirerole1 = %s", args.umpirerole1)
	end
	if args.umpiregames1 then
		printfd("| umpiregames1 = %s", args.umpiregames1)
	end
	if args.umpireyears2 then
		printfd("| umpireyears2 = %s", args.umpireyears2)
	end
	if args.umpireleague2 then
		printfd("| umpireleague2 = %s", args.umpireleague2)
	end
	if args.umpirerole2 then
		printfd("| umpirerole2 = %s", args.umpirerole2)
	end
	if args.umpiregames2 then
		printfd("| umpiregames2 = %s", args.umpiregames2)
	end
	if args.umpireyears3 then
		printfd("| umpireyears3 = %s", args.umpireyears3)
	end
	if args.umpireleague3 then
		printfd("| umpireleague3 = %s", args.umpireleague3)
	end
	if args.umpirerole3 then
		printfd("| umpirerole3 = %s", args.umpirerole3)
	end
	if args.umpiregames3 then
		printfd("| umpiregames3 = %s", args.umpiregames3)
	end
	if args.umpireyears4 then
		printfd("| umpireyears4 = %s", args.umpireyears4)
	end
	if args.umpireleague4 then
		printfd("| umpireleague4 = %s", args.umpireleague4)
	end
	if args.umpirerole4 then
		printfd("| umpirerole4 = %s", args.umpirerole4)
	end
	if args.umpiregames4 then
		printfd("| umpiregames4 = %s", args.umpiregames4)
	end
	if args.umpireyears5 then
		printfd("| umpireyears5 = %s", args.umpireyears5)
	end
	if args.umpireleague5 then
		printfd("| umpireleague5 = %s", args.umpireleague5)
	end
	if args.umpirerole5 then
		printfd("| umpirerole5 = %s", args.umpirerole5)
	end
	if args.umpiregames5 then
		printfd("| umpiregames5 = %s", args.umpiregames5)
	end
	printfd("| careerhighlights = %s", args.careerhighlights)
	print("}}")

	return getBuffer("\n")
end

function p.main(frame)
	local args = require("Module:Arguments").getArgs(frame)
	return p._main(args)
end

return p