Jump to content

Module:Series overview/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Alex 21 (talk | contribs) at 08:57, 14 July 2016 (Create sandbox version of Module:Series overview). 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 {{Series overview}}.

require('Module:No globals')
local yesno = require('Module:Yesno')

--------------------------------------------------------------------------------
-- SeriesOverview class
-- The main class.
--------------------------------------------------------------------------------

local SeriesOverview = {}

function SeriesOverview.new(frame, args)
	args = args or {}
	local allReleased = yesno(args.allreleased)

	-- Create series overview table
	local root = mw.html.create('table')
	local cellPadding = '0 8px'

	root
		:addClass('wikitable')
		:addClass('plainrowheaders')
		:css('text-align', 'center')

	-- Caption
	if args.caption then
		root:tag('caption'):wikitext(args.caption)
	end
	
	-- Sorting function
	local function series_sort(op1, op2)
	    local n1,s1 = string.match(op1,"(%d+)(%a*)");
	    local n2,s2 = string.match(op2,"(%d+)(%a*)");
	    local n1N,n2N = tonumber(n1),tonumber(n2)
	
	    if n1N == n2N then
	        return s1 < s2
	    else
	        return n1N < n2N
	    end
	end
	
	-- Allow usages of {{N/A}} cells
	local function series_attributes(infoParam)
		local entries = {}
		local infoCell = mw.html.create('td')
		
		while true do
			local a,b = string.match(infoParam,'(%a*)="([^"]*)"');
			if a == nil or b == nil then break end
			infoCell:attr(a,b)
			infoParam = string.gsub(infoParam,'(%a*)="([^"]*)"','',1)
		end
	
		infoParam = string.gsub(infoParam,'%s|%s','',1)
		infoCell:wikitext(infoParam)
		
		return infoCell
	end

	-- Headers
	do
		local headerRow = root:tag('tr')
		headerRow
			:css('text-align', 'center')

		local setNetwork = args.network0S or args.network1
		local releasedBlurb = args.released and 'released' or 'aired'
		
		-- Colspan calculation for information cells (0 = no info set)
		local numInfoCells = 0;
		for i = string.byte('A'), string.byte('Z') do
			local param = 'info' .. string.char(i)
			if args[param] then numInfoCells = numInfoCells + 1 end
		end
		
		-- Season header
		headerRow:tag('th')
			:attr('scope', 'col')
			:attr('rowspan', allReleased and numInfoCells == 0 and 1 or 2)
			:attr('colspan', 2)
			:css('padding', cellPadding)
			:wikitext(args.uk and 'Series' or 'Season')

		-- Episodes header
		headerRow:tag('th')
			:attr('scope', 'col')
			:attr('rowspan', allReleased and numInfoCells == 0 and 1 or 2)
			:attr('colspan', 2)
			:css('padding', cellPadding)
			:wikitext('Episodes')

		-- Originally aired header
		headerRow:tag('th')
			:attr('scope', 'col')
			:attr('rowspan', allReleased and numInfoCells > 0 and 2 or 1)
			:attr('colspan', setNetwork and 3 or 2)
			:wikitext('Originally ' .. releasedBlurb)
		
		-- Network subheader for released series
		if setNetwork and allReleased then
			headerRow:tag('th')
				:attr('scope', 'col')
				:attr('rowspan', allReleased and numInfoCells == 0 and 1 or 2)
				:css('padding', cellPadding)
				:wikitext('Network')
		end
		
		-- Subheader row
		local subheaderRow = mw.html.create('tr')

		-- Info header
		if args.infoheader then
			headerRow:tag('th')
				:attr('scope', 'col')
				:attr('colspan', numInfoCells > 0 and numInfoCells or nil)
				:attr('rowspan', (allReleased and 1) or (numInfoCells > 0 and 1) or 2)
				:css('padding', cellPadding)
				:wikitext(args.infoheader)
		end

		if not allReleased then
			-- First aired subheader
			subheaderRow:tag('th')
				:attr('scope', 'col')
				:wikitext('First ' .. releasedBlurb)

			-- Last aired subheader
			subheaderRow:tag('th')
				:attr('scope', 'col')
				:wikitext('Last ' .. releasedBlurb)
			
			-- Network subheader for aired series
			if setNetwork then
				subheaderRow:tag('th')
					:attr('scope', 'col')
					:css('padding', cellPadding)
					:wikitext('Network')
			end
		end

		-- Information subheaders
		for i = string.byte('A'), string.byte('Z') do
			local param = 'info' .. string.char(i)
			if args[param] then
				subheaderRow:tag('th')
					:attr('scope', 'col')
					:css('padding', cellPadding)
					:wikitext(args[param])
			end
		end
	
		-- Check for scenarios with an empty subheaderRow
		if not allReleased or numInfoCells > 0 then
			root:node(subheaderRow);
		end
	end

	-- Season rows
	do
		local SeasonEntries = {};
	
		-- Extract info and place into a 3D array
		for k,v in pairs(args) do
			local str, num, str2 = string.match(k, '([^%d]*)(%d*)(%a*)')
			if num ~= '' then 
				-- Special
				local special = false
				if string.sub(str2,1,1) == 'S' then
					special = true
					num = num .. str2
					str2 = ''
				end
				-- Add to entries, create if necessary
				if not SeasonEntries[num] then
					SeasonEntries[num] = {}
				end
				SeasonEntries[num][str .. str2] = v
				if special then
					SeasonEntries[num]['special'] = 'y'
				end
			end
		end
	
		-- Order table by season number
		local ordered_keys = {}
		for k in pairs(SeasonEntries) do
			table.insert(ordered_keys, k)
		end
		table.sort(ordered_keys,series_sort)
	
		for X = 1, #ordered_keys do
			local season, entry = ordered_keys[X], SeasonEntries[ordered_keys[X]]
			local splitSeason = entry.startA
			
			-- New season row
			local seasonRow = root:tag('tr');
			
			-- Dates
			local startDate = entry['start']
			local endDate = entry['end']
			
			-- First season row
			
			-- Coloured cell
			seasonRow:tag('td')
				:attr('scope','row')
				:attr('rowspan', ((splitSeason and entry.color) or (entry.colorA and entry.colorA == entry.colorB)) and 2 or nil)
				:css('background',entry.colorA or entry.color)
				:css('width','10px')
			
			-- Season number link
			seasonRow:tag('td')
				:attr('rowspan', splitSeason and 2 or nil)
				:attr('colspan', entry.special and not entry.episodes and 3 or 1)
				:wikitext(entry.link and '[[' .. entry.link .. '|' .. (entry.linkT or season) .. ']]' or (entry.linkT or season))
			
			-- Episodes
			if entry.episodes then
				seasonRow:tag('td')
					:attr('colspan', splitSeason and 1 or 2)
					:attr('rowspan', splitSeason and 2 or nil)
					:wikitext(entry.episodes)
			elseif not entry.special then
				local infoCell = series_attributes( frame:expandTemplate{title='N/A',args={'TBA'}} )
				infoCell
					:attr('colspan', splitSeason and 1 or 2)
					:attr('rowspan', splitSeason and 2 or nil)
				seasonRow:node(infoCell);
			end
			
			-- Episodes for first half of split season
			if splitSeason then
				if entry.episodesA then
					seasonRow:tag('td')
						:wikitext(entry.episodesA or frame:expandTemplate{title='TableTBA'})
				else
					local infoCell = series_attributes( frame:expandTemplate{title='N/A',args={'TBA'}} )
					seasonRow:node(infoCell);
				end
			end
			
			-- Start date
			seasonRow:tag('td')
				:attr('colspan', ((not entry.special and endDate == 'start') or (entry.special and not endDate)) and 2 or 1)
				:css('padding',cellPadding)
				:wikitext(entry.startA or startDate)
			
			-- End date
			if not allReleased and endDate ~= 'start' and ((entry.special and endDate) or not entry.special) then
				if entry.endA or endDate then
					seasonRow:tag('td')
						:css('padding',cellPadding)
						:wikitext(entry.endA or endDate)
				else
					local infoCell = series_attributes( frame:expandTemplate{title='N/A',args={'TBA'}} )
					infoCell:css('padding',cellPadding)
					seasonRow:node(infoCell);	
				end
			end
			
			-- Network
			if entry.network or entry.networkA then
				-- Calculate rowspan for network cell
				local networkLength = 1
				for i = X+1, #ordered_keys do
					local entryB = SeasonEntries[ordered_keys[i]]
					-- Split season, then regular season
					if entryB.startA then
						if not entryB.networkA then networkLength = networkLength + 1
						else break end
						if not entryB.networkB then networkLength = networkLength + 1
						else break end
					else
						if not entryB.network then networkLength = networkLength + 1
						else break end
					end
				end
				-- Network cell
				seasonRow:tag('td')
					:attr('rowspan',networkLength)
					:wikitext(entry.networkA or entry.network)
			end
			
			-- Information
			for i = string.byte('A'), string.byte('Z') do
				local param = 'info' .. string.char(i)
				local infoParam = entry[param .. 'A'] or entry[param]
				if infoParam then
					-- Cells with {{N/A|...}} already expanded
					if string.sub(infoParam,1,5) == 'style' then
						local infoCell = series_attributes(infoParam)
						infoCell:attr('rowspan', (splitSeason and entry[param]) and 2 or nil)
						seasonRow:node(infoCell);
					else
						-- Unstyled content info cell
						seasonRow:tag('td')
							:wikitext(infoParam)
							:attr('rowspan', (splitSeason and entry[param]) and 2 or nil)
					end
				end
			end
			
			-- Second season row for split seasons
			if splitSeason then
				-- Second row
				local seasonRowB = root:tag('tr');
				
				-- Coloured cell
				if not entry.color and entry.colorB and entry.colorA ~= entry.colorB then
					seasonRowB:tag('td')
						:attr('scope','row')
						:css('background',entry.colorB)
						:css('width','10px')
				end
				
				-- Episodes for first half of split season
				seasonRowB:tag('td')
					:wikitext(entry.episodesB or frame:expandTemplate{title='TableTBA'})
				
				-- Start date
				seasonRowB:tag('td')
					:attr('colspan',entry.endB == 'start' and 2 or 1)
					:css('padding',cellPadding)
					:wikitext(entry.startB)
				
				-- End date
				if not allReleased and entry.endB ~= 'start' then
					seasonRowB:tag('td')
						:css('padding',cellPadding)
						:wikitext(entry.endB)
				end
				
				-- Network
				if entry.networkB then
					-- Calculate rowspan for network cell
					local networkLength = 1
					for i = X+1, #ordered_keys do
						local entryB = SeasonEntries[ordered_keys[i]]
						-- Split season, then regular season
						if entryB.startA then
							if not entryB.networkA then networkLength = networkLength + 1
							else break end
							if not entryB.networkB then networkLength = networkLength + 1
							else break end
						else
							if not entryB.network then networkLength = networkLength + 1
							else break end
						end
					end
					-- Network cell
					seasonRowB:tag('td')
						:attr('rowspan',networkLength)
						:wikitext(entry.networkB)
				end
				
				-- Information
				for i = string.byte('A'), string.byte('Z') do
					local param = 'info' .. string.char(i)
					if entry[param .. 'B'] then
						seasonRowB:tag('td')
							:wikitext(entry[param .. 'B'])
					end
				end
			end -- End 'if' splitSeason
		end -- End 'for' ordered_keys
	end -- End 'do' season rows

	return tostring(root)
end

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

local p = {}

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:Series overview'
	})
	return SeriesOverview.new(frame, args)
end

return p