Jump to content

Module:Episode table

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Alex 21 (talk | contribs) at 11:21, 14 July 2016 (Create module.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

-- This module implements {{Episode table}}.

--------------------------------------------------------------------------------
-- EpisodeTable class
-- The main class.
--------------------------------------------------------------------------------

local contrast_ratio = require('Module:Color contrast')._ratio
local EpisodeTable = {}

function EpisodeTable.cell(background, width, text, reference)
	local cell = mw.html.create('th')
	
	-- Cell
	cell:attr('scope','col')
		:css('background',background or '#CCCCFF')
		:css('width',width ~= '' and width .. '%' or nil)
		:wikitext(text);
	
	-- Reference
	local link1_cr = contrast_ratio{'#0645AD', background or '#CCCCFF', ['error'] = 0}
	local link2_cr = contrast_ratio{'#0B0080', background or '#CCCCFF', ['error'] = 0}
	
	if reference then
		local refspan = mw.html.create('span')
			:wikitext(reference)
		
		if link1_cr < 7 or link2_cr < 7 then
			refspan
				:css('background-color','white')
				:css('padding','1px')
				:css('display','inline-block')
				:css('line-height','50%')
			end
		
		cell:wikitext("&#8202;" .. tostring(refspan))
	end
	
	return cell
end

function EpisodeTable.abbr(text,title)
	local abbr = mw.html.create('abbr')
		:attr('title',title)
		:wikitext(text)
	return tostring(abbr)
end

function EpisodeTable.new(args)
	args = args or {}
	local categories = ''
	local background = args.background;
	
	-- Create episode table
	local root = mw.html.create('table')
	
	root
		:addClass('wikitable')
		:addClass('plainrowheaders')
		:addClass('wikiepisodetable')
		:css('width', args.total_width and string.gsub(args.total_width,'%','') .. '%' or '100%')
	
	-- Caption
	if args.caption then
		root:tag('caption'):wikitext(args.caption)
	end
	
	-- Colour contrast
	local black_cr = contrast_ratio{background, 'black', ['error'] = 0}
	local white_cr = contrast_ratio{'white', background, ['error'] = 0}
	if black_cr < 7 and white_cr < 7 then
		categories = categories .. '[[Category:Articles using Template:Episode table with invalid colour combination]]' 
	end
	
	-- Main row
	local mainRow = root:tag('tr')
	mainRow
		:css('color', background and (black_cr > white_cr and 'black' or 'white') or 'black')
		:css('text-align', 'center')
	
	-- Cells
	do
		local viewers = (args.country ~= '' and args.country or '') .. ' ' .. (args.country ~= '' and 'v' or 'V') .. 'iewers' ..
			((not args.viewers_type or args.viewers_type ~= '') and '<br />(' .. (args.viewers_type or 'millions') .. ')' or '')
		
		local cellNames = {
			{'overall','EpisodeNumber',EpisodeTable.abbr('No.','Number') ..
				((args.season or args.series or args.EpisodeNumber2 or args.EpisodeNumber2Series) and '<br />overall' or '')},
			{'season','EpisodeNumber2',EpisodeTable.abbr('No.','Number') .. ' in<br />season'},
			{'series','EpisodeNumber2Series',EpisodeTable.abbr('No.','Number') .. ' in<br />series'},
			{'title','Title','Title'},
			{'aux1','Aux1',''},
			{'director','DirectedBy','Directed by'},
			{'writer','WrittenBy','Written by'},
			{'aux2','Aux2',''},
			{'aux3','Aux3',''},
			{'airdate','OriginalAirDate','Original ' .. (args.released and 'release' or 'air') .. ' date'},
			{'altdate','AltDate',''},
			{'prodcode','ProdCode',EpisodeTable.abbr('Prod.','Production') .. '<br />code'},
			{'viewers','Viewers',viewers},
			{'aux4','Aux4',''}
		}
	
		for k,v in pairs(cellNames) do
			local thisCell = args[v[1]] or args[v[2]]
			if thisCell then
				local thisCellT = args[v[1] .. 'T'] or args[v[2] .. 'T']
				local thisCellR = args[v[1] .. 'R'] or args[v[2] .. 'R']
				mainRow:node(EpisodeTable.cell(background, thisCell, thisCellT or v[3], thisCellR))
			end
		end
	
		-- Episodes
		root:node(args.episodes)
	end
	
	return tostring(root) .. categories
end

--------------------------------------------------------------------------------
-- Exports
--------------------------------------------------------------------------------

local p = {}

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		removeBlanks = false,
		wrappers = 'Template:Episode table'
	})
	return EpisodeTable.new(args)
end

return p