Module:Ref

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 16:45, 11 March 2024 by KojoB (talk | contribs)
Jump to navigation Jump to search

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

local p = {}

function ContainsOr(base, options)
	local found = false
	for _, option in ipairs(options) do
	    if base == option then
	        found = true
	        break
	    end
	end
	return found
end
	
function CreateLink(url, title)
	return "[" .. url .. " " .. title .. "]"
end

function p.main(frame)
	local parent_args = frame:getParent().args
	local final_content = ""
	
	-- Variables from parent template.
		-- Ref tag parameters.
		local name = parent_args["name"]
		local group = parent_args["group"] -- "lower-alpha" or nil mostly
		-- MLA citation parameters.
		local title = parent_args["title"]
		local url = parent_args["url"]
		local author = parent_args["author"]
		local publisher = parent_args["publisher"]
		local platform = parent_args["platform"]
		local handle = parent_args["handle"]
		local date = parent_args["date"]
		local archived = parent_args["archived"]
		local isbn = parent_args["isbn"]
	
	-- Link platform-specific handles and get name.
	local handle_link = ""
	local platform_name = ""
	if ContainsOr(platform, {"Twitter", "X", "twitter", "x"}) then
		handle_link = "[https://twitter.com/@" .. handle .. " @" .. handle .. "]"
		platform_name = "Twitter / X"
	elseif ContainsOr(platform, {"YouTube", "youtube", "YT", "yt"}) then
		handle_link = CreateLink("https://www.youtube.com/@" .. handle, "@" .. handle)
		platform_name = "YouTube"
	elseif ContainsOr(platform, {"JoJo News", "jojonews", "jojo news", "JOJO News", "JoJo-News", "jojo-news"}) then
		handle_link = CreateLink("https://jojo-news.com/author/" .. handle, handle)
		platform_name = "JoJo-News"
	end
		
	if parent_args[1] then
		final_content = parent_args[1]
	elseif title then
		if author then
			final_content = final_content .. author
			if handle then
				final_content = final_content .. " <nowiki>[</nowiki>" .. handle_link .. "]"
			end
			if date then 
				final_content = final_content .. " (" .. date .. "), "
			end
		end
		if url then
			final_content = final_content .. "''\"" .. CreateLink(url, title) .. "\"''"
			if archived == true then
				final_content = final_content .. " (Archived)"
			end
		else
			final_content = final_content .. "''\"" .. title .. "\"''"
		end
		if publisher then
			final_content = final_content .. ", " .. publisher
		elseif platform then
			final_content = final_content .. ", on " .. platform_name
		end
		if (not author) and date then
			final_content = final_content .. ", " .. date
		end
		final_content = final_content .. "."
		if isbn then
			final_content = final_content .. " ISBN: " .. frame:expandTemplate{ title = "ISBN", args = { isbn }}
		end
	end
	
	return frame:extensionTag{ name = "ref", content = final_content, args = { name = name, group = group }}
end

return p