Module:Icons

--

--- --                             Module:Icons -- -- This module renders the icons in the top-right corner of articles. -- Credits to Wookieepedia. -- This module has been modified to only have the icons present and not -- DISPLAYTITLE and Canon/Legends tabs from Wookieepedia. ---

local DEBUG_MODE = false -- if true, errors are not caught

--- -- Icon data ---

-- -- This table stores data for all the icons displayed in the top-right. It can -- have the following fields: -- * image - the icon image name, minus any "File:" prefix (required). -- * tooltip - the icon tooltip (optional). -- * link - the page to link from the icon (optional). -- * category - a category to go with the icon, minus any "Category:" prefix --    (optional). -- * protectionAction - for protection icons, an action such as "edit" or "move" --     to check (optional). -- * protectionLevel - for protection icons, the protection level to check, --     such as "sysop". If the page doesn't have the right protection level --     it is put in a tracking category and the icon is not displayed --     (optional). -- Note: this is just a convenient place to store the data. The subtables are -- accessed from the code manually, so adding new subtables won't automatically -- add new icons, and removing subtables may break things. --

local iconData = { admin = { image = "AdminBadge.png", tooltip = "This user has administrator privileges on the TV5 Wiki.", link = "TV5 Wiki:Staff#Administrators", category = "Administrators" },	cignal = { image = "CignalIcon.png", tooltip = "This channel is available on Cignal.", link = "Category:Channels available on Cignal", category = "Channels available on Cignal" },	audio = { image = "Youtube-top.png|20px", tooltip = "Listen to an audio version of this article on Wookieepedia's official YouTube channel.", category = "Articles with audio counterparts" },	fa = { image = "FeaturedIcon.png", tooltip = "This is a featured article.", link = "Animation Wiki:Featured articles", category = "Featured articles" },	ffa = { image = "Premium-DefeaturedIcon.png", tooltip = "This is a former Wookieepedia Featured Article.", link = "Wookieepedia:Featured articles", category = "Wookieepedia former Featured articles" },	ga = { image = "GoodIcon.png", tooltip = "This is a good article.", link = "Animation Wiki:Good articles", category = "Good articles" },	fga = { image = "Premium-FormerGAicon.png", tooltip = "This is a former Wookieepedia Good Article.", link = "Wookieepedia:Good articles", category = "Wookieepedia former Good articles" },	ca = { image = "Premium-ComprehensiveArticle.png", tooltip = "This is a Wookieepedia Comprehensive Article.", link = "Wookieepedia:Comprehensive articles", category = "Wookieepedia Comprehensive articles" },	fca = { image = "Premium-FormerCAIcon.png", tooltip = "This is a former Wookieepedia Comprehensive Article.", link = "Wookieepedia:Comprehensive articles", category = "Wookieepedia former Comprehensive articles" },	fullprot = { protectionAction = "edit", protectionLevel = "sysop", image = "Padlock-red.svg.png", tooltip = "This page is protected from editing.", link = "Animation Wiki:Protection policy#Fully protected", category = "Fully protected" },	semiprot = { protectionAction = "edit", protectionLevel = "autoconfirmed", image = "Padlock-silver.svg.png", tooltip = "This page is semi-protected. Only autoconfirmed users can edit this page.", link = "Animation Wiki:Protection policy#Semi-protected", category = "Semi-protected" },	moveprot = { protectionAction = "move", protectionLevel = "sysop", image = "Padlock-olive.svg.png", tooltip = "This page is protected from moving.", link = "Animation Wiki:Protection policy#Move protected", category = "Move-protected" },	upprot = { protectionAction = "upload", protectionLevel = "sysop", image = "Padlock-purple.svg", tooltip = "This file is upload protected from uploading.", link = "Animation Wiki:Protection policy#Upload protected", category = "Upload-protected" },	cascprot = { protectionAction = "edit", protectionLevel = "sysop", image = "Top-icon-cascadeprotect.png", tooltip = "This page is cascade-protected. All items transcluded onto this page are fully protected as well.", link = "Animation Wiki:Protection policy#Cascade protected", category = "Cascade-protected" },	noncanon = { image = "Premium-Era-inf.png", tooltip = "The subject of this article is considered non-canon." } }

--- -- Helper functions ---

-- Find whether the specified page exists. We use pcall to catch errors if we -- are over the expensive parser function count limit, or a number of other -- juicy errors. This function increases the expensive parser function count -- for every new page called. local function exists(page) local success, title = pcall(mw.title.new, page) return success and title and title.exists or false end

--- -- Eras class ---

-- The eras class does all of the heavy lifting in the module. We use a class -- rather than normal functions so that we can avoid passing lots of different -- values around for each different function.

local Eras = {} Eras.__index = Eras -- Set up inheritance for tables that use Eras as a metatable.

-- This function makes a new eras object. Here we set all the values from the -- arguments and do any preprocessing that we need. function Eras.new(args, title) local obj = setmetatable({}, Eras) -- Make our object inherit from Eras. obj.title = title or mw.title.getCurrentTitle

-- Set object structure obj.categories = {}

-- Set display title parameters obj.noDisplayTitle = args.notitle obj.displayTitleBase = args.title obj.displayTitleParen = args.title2

-- Set audio version link if present obj.audio = args.audio

-- Set hidden status obj.isHidden = args.hide -- Set notoc value obj.hideToc = args.notoc

-- Get the icon data. do local icons = {} for _, v in ipairs(args) do			local t = iconData[string.lower(v)] if t then icons[string.lower(v)] = t			else -- The specified icon wasn't found in the icon data, so set a -- tracking category flag. obj.hasBadParameter = true end end if obj.audio then local t = iconData["audio"] t.link = obj.audio icons["audio"] = t		end obj.icons = icons end

