Module:Sandbox/Gonnym/sometest5

Revision as of 12:41, 20 August 2020 by imported>Gonnym
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Sandbox/Gonnym/sometest5/doc

local helper = require('Module:Sandbox/Gonnym/sometest5/2')

local p = {}

local ERROR_MESSAGE = '[[Template:Period in television category]] is for use only on category pages whose title is of the form "YYYY in television", where YYYY is a 4-digit year, or "YYY0s in television", where YYYY0s is a 4-digit decade (e.g. "1970s" or "2010s") or "XXth century in television", where XX is a 2-digit century.'

local categoryHeaderList = {
	[1] = {
		["type"] = "year",
		["pattern"] = "^(%d%d%d%d) in television$",
	},
	[2] = {
		["type"] = "decade",
		["pattern"] = "(%d%d%d0)(s) in television$",
	},
	[3] = {
		["type"] = "century",
		["pattern"] = "(%d%d)([snrt][tdh]) century in television$",
	},
}

local function getCategories(categoryHeader)
	local categoryList = {
		["in_mass_media"] = "[[Category:%s in mass media|Television]]",	-- year/decade/century value
		["in_telvision"] = "[[Category:%s in television]]",		-- decade/century/millennium value
		["television_by"] = "[[Category:Television by %s]]"		-- the word "year"/"decade"/"century"
	}
	categories = {}
	table.insert(categories, string.format(categoryList["in_mass_media"], categoryHeader.timePeriod .. categoryHeader.suffix))
	table.insert(categories, string.format(categoryList["in_telvision"], helper.getNextTierTimePeroid(categoryHeader, categoryHeader.timePeriod)))
	table.insert(categories, string.format(categoryList["television_by"], categoryHeader.type))
	return table.concat(categories)
end

local function getHeaderText(frame, categoryHeader)
	local headerText = "Topics specifically related to the %s '''[[%s]]''' in '''[[television]]'''."
	return string.format(headerText, categoryHeader.link[1], categoryHeader.link[2])
end

local function setAdditionalParameterValues(frame, categoryHeader)
	if (categoryHeader.type == "year") then
		categoryHeader.link = {"year", categoryHeader.timePeriod}
	elseif (categoryHeader.type == "decade") then
		categoryHeader.link = {"decade", frame:expandTemplate{title = "Decade link", args = {categoryHeader.timePeriod}}}
	elseif (categoryHeader.type == "century") then
		categoryHeader.suffix = categoryHeader.suffix .. "-century"
		categoryHeader.link = {"", categoryHeader.timePeriod .. categoryHeader.suffix}
	end
end

local function getCategoryLayout(frame, title, categoryHeader)
	setAdditionalParameterValues(frame, categoryHeader)
	local portals = helper.getPortals(frame, {"Television"}, timePeriod)
	local hatnote = helper.getCatMainTemplate(frame, title)
	local headerText = getHeaderText(frame, categoryHeader)
	local navigationTemplate = helper.getNavigationTemplate(frame, categoryHeader)
	local categories = getCategories(categoryHeader)
	
	return portals .. hatnote .. headerText .. navigationTemplate .. categories .. "__NOGALLERY__"
end

local function get_category_header(title)
	for i = 1, #categoryHeaderList do
		local categoryHeader = categoryHeaderList[i]
		local timePeriod, suffix = string.match(title, categoryHeader.pattern)
		if (timePeriod) then
			categoryHeader.timePeriod = timePeriod
			categoryHeader.suffix = suffix or ""
			return categoryHeaderList[i]
		end
	end
	return nil
end

local function _main(frame, args)
	local pageName = mw.title.getCurrentTitle()
	if (helper.isOnCategory(pageName)) then
		local title = pageName.text
		categoryHeader = get_category_header(title)
		if (categoryHeader) then
			categoryHeader.suffix = suffix or ""
			return getCategoryLayout(frame, title, categoryHeader)
		end
		return helper.getError(helper.errorMessagePrefixList["INVALID_PAGE_NAME"], ERROR_MESSAGE)
	else
		return helper.getError(helper.errorMessagePrefixList["NOT_ON_CATEGORY"], ERROR_MESSAGE)
	end
end


--local function byCountryTest()
--	pattern = ^(%d%d%d%d) in television by country$
--	error = adds "by country" to the example
--	containerCategory = helper.getContainerCategoryTemplate(frame)
--	header = "Topics specifically related to the %s '''[[%s]]''' in '''[[television]]''', by country."
--	categories = [[Category:%s by country| Television]] - year/dacade/century value
--	categories = [[Category:%s in television| Country]] - year/dacade/century value
--	categories = [[Category:Television by %s by country]] - the word year/dacade/century
--	categories = [[Category:%s in television by country]] - dacade/century/millinum value
--end

function p.main(frame)
	local getArgs = require('Module:Arguments').getArgs;
	local args  = getArgs(frame)
	return _main(frame, args) .. frame:expandTemplate{title = "CatAutoTOC"}
end

return p