Module:Crew

From Tardis Wiki, the free Doctor Who reference
Revision as of 18:56, 2 June 2024 by Bongolium500 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Crew/doc

local util_link = require('Module:LinkUtil')
local util_text = require('Module:TextUtil')
local util_vars = require('Module:VarsUtil')
local p = {} --p stands for package
local h = {} --h stands for helper 

function h.standardCredit(label, pluralLabel, varname, smw, smw2, parentArgs)
	--check if variable is needed in the usuage of the credit template
	if parentArgs[varname] and parentArgs[varname] ~= "" then
		--if it is, add it to the output
		outputString = " - [["
		if smw and smw ~= "" then
			outputString = outputString .. smw .. "::"
		end
		outputString = outputString .. parentArgs[varname].. "|" .. util_link.stripDab(parentArgs[varname]) .. "]]"
		if smw2 and smw2 ~= "" then
			frame = mw.getCurrentFrame()
			frame:callParserFunction("#set", {"", [smw2]=parentArgs[varname]})
		end
		--also loop through also further variables for this credit (e.g. for director, director2, director3, etc are also checked)
		local done = false
		local count = 2
		while not done do
			if parentArgs[varname .. tostring(count)] and parentArgs[varname .. tostring(count)] ~= "" then
				outputString = outputString .. ", [["
				if smw and smw ~= "" then
					outputString = outputString .. smw .. "::"
				end
				outputString = outputString .. parentArgs[varname .. tostring(count)].. "|" .. util_link.stripDab(parentArgs[varname .. tostring(count)]) .. "]]"
				if smw2 and smw2 ~= "" then
					frame = mw.getCurrentFrame()
					frame:callParserFunction("#set", {"", [smw2]=parentArgs[varname .. tostring(count)]})
				end
				count = count + 1
			else
				done = true
			end
		end
		
		--add the name of the credit with an S if needed.
		if count > 2 then
			outputString = pluralLabel .. outputString
		else
			outputString = label .. outputString
		end
	end
	return outputString
end

function h.topCredit(label, pluralLabel, varname, smw, smw2, parentArgs)
	--check if variable is needed in the usuage of the credit template
	if parentArgs[varname] and parentArgs[varname] ~= "" then
		--if it is, add it to the output
		outputString = " <br />[["
		if smw and smw ~= "" then
			outputString = outputString .. smw .. "::"
		end
		outputString = outputString .. parentArgs[varname].. "|" .. util_link.stripDab(parentArgs[varname]) .. "]]"
		if smw2 and smw2 ~= "" then
			frame = mw.getCurrentFrame()
			frame:callParserFunction("#set", {"", [smw2]=parentArgs[varname]})
		end
		--also loop through also further variables for this credit (e.g. for director, director2, director3, etc are also checked)
		local done = false
		local count = 2
		while not done do
			if parentArgs[varname .. tostring(count)] and parentArgs[varname .. tostring(count)] ~= "" then
				if parentArgs[varname .. tostring(count+1)] and parentArgs[varname .. tostring(count+1)] ~= "" then
					outputString = outputString .. ", [["
				else
					outputString = outputString .. " & [["
				end
				if smw and smw ~= "" then
					outputString = outputString .. smw .. "::"
				end
				outputString = outputString .. parentArgs[varname .. tostring(count)].. "|" .. util_link.stripDab(parentArgs[varname .. tostring(count)]) .. "]]"
				if smw2 and smw2 ~= "" then
					frame = mw.getCurrentFrame()
					frame:callParserFunction("#set", {"", [smw2]=parentArgs[varname .. tostring(count)]})
				end
				count = count + 1
			else
				done = true
			end
		end
		
		--add the name of the credit with an S if needed.
		if count > 2 then
			outputString = "'''" .. pluralLabel .. "'''" .. outputString
		else
			outputString = "'''" .. label .. "'''" .. outputString
		end
		outputString = outputString .. "<br /><br />"
	end
	return outputString
end

