Module:Eras

--

local iconData = { pre = { image = "Premium-Era-pre.png", tooltip = "The subject of this article appeared Before the Republic.", link = "w:c:starwars:Pre-Republic era" },   old = { image = "Premium-Era-old.png", tooltip = "The subject of this article appeared in the Old Republic era.", link = "w:c:starwars:Old Republic era" },   imp = { image = "Premium-Era-imp.png", tooltip = "The subject of this article appeared in the Rise of the Empire era.", link = "w:c:starwars:Rise of the Empire era" },   reb = { image = "Premium-Era-reb.png", tooltip = "The subject of this article appeared in the Rebellion era.", link = "w:c:starwars:Rebellion era" },   new = { image = "Premium-Era-new.png", tooltip = "The subject of this article appeared in the New Republic era.", link = "w:c:starwars:New Republic era" },   njo = { image = "Premium-Era-njo.png", tooltip = "The subject of this article appeared in the New Jedi Order era.", link = "w:c:starwars:New Jedi Order era" },   leg = { image = "Premium-Era-leg.png", tooltip = "The subject of this article appeared in the Legacy era.", link = "w:c:starwars:Legacy era" },   real = { image = "Premium-Era-real.png", tooltip = "The subject of this article exists in or is relevant to the real world.", link = "Category:Real-world articles" },   fa = { image = "Premium-FeaturedIcon.png", tooltip = "This is a Star Wars Fanon Featured Article.", link = "Star Wars Fanon:Featured articles" },   ffa = { image = "Premium-DefeaturedIcon.png", tooltip = "This is a former Star Wars Fanon Featured Article.", link = "Star Wars Fanon:Featured articles" },   ga = { image = "Premium-GoodIcon.png", tooltip = "This is a Star Wars Fanon Good Article.", link = "Star Wars Fanon:Good articles" },   fga = { image = "Premium-FormerGAicon.png", tooltip = "This is a former Star Wars Fanon Good Article.", link = "Star Wars Fanon:Good articles" },   waw = { image = "Premium-WAW.png", tooltip = "This is a SWF Wiki Awards-winning article.", link = "Star Wars Fanon:The Wiki Awards" }, }

local Eras = {} Eras.__index = Eras

function Eras.new(args, title) local obj = setmetatable({}, Eras) obj.title = title or mw.title.getCurrentTitle

obj.categories = {}

obj.noDisplayTitle = args.notitle obj.displayTitleBase = args.title obj.displayTitleParen = args.title2

if args.type then local override = args.type:lower obj.continuityOverride = override end

do       local icons = {} for _, v in ipairs(args) do           local t = iconData[string.lower(v)] if t then icons[string.lower(v)] = t           else obj.hasBadParameter = true end end obj.icons = icons end

return obj end

function Eras:getIconData(code) return self.icons[code] end

function Eras:hasAnyOfIcons(...) for i = 1, select('#', ...) do       if self:getIconData(select(i, ...)) then return true end end return false end

function Eras:renderIcon(data)

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

function Eras:renderContinuityIcon

local continuity if self.continuityOverride == 'legends' then continuity = 'legends' elseif self.title.namespace == 0 then if self:hasAnyOfIcons('pre', 'old', 'imp', 'reb', 'new', 'njo', 'leg') then continuity = 'legends' elseif self:hasAnyOfIcons('real') then if self.continuityOverride == 'canon' then continuity = 'canon' end else continuity = 'canon' end end

if continuity == 'canon' then local data = { image = 'Premium-Eras-canon.png', tooltip = 'This article details a subject that falls under the Disney canon continuity.', link = 'w:c:starwars:Canon' }       return self:renderIcon(data) elseif continuity == 'legends' then local data = { image = 'Premium-Eras-legends.png', tooltip = 'This article details a subject that falls under the EU/Legends continuity.', link = 'w:c:starwars:Star Wars Legends' }       return self:renderIcon(data) end end

function Eras:renderPublishingIcons if self.continuityOverride ~= 'canon' then local ret = {} local codes = {'pre', 'old', 'imp', 'reb', 'new', 'njo', 'leg'} for _, code in ipairs(codes) do           local data = self:getIconData(code) if data then ret[#ret + 1] = self:renderIcon(data) end end return table.concat(ret) end end

function Eras:renderNonPublishingIcons local ret = {} local codes = {'real', 'fa', 'ffa', 'ga', 'fga', 'waw'} for _, code in ipairs(codes) do       local data = self:getIconData(code) if data then ret[#ret + 1] = self:renderIcon(data) end end return table.concat(ret) end

function Eras:renderIcons local icons = {} icons[#icons + 1] = self:renderContinuityIcon icons[#icons + 1] = self:renderPublishingIcons

icons = table.concat(icons)

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

return tostring(root) end

function Eras:__tostring local ret = {} ret[#ret + 1] = self:renderIcons return table.concat(ret) end

local p = {} function p._main(args)

local function getErasResult local erasObj = Eras.new(args)

return tostring(erasObj) end

local success, result success, result = pcall(getErasResult)

if success then return result else return string.format(           ' Template:Eras error: %s. ' ..            '' .. 'Category:Pages with bad parameters in Template:Eras',            result -- this is the error message        ) end end

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

-- --