FANDOM


-- Renders the Canon and Legends tabs for articles that are in both
-- continuities.
function Eras:renderCanonTab()
	if self.isHidden or self.title.namespace ~= 0 then
		-- Exit if we have been explicitly hidden or if we are not in the main
		-- namespace.
		return nil
	end
 
	-- Find the page type, canon title, and legends title.
	local pageType, canonTitle, legendsTitle
	if self.legendsArticle then
		pageType = 'canon'
		canonTitle = self.title.text
		legendsTitle = self.legendsArticle
	elseif self.canonArticle then
		pageType = 'legends'
		canonTitle = self.canonArticle
		legendsTitle = self.title.text
	elseif self:hasCanonTitle() then
		pageType = 'canon'
		canonTitle = self.title.text
		legendsTitle = canonTitle:match('^(.*)/Puppet$') or canonTitle
	elseif self:hasLegendsTitle() then
		pageType = 'legends'
		legendsTitle = self.title.text
		canonTitle = legendsTitle:match('^(.*)/Puppet$') or legendsTitle
	elseif exists(self.title.text .. '/Puppet') then
		pageType = 'canon'
		legendsTitle = self.title.text .. '/Puppet'
		canonTitle = self.title.text
	elseif exists(self.title.text .. '/Canon') then
		pageType = 'legends'
		canonTitle = self.title.text .. '/Canon'
		legendsTitle = self.title.text
	else
		-- Could not determine that the article has both a Canon and a Legends
		-- version, so exit.
		return nil
	end
 
	if self:hasCanonTitle() then
		self:addCategory('Articles with /Canon')
	end
 
	-- Make the table root.
	local root = mw.html.create('table')
	root
		:attr('id', 'canontab')
		:css('text-align', 'center')
		:css('padding', '0')
		:css('margin', '0 0 5px 0')
		:css('border-left', '0')
		:css('border-right', '0')
		:css('border-top', '0')
		:css('border-bottom', '7px solid #002e54')
		:css('border-spacing', '0')
		:css('border-collapse', 'collapse')
		:css('width', '100%')
		:css('vertical-align', 'top')
 
	local row = root:tag('tr')
 
	-- This makes one Canon/Legends cell. Having this as a function rather than
	-- doing it with chaining allows us to avoid putting the same code in twice.
	local function makeCell(id, color, image, link, tooltip)
		local cell = mw.html.create('td')
		cell
			:attr('id', id)
			:css('padding', '0 0 5px 0')
			:css('background-color', color)
			:css('line-height', '0.95em')
			:css('font-size', '150%')
			:css('font-weight', 'bold')
			:css('width', '20px')
			:css('vertical-align', 'top')
			:tag('span')
				:addClass('content-bg rtop')
				:css('background', '#ffffff')
				:tag('span')
					:addClass('r1')
					:css('background', color)
					:done()
				:tag('span')
					:addClass('r2')
					:css('background', color)
					:done()
				:tag('span')
					:addClass('r3')
					:css('background', color)
					:done()
				:tag('span')
					:addClass('r4')
					:css('background', color)
					:done()
				:done()
			:wikitext(string.format(
				' [[File:%s|link=%s|%s]]',
				image, link, tooltip
			))
		return cell
	end
 
	local foregroundColor = '#002e54'
	local backgroundColor = '#d8e9fc'
 
	-- Make the canon cell.
	do
		local id = 'canontab-canon'
		local link = canonTitle
		local color, image, tooltip
		if pageType == 'canon' then
			color = foregroundColor
			image = 'Tab-canon-white.png'
			tooltip = 'This article covers the Original version of this subject.'
		else
			color = backgroundColor
			image = 'Tab-canon-black.png'
			tooltip = "Click here for The Puppet Saga's article on the Canon " ..
				"version of this subject."
		end
		row:node(makeCell(id, color, image, link, tooltip))
	end
 
	-- First separator cell
	row:tag('td')
		:attr('id', 'canontab-separator1')
		:css('width', '3px')
		:wikitext(' ')
 
	-- Make the legends cell
	do
		local id = 'canontab-legends'
		local link = legendsTitle
		local color, image, tooltip
		if pageType ~= 'canon' then -- is a Legends page
			color = foregroundColor
			image = 'Tab-legends-white.png'
			tooltip = 'This article covers the Original version of this subject.'
		else -- is a Canon page
			color = backgroundColor
			image = 'Tab-legends-black.png'
			tooltip = "Click here for The Puppet Saga's article on the Legends " ..
				"version of this subject."
		end
		row:node(makeCell(id, color, image, link, tooltip))
	end
 
	-- Second separator cell
	row:tag('td')
		:attr('id', 'canontab-separator2')
		:css('width', '3000px')
		:wikitext(' ')
 
	return tostring(root)