return obj end

-- Raise an error. If DEBUG_MODE is set to false, then errors raised here -- are caught by the export function p._main. function Eras:raiseError(msg) local level if DEBUG_MODE then level = nil else level = 0 -- Suppress module name and line number in the error message. end error(msg, level) end

-- Add a category, to be rendered at the very end of the template output. function Eras:addCategory(cat, sort) table.insert(self.categories, {category = cat, sortKey = sort}) end

-- Shortcut method for getting an icon data subtable. function Eras:getIconData(code) return self.icons[code] end

-- Returns a boolean showing whether any of the icons were specified by the -- user. function Eras:hasAnyOfIcons(...) for i = 1, select('#', ...) do		if self:getIconData(select(i, ...)) then return true end end return false end

-- Renders an eras icon from the given icon data. It deals with the image, -- tooltip, link, and the category, but not the protection fields. function Eras:renderIcon(data) -- Render the category at the end if it exists. if data.category then self:addCategory(data.category) end -- Render the icon and return it. local ret = {} ret[#ret + 1] = '[[File:'	ret[#ret + 1] = data.image	if data.tooltip then		ret[#ret + 1] = '|'		ret[#ret + 1] = data.tooltip	end	if data.link then		ret[#ret + 1] = '|link='		ret[#ret + 1] = data.link	end	ret[#ret + 1] = ']]' return table.concat(ret) end

-- Renders a protection eras icon from the given data. If the page doesn't have -- the specified protection level, returns nil and adds a flag to add a -- tracking category later on in processing. function Eras:renderProtectionIcon(data) if not data.protectionAction then return self:renderIcon(data) end local protectionLevel = self.title.protectionLevels[data.protectionAction] protectionLevel = protectionLevel and protectionLevel[1] if protectionLevel and protectionLevel == data.protectionLevel then return self:renderIcon(data) else self.hasIncorrectProtectionIcon = true end end

-- Renders other icons, e.g. featured article status and protection status. function Eras:renderNonPublishingIcons local ret = {} local codes = {'admin', 'cignal', 'audio', 'fa', 'ffa', 'ga', 'fga', 'ca', 'fca'} for _, code in ipairs(codes) do		local data = self:getIconData(code) if data then ret[#ret + 1] = self:renderIcon(data) end end local protectionCodes = {'fullprot', 'semiprot', 'moveprot', 'upprot', 'cascprot'} for _, code in ipairs(protectionCodes) do		local data = self:getIconData(code) if data then ret[#ret + 1] = self:renderProtectionIcon(data) end end return table.concat(ret) end

-- Render all the icons and eclose them in a surrounding div tag. function Eras:renderIcons local icons = {} icons[#icons + 1] = self:renderNonPublishingIcons icons = table.concat(icons)

local root = mw.html.create('div') root :attr('id', 'icons') :css('float', 'right') :css('position', 'static') :css('display', 'none') :wikitext(icons)

return tostring(root) end

-- 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 exists(self.title.text .. '/Legends') then pageType = 'canon' legendsTitle = self.title.text .. '/Legends' 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

-- Add categories. if pageType == 'canon' then self:addCategory('Canon articles with Legends counterparts') elseif pageType == 'legends' then self:addCategory('Legends articles with canon counterparts') else self:addCategory('Outliers') 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', '0') :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', '5px 0 5px 0') :css('background-color', color) :css('line-height', '0.95em') :css('font-size', '150%') :css('font-weight', 'bold') :css('border-bottom', '3px solid #002e54') :css('min-width', '170px') :css('vertical-align', 'top') :css('border-radius', '5px 5px 0 0') :wikitext(string.format( ' ',				image, link, tooltip ))		return cell end

local foregroundColor = '#002e54' local backgroundColor = '#d8e9fc'

-- Make the canon cell. do local link = canonTitle local color, image, tooltip if pageType == 'canon' then id = 'canontab-canon_ctcw' color = foregroundColor image = 'Tab-canon-white.png' tooltip = 'This article covers the Canon version of this subject.' else id = 'canontab-canon_ctcb' color = backgroundColor image = 'Tab-canon-black.png' tooltip = "Click here for Wookieepedia'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') :css('border-bottom', '3px solid #002e54') :wikitext(' ') -- Make the legends cell do local link = legendsTitle local color, image, tooltip if pageType ~= 'canon' then -- is a Legends page id = 'canontab-legends_ctlw' color = foregroundColor image = 'Tab-legends-white.png' tooltip = 'This article covers the Legends version of this subject.' else -- is a Canon page id = 'canontab-legends_ctlb' color = backgroundColor image = 'Tab-legends-black.png' tooltip = "Click here for Wookieepedia'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' 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', '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:Top'		) end if self.hasIncorrectProtectionIcon then ret[#ret + 1] = renderCategory(			'Pages with incorrect protection icons'		) end

return table.concat(ret) end

-- Add if needed function Eras:renderNotoc if self.hideToc then return '' end return nil 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:renderIcons ret[#ret + 1] = self:renderCanonTab ret[#ret + 1] = self:renderCategories ret[#ret + 1] = self:renderNotoc 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) -- Temporary hack to hide ugly error message on LEGO articles if erasObj == false then return '' .. 'Category:Pages with bad parameters in Template:Top' end 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(			' Template:Eras error: %s. ' ..			'' .. 'Category:Pages with bad parameters in Template:Top',			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 -- "", 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

-- --