มอดูล:Formatted appearance

-- This module requires the use of Module:List.
local list = require('Module:List')
local p = {}
-- Local function which is used to get a correctly formatted entry.
-- Function checks if the array had a value added by checking the counter,
-- and returns the relevant result.
local function getFormattedEntry(args, counter)
	if (counter == 1) then																				-- Check if the counter stayed the same.
		return ""																						-- Nothing was added to array; Return empty string.
	elseif (counter == 2) then																			-- Check if only one value was added to the array.
		return args1																					-- Only one value was added to array; Return that value.
	else																								-- The array had more than one value added.
		return list.makeList("unbulleted", args)														-- Call list.makeList() to retrieve the formatted plainlist.
	end
end
--
Local function which is used to format an appearance for a comic book,
in the style of:
	Line 1:  # (with comic book title in italics)
	Line 2: 
For other usages, see createGenericEntry().
The function works with the following combinations:
	-- Only comic book title (example: "The Incredible Hulk").
	-- Title and issue number (example: "The Incredible Hulk" and "181").
	-- Title and release date (example: "The Incredible Hulk and "November 1974").
	-- Title, issue number and release date (example: "The Incredible Hulk", "181" and "November 1974").
	
	-- Only release date (example: "November 1974").
--
local function createComicEntry(appearanceMajor, appearanceMinor, appearanceDate)
	local fullString = {}																				-- Variable to save the array.
	local counter = 1																					-- Variable to save the array counter.
	
	if (appearanceMajor ~= nil) then																	-- Check if a comic book title was entered.
		if (appearanceMinor == nil) then																-- A comic book title was entered; Check if a issue number was entered.
			fullStringcounter = appearanceMajor 														-- A issue was not entered; Add only the comic book title to the array.
			counter = counter + 1																		-- Increment counter by one.
		else 
			fullStringcounter = appearanceMajor .. " " .. appearanceMinor								-- A issue was entered; Add both to the array.
			counter = counter + 1																		-- Increment counter by one.
		end
	end
	
	if (appearanceDate ~= nil) then																		-- Check if a release date was entered.
		fullStringcounter = appearanceDate															-- A release date was entered; Add it to the array.
		counter = counter + 1																			-- Increment counter by one.
	end
	return getFormattedEntry(fullString, counter)														-- Call getFormattedEntry() to get a correctly formatted entry.
end
--
Local function which is used to format an appearance for most usages,
including television, film, books, songs and games, in the style of:
	Line 1:  (in quotes) (Minor works include: TV episodes, chapters, songs and game missions)
	Line 2:  (in italics) (Major works include: TV series, films, books, albums and games)
	Line 3: 
For comic book usages, see createComicEntry().
The function works with the following combinations:
	-- Only minor work title (example: "Live Together, Die Alone").
	-- Minor work title and major work title (example: "Live Together, Die Alone" and "Lost").
	-- Minor work title and release date (example: "Live Together, Die Alone" and "May 24, 2006").
	-- Minor work title, major work title and release date (example: "Live Together, Die Alone", "Lost" and "May 24, 2006").
	
	-- Only major work title (example: "Lost").
	-- major work title and release date (example: "Lost" and "May 24, 2006").
	
	-- Only release date (example: "May 24, 2006").
--
local function createGenericEntry(appearanceMajor, appearanceMinor, appearanceDate)
	local fullString = {}																				-- Variable to save the array.
	local counter = 1																					-- Variable to save the array counter.
	
	if (appearanceMinor ~= nil) then																	-- Check if a minor appearance was entered.
		fullStringcounter = appearanceMinor															-- A minor appearance was entered; Add it to the array.
		counter = counter + 1																			-- Increment counter by one.
	end
	
	if (appearanceMajor ~= nil) then																	-- Check if a major appearance was entered.
		fullStringcounter = appearanceMajor															-- A major appearance was entered; Add it to the array.
		counter = counter + 1																			-- Increment counter by one.
	end
	
	if (appearanceDate ~= nil) then																		-- Check if a release date was entered.
		fullStringcounter = appearanceDate															-- A release date was entered; Add it to the array.
		counter = counter + 1																			-- Increment counter by one.
	end
	return getFormattedEntry(fullString, counter)														-- Call getFormattedEntry() to get a correctly formatted entry.
end
-- Local function which is used to format with a hash symbol comic book issues.
-- For other minor works, see getFormattedGenericMinorWork().
local function getFormattedComicMinorWorkTitle(issue)
	if (issue ~= nil) then																				-- Check if the issue is not nil.
		if (string.find(issue, "#")) then																-- Check if the issue already has a hash symbol.
			return issue																				-- Hash symbol already present; Return issue.
		else
			local formattedString = string.gsub(issue, "%d+", "#%1")									-- Hash symbol not found; Add the symbol before the issue number.
			return formattedString																		-- Return issue.
		end
	else
		return nil																						-- issue is nil; Return nil.
	end
end
-- Local function which is used to format with quotes a minor work title of most types.
-- For comic book issues, see getFormattedComicMinorWork() (see MOS:MINORWORK).
local function getFormattedGenericMinorWorkTitle(title)
	if (title ~= nil) then																				-- Check if the title is not nil.
		return "\"" .. title .. "\""																	-- Title is not nil; Add quotes to the title.
	else
		return nil																						-- Title is nil; Return nil.
	end
end
-- Local function which is used to format with italics a major work title (see MOS:MAJORWORK).
local function getFormattedMajorWorkTitle(title)
	if (title ~= nil) then																				-- Check if the title is not nil.
		return "" .. title .. ""																	-- Title is not nil; Add italics to the title.
	else
		return nil																						-- Title is nil; Return nil.
	end
end
-- Local function which does the actual main process.
local function _getFormattedAppearance(args)
	local appearanceMajor = args'major_work'															-- Get the title of the major work.
	local appearanceMinor = args'minor_work'															-- Get the title of the minor work.
	
	local isComic = false																				-- Variable to save the status of wether the appearence is from a comic book.
	if (args'issue' ~= nil) then																		-- Check if the comic specific issue is not nil.					
		appearanceMinor = args'issue'																	-- Issue is not nil; Get the issue number.
		isComic = true																					-- Set isComic to true.
	end
	
	local appearanceDate = args'date'																	-- Get the release date of the minor work.
	
	local formattedAppearanceMajor = getFormattedMajorWorkTitle(appearanceMajor)						-- Call getFormattedMajorWorkTitle() to get a formatted major work title.
	if (isComic == false) then																			-- Check if the appearance is a comic book appearance.
																										-- The appearance is not a comic book appearance; 
		local formattedAppearanceMinor = getFormattedGenericMinorWorkTitle(appearanceMinor)				-- Call getFormattedGenericMinorWorkTitle() to get a formatted minor work title.
		return createGenericEntry(formattedAppearanceMajor, formattedAppearanceMinor, appearanceDate)	-- Call createGenericEntry() to create an appearance entry.
	else
																										-- The appearance is a comic book appearance. 
		local formattedAppearanceMinor = getFormattedComicMinorWorkTitle(appearanceMinor)				-- Call getFormattedComicMinorWorkTitle() to get a formatted minor work title.
		return createComicEntry(formattedAppearanceMajor, formattedAppearanceMinor, appearanceDate)		-- Call createComicEntry() to create a comic book appearance entry.
	end
end
--
Public function which is used to format the |first_appeared= and |last_appeared= fields.
The usage of this module allows for correct title formatting (see MOS:MAJORWORK and MOS:MINORWORK),
and correct line breaks based on guidelines (see WP:UBLIST).
Parameters:
	-- |major_work=		— optional; The title of the major work the fictional element appeared in.
										Major works include TV series, films, books, albums and games.
	-- |minor_work=		— optional; The title of the minor work the fictional element appeared in.
										Minor works include TV episodes, chapters, songs and game missions.
	-- |issue=			— optional; The number of the comic book issue the fictional element appeared in.
	-- |date=			— optional; The date of the publication/release of the minor work where the fictional element appeared in.
--
function p.getFormattedAppearance(frame)
	local getArgs = require('Module:Arguments').getArgs													-- Use Module:Arguments to access module arguments.
	local args = getArgs(frame)																			-- Get the arguments sent via the template.
	return _getFormattedAppearance(args)																-- Call _getFormattedAppearance() to perform the actual process.
end
return p