function h.highestCredit(label, pluralLabel, varname, smw, smw2, parentArgs)
	--check if variable is needed in the usuage of the credit template
	if parentArgs[varname] and parentArgs[varname] ~= "" then
		--if it is, add it to the output
		outputString = "&nbsp;&nbsp;&nbsp;[["
		if smw and smw ~= "" then
			outputString = outputString .. smw .. "::"
		end
		outputString = outputString .. parentArgs[varname].. "|" .. util_link.stripDab(parentArgs[varname]) .. "]]"
		if smw2 and smw2 ~= "" then
			frame = mw.getCurrentFrame()
			frame:callParserFunction("#set", {"", [smw2]=parentArgs[varname]})
		end
		--also loop through also further variables for this credit (e.g. for director, director2, director3, etc are also checked)
		local done = false
		local count = 2
		while not done do
			if parentArgs[varname .. tostring(count)] and parentArgs[varname .. tostring(count)] ~= "" then
				if parentArgs[varname .. tostring(count+1)] and parentArgs[varname .. tostring(count+1)] ~= "" then
					outputString = outputString .. ", [["
				else
					outputString = outputString .. " and [["
				end
				if smw and smw ~= "" then
					outputString = outputString .. smw .. "::"
				end
				outputString = outputString .. parentArgs[varname .. tostring(count)].. "|" .. util_link.stripDab(parentArgs[varname .. tostring(count)]) .. "]]"
				if smw2 and smw2 ~= "" then
					frame = mw.getCurrentFrame()
					frame:callParserFunction("#set", {"", [smw2]=parentArgs[varname .. tostring(count)]})
				end
				count = count + 1
			else
				done = true
			end
		end
		
		--add the name of the credit with an S if needed.
		if count > 2 then
			outputString = "'''''" .. pluralLabel .. "'''''" .. outputString
		else
			outputString = "'''''" .. label .. "'''''" .. outputString
		end
	end
	return outputString
end




--for use when building crew credit templates (in the style of {{Wales Crew}})
function p.generateCredit(frame)
	--get frames and args
	local parentFrame = frame:getParent()
	local args = frame.args
	local parentArgs = parentFrame.args
	
	--initialise the output - it needs to be a blank string so that something is output at the end even if that something is blank
	local outputString = ""
	
	--process arguments
	local credit = args["credit"]
	local varname
	if args["varname"] and args["varname"] ~= "" then
		varname = args["varname"]
	else
		varname = util_text.pascalCase(util_link.stripDab(credit))
	end
	local label
	if args["label"] and args["label"] ~= "" then
		label = args["label"]
	elseif args["display"] and args["display"] ~= "" then
		label = "[[" .. credit .. "|" .. args["display"] .. "]]"
	else
		label = "[[" .. credit .. "|" .. util_text.titleCase(util_link.stripDab(credit)) .. "]]"
	end
	local pluralLabel
	if args["pluralLabel"] and args["pluralLabel"] ~= "" then
		pluralLabel = args["pluralLabel"]
	elseif args["pluralisation"] and args["pluralisation"] ~= "" then
		pluralLabel = "[[" .. credit .. "|" .. args["pluralisation"] .. "]]"
	elseif args["notPlural"] then
		pluralLabel = label
	else
		pluralLabel = label .. "s"
	end
	local smw = args["smw"]
	local smw2 = args["smw2"]
	local display
	if args["display"] and args["display"] ~= "" then
		display = args["display"]
	else
		display = util_text.titleCase(util_link.stripDab(credit))
	end
	
	local creditType = args["creditType"]
	
	if creditType == "highest" then
		outputString = h.highestCredit(label, pluralLabel, varname, smw, smw2, parentArgs)
	elseif creditType == "top" then
		outputString = h.topCredit(label, pluralLabel, varname, smw, smw2, parentArgs)
	else
		outputString = h.standardCredit(label, pluralLabel, varname, smw, smw2, parentArgs)
	end
	
	util_vars.setVar("autoDocumentation", (util_vars.getVar("autoDocumentation") or "") .. "|" .. varname .. "=")
	
	return outputString
end

return p