Documentation for this module may be created at Module:ShowTabs/doc

-- <nowiki>
 
-------------------------------------------------------------------------------
--                              Module:ShowTabs
-- This code was imported from the Cowboy Bebop Wiki
-------------------------------------------------------------------------------
 
local DEBUG_MODE = false -- if true, errors are not caught


-------------------------------------------------------------------------------
-- ShowTabs class
-------------------------------------------------------------------------------
 
-- The ShowTabs 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 ShowTabs = {}
ShowTabs.__index = ShowTabs -- Set up inheritance for tables that use ShowTabs as a metatable.
 
-- This function makes a new ShowTabs object. Here we set all the values from the
-- arguments and do any preprocessing that we need.
function ShowTabs.new(args, title)
	local obj = setmetatable({}, ShowTabs) -- Make our object inherit from ShowTabs.
	obj.title = title or mw.title.getCurrentTitle()

	-- Set Film status
	obj.hasFilmTitle = obj:findFilmTitle()

	return obj
end

-- Whether the current title ends with /Film.
function ShowTabs:findFilmTitle()
	return self.title.text:find('/Film$')
end
 
-- This method is called when the tostring function is used on the ShowTabs object.
function ShowTabs:__tostring()
    local ret = {}
    pagename = self.title.text
    
	local root = mw.html.create('table')
	root
		:css('margin-left', 'auto')
		:css('margin-right', 'auto')
	
	local row = root:tag('tr')
	
	local cell1 = mw.html.create('td')
		
	local cell2 = mw.html.create('td')
	    	
	if self.hasFilmTitle then
	    cell1
	    	:wikitext(string.format(
	    		'[[File:BooksButtonOff.png|150 px|link=%s|%s]]',
	    		pagename:gsub('/Film$', '')
	    	))
		cell2
	       	:wikitext(string.format(
		   		'[[File:FilmsButtonOn.png|150 px|link=]]'
		   	))
	else
	    cell1
		    :wikitext(string.format(
		    	'[[File:BooksButtonOn.png|150 px|link=]]'
		    ))
		cell2
		    :wikitext(string.format(
		    	'[[File:FilmsButtonOff.png|150 px|link=%s/Film]]',
		    	pagename
		    ))
	end
	
	row:node(cell1)
		
	row:node(cell2)
	
    ret[#ret + 1] = tostring(root)
	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 getShowTabsResult ()
		local tabObj = ShowTabs.new(args)
		return tostring(tabObj)
	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 = getShowTabsResult()
	else
		success, result = pcall(getShowTabsResult)
	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:ShowTabs]] error: %s.</strong>' ..
			'[[' .. 'Category:Pages with bad parameters in Template:ShowTabs]]',
			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:ShowTabs|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>
Community content is available under CC-BY-SA unless otherwise noted.