end
-- Render all the categories that were specified using Eras:addCategory or with
-- category flags.
function Eras:renderCategories()
	local fullPagename = self.title.prefixedText
	if fullPagename == 'Template:Eras' or fullPagename == 'Template:Eraicon' then
		-- Exit if we are on a blacklisted page.
		return nil
	end
 
	local pagename = self.title.text
 
	-- Renders one category.
	local function renderCategory(cat, sort)
		return string.format(
			'[[%s:%s|%s]]', 'Category', cat, sort or pagename
		)
	end
 
	local ret = {}
 
	-- Render categories from Eras:addCategory
	for i, t in ipairs(self.categories) do
		ret[i] = renderCategory(t.category, t.sortKey)
	end
 
	-- Render categories from category flags.
	if self.hasBadParameter then
		ret[#ret + 1] = renderCategory(
			'Pages with bad parameters in Template:Eras'
		)
	end
	if self.hasIncorrectProtectionIcon then
		ret[#ret + 1] = renderCategory(
			'Pages with incorrect protection icons'
		)
	end
 
	return table.concat(ret)
end
 
-- This method is called when the tostring function is used on the Eras object.
-- (This works in a similar fashion to Eras.__index above.) It calls all the
-- top-level render methods and returns the final output.
function Eras:__tostring()
	local ret = {}
	ret[#ret + 1] = self:renderDisplayTitle()
	ret[#ret + 1] = self:renderIcons()
	ret[#ret + 1] = self:renderCanonTab()
	ret[#ret + 1] = self:renderCategories()
	return table.concat(ret)
end
 
-------------------------------------------------------------------------------
-- Exports
-------------------------------------------------------------------------------
 
local p = {}
 
-- This function is the entry point from other Lua modules.
function p._main(args)
	-- Define a function to call from pcall so that we can catch any errors
	-- and display them to the user rather than the cryptic "Script error".
	-- (It's not so cryptic if you click on it to see the error message, but
	-- not so many users know to do that.)
	local function getErasResult ()
		local erasObj = Eras.new(args)
		return tostring(erasObj)
	end
 
	-- Get the result. We only catch errors if debug mode is set to false.
	local success, result
	if DEBUG_MODE then
		success = true
		result = getErasResult()
	else
		success, result = pcall(getErasResult)
	end
 
	-- Return the result if there were no errors, and a formatted error message 
	-- if there were.
	if success then
		return result
	else
		return string.format(
			'<strong class="error">[[Template:Eras]] error: %s.</strong>' ..
			'[[' .. 'Category:Pages with bad parameters in Template:Eras]]',
			result -- this is the error message
		)
	end
end
 
-- This is the function accessed from wikitext. It must be accessed through
-- a template. The template should transclude only the text
-- "{{#invoke:Eras|main}}", and then when that template is used, any arguments
-- passed to it are magically sent through to the module.
function p.main(frame)
	local args = {}
	for k, v in pairs(frame:getParent().args) do
		v = v:match('^%s*(.-)%s*$') -- trim whitespace
		if v ~= '' then
			args[k] = v
		end
	end
	return p._main(args)
end
 
return p
 
-- </nowiki>
-- [[Category:Eras utility templates|{{PAGENAME}}]]

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.