Jump to content

Module:Television episode disambiguation description

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gonnym (talk | contribs) at 12:53, 3 October 2018. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- Do not add a style which is not supported by the [[WP:NCTV]] guidelines.

-- This module requires the use of Module:Extract short description.
local extract = require ('Module:Extract short description');

local p = {}

-- Local function used to create the stylized article disambiguation.
local function getStylizedDisambiguation(disambiguation)
	local isDisambiguationExtended = string.find(disambiguation, "episode")				-- Search for the word "episode" in the article name disambiguation (disambiugation is extended).
	
	if not isDisambiguationExtended then												-- Check if the article name has extended disambiguation.
		return "(''" .. disambiguation .. "'')"											-- Article does not have extended disambiguation; 
																						-- Add italics to the disambiguation which should only be the TV series name per [[MOS:ITALICTITLE]] and [[WP:NCTV]].
	else																				-- Articles has extended disambiguation;
		local tvSeries = string.gsub(disambiguation, "episode", "", 1, true)			-- Get the TV series name without the extended disambiguation.
		return "(''" .. tvSeries .. "'' episode)"										-- Add italics to the disambiguation which should only be the TV series name per [[MOS:ITALICTITLE]] and [[WP:NCTV]]; 
	end																					-- and add back the extended disambiguation.

end

-- Local function used to create the stylized article title.
local function getStylizedArticleTitle(articleName)
	local articleTitle = string.gsub(articleName, "%s+%b()$", "", 1, false)				-- Get the article title without the disambiguation.
	return "\"" .. articleTitle .. "\""													-- Add quotation marks to the title per [[MOS:QUOTETITLE]].
end

-- Local function used to get the disambiguated formatted episode link.
local function getDisambiguatedFormattedLink(articleName)
	local disambiguation = string.match(articleName, "%s%((.-)%)")						-- Get the text inside the disambiguation parentheses.
	local stylizedArticleName															-- Variable to save new stylized article name.
	
	if not disambiguation then															-- Check if the article name does not have disambiguation parentheses.
		stylizedArticleName = "\"" .. articleName .. "\""								-- Article does not have disambiguation parentheses; Add quotation marks for the title per [[MOS:QUOTETITLE]].
	else																				-- Article has disambiguation parentheses;
		local stylizedArticleTitle = getStylizedArticleTitle(articleName)				-- Call getStylizedArticleTitle() to get the stylized article title.
		local stylizedDisambiguation = getStylizedDisambiguation(disambiguation)		-- Call getStylizedDisambiguation() to get the stylized disambiguation.
		stylizedArticleName = stylizedArticleTitle .. " " .. stylizedDisambiguation		-- Recreate the article name from the title and disambiguation.
	end
	
	return "[[" .. articleName .. "|" .. stylizedArticleName .. "]]"					-- Create a pipped link and return it.
end

-- Local function used to validate if data was 
-- entered into a parameter of type string.
local function validateStringParam(value)
	if (value == nil or																	-- Check if the value is nil - safety check as this should not be possible.
		value == "") then																-- Check if the value is empty - the param is written in the infobox, but has no data.

		return false																	-- Param has no value; Return false.
	else																				-- Anything else;
		return true																		-- Param has a value; Return true.
	end
end

-- Local function used to create a formatted episode link.
local function getFormmatedArticleLink(articleName, parenthesesPartOfTitle)
	local formattedLink																	-- Variable to save the formatted link.

	if (validateStringParam(parenthesesPartOfTitle) == true) then						-- Call validateStringParam() to check if the parentheses is part of the episode title.
		formattedLink = "\"[[" .. articleName .. "]]\""									-- Parentheses is part of the title; Add quotation marks for the title per [[MOS:QUOTETITLE]].
	else																				-- Parentheses is not part of the title; 
		formattedLink = getDisambiguatedFormattedLink(articleName)						-- Call getDisambiguatedFormmatedLink() to get the disambiguated formatted episode link.
	end

    return formattedLink																-- Return the formmated link.
end

-- Local function used to retrieve the short description
-- from an episode article's Infobox television episode template.
local function getShortDescription(frame, articleName)
	local shortDescription = extract.extract_from_template(
		frame, articleName, "Infobox television episode/sandbox")						-- Call extract_from_template() from Module:Extract short description to get the short description.
	return shortDescription
end

-- Public function used to create an entry for a television episode
-- in a disambiugation page.
-- The entry is in the form of: "<article name>", <short description>
-- If set to "link_only", only a formatted episode link will be returned.
-- See documentation for examples.
--
-- This module function takes three parameters:
--	{{{1}}} or |article= — required; The name of the episode's article name.
--	|is_disambiguated= — optional; Set if the disambiguation is part of the episode name.
--	|link_only= — optional; Set if you are only intrested in getting a formatted article link.
function p.main(frame)
	local articleName = frame.args[1] or frame.args["name"]								-- Get the article name.
	local parenthesesPartOfTitle = frame.args["is_disambiguated"]						-- Get the optional value.
	local formattedLinkOnly = frame.args["link_only"]									-- Get the optional value.

	if (validateStringParam(articleName) == false) then									-- Call validateStringParam() to check if the article name was entered.
		return '<span style="font-size:100%;" class="error">error: an article title is required.</span>' -- No article name was entered; Return error.
	end

	if (validateStringParam(formattedLinkOnly) == true) then							-- Call validateStringParam() to check if only a formatted link is needed.
		return getFormmatedArticleLink(articleName, parenthesesPartOfTitle)				-- Only a formatted link is needed; Call getFormmatedArticleLink() and return a formatted link.
		
	else																				-- A complete entry is needed;
		local formattedLink = getFormmatedArticleLink(
			articleName, parenthesesPartOfTitle)										-- Call getFormmatedArticleLink() and return a formatted link.
		local shortDescription = getShortDescription(frame, articleName)				-- Call getShortDescription() and return the episode's short description.
		
		return formattedLink .. ", " .. shortDescription								-- Return a complete entry.
	end
end	

return p