Ugrás a tartalomhoz

Modul:Episode table

Ellenőrzött
A Wikipédiából, a szabad enciklopédiából

Episode table[mi ez?] • [dokumentáció: mutat, szerkeszt] • [tesztek: létrehozás]

-- This module implements {{Epizód táblázat}} and {{Epizód táblázat/rész}}.

local HTMLcolor = mw.loadData( 'Modul:Color contrast/colors' )

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

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

function EpisodeTable.cell(background, width, text, reference, textColor)
	local cell = mw.html.create('th')
	
	-- Width
	local cell_width
	if width == 'auto' then
		cell_width = 'auto'
	elseif tonumber(width) ~= nil then
		cell_width = width .. '%'
	else
		cell_width = nil
	end
	
	-- Cell
	cell:attr('scope','col')
		:css('background',background or '#CCCCFF')
		:css('width',cell_width)
		:css('color',textColor)
		:wikitext(text)
	
	-- Reference
	if reference and reference ~= '' then
		cell:wikitext(" " .. EpisodeTable.reference(reference, background))
	end
	
	return cell
end

function EpisodeTable.reference(reference, background)
	local link1_cr = contrast_ratio{'#0645AD', background or '#CCCCFF', ['error'] = 0}
	local link2_cr = contrast_ratio{'#0B0080', background or '#CCCCFF', ['error'] = 0}
	
	local refspan = mw.html.create('span')
		:wikitext(reference)
	
	if link1_cr < 7 or link2_cr < 7 then
		refspan
			:css('color','black')
			:css('background-color','white')
			:css('padding','1px')
			:css('display','inline-block')
			:css('line-height','50%')
	end
	
	return tostring(refspan)
end

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

function EpisodeTable.part(frame,args)
	local row = mw.html.create('tr')
	local cArgs = args.sz or args.c
	
	if (cArgs == nil or cArgs == '') then cArgs = '#CCCCFF' end
	
	local black_cr = contrast_ratio{cArgs or '#CCCCFF', 'black', ['error'] = 0}
	local white_cr = contrast_ratio{'white', cArgs or '#CCCCFF', ['error'] = 0}
	
	local partTypes = {
		{'act','Act'},
		{'chapter','fejezet'},
		{'part','rész'},
		{'volume','felvonás'},
		{'week','hét'},
	}
	
	local displaytext = ''
	local isAnyPartSet = false
	
	for k,v in pairs(partTypes) do
		if args[v[1]] or args[v[2]] then
			isAnyPartSet = true
			displaytext = (args[v[2]] or args[v[1]]) .. '. ' .. v[2]
		end
	end
	local subtitleArgs = args["alcím"] or args.subtitle
	if subtitleArgs then
		displaytext = displaytext .. ((isAnyPartSet and ': ' or '') .. subtitleArgs)
	end

	local plainText = require('Modul:Plain text')._main
	local displayTextAnchor = plainText(displaytext)
	local rArgs = args.f or args.r
	
	row:tag('td')
		:attr('colspan', 13)
		:attr('id', args.id or displayTextAnchor)
		:css('text-align', 'center')
		:css('background-color', cArgs or '#CCCCFF')
		:css('color', black_cr > white_cr and 'black' or 'white')
		:wikitext("'''" .. displaytext .. "'''" .. (rArgs and "&#8202;" .. EpisodeTable.reference(rArgs, cArgs) or ''))
	
	return tostring(row)
end

function EpisodeTable.new(frame,args)
	args = args or {}
	local categories = ''
	local backgroundArgs = args['háttér'] or args.background
	local background = (backgroundArgs and backgroundArgs ~= '' and backgroundArgs ~= '#') and backgroundArgs or nil
	
	-- Add # to background if necessary
	if background ~= nil and HTMLcolor[background] == nil then
		background = '#'..(mw.ustring.match(background, '^[%s#]*([a-fA-F0-9]*)[%s]*$') or '')
	end
	
	-- Default widths noted by local consensus
	local defaultwidths = {};
	defaultwidths.overall = 5;
	defaultwidths.overall2 = 5;
	defaultwidths.season = 5;
	defaultwidths.series = 5;
	defaultwidths.prodcode = 7;
	defaultwidths.viewers = 10;
	
	-- Create episode table
	local root = mw.html.create('table')
	
	-- Table width
	local total_widthArgs = args['teljes szélesség'] or args.total_width
	local table_width = string.gsub(total_widthArgs or '','%%','')
	if total_widthArgs == 'auto' or total_widthArgs == '' then
		table_width = 'auto'
	elseif tonumber(table_width) ~= nil then
		table_width = table_width .. '%'
	else
		table_width = '100%'
	end
	
	root
		:addClass('wikitable')
		:addClass('plainrowheaders')
		:addClass('wikiepisodetable')
		:css('width', table_width)
	
	-- Caption
	local argsCaption = args.felirat or args.caption
	if args.show_caption then
		-- Visible caption option, with a tracking category
		root:tag('caption'):wikitext(argsCaption)
		categories = categories .. '[[Category:Articles using Sablon:Episode table with a visible caption]]'
	elseif argsCaption then
		-- If a visible caption isn't defined, then default to the screenreader-only caption
		root:tag('caption'):wikitext(frame:expandTemplate{title='Screen reader-only',args={argsCaption}})
	end
	
	-- Colour contrast; add to category only if it's in the mainspace
	local title = mw.title.getCurrentTitle()
	local black_cr = contrast_ratio{background, 'black', ['error'] = 0}
	local white_cr = contrast_ratio{'white', background, ['error'] = 0}
	
	if title.namespace == 0 and (backgroundArgs and backgroundArgs ~= '' and backgroundArgs ~= '#') and black_cr < 7 and white_cr < 7 then
		categories = categories .. '[[Category:Articles using Sablon:Episode table with invalid colour combination]]' 
	end
	
	-- Main row
	local textColor = background and (black_cr > white_cr and 'black' or 'white') or 'black'
	local mainRow = root:tag('tr')
	mainRow
		:css('color', textColor)
		:css('text-align', 'center')
	
	-- Cells
	do
		local used_season = false
		local countryArgs = args['ország'] or args.country
		local country = countryArgs ~= '' and countryArgs ~= nil
		local viewers = (country and countryArgs or '') .. ' ' .. (country and 'n' or 'N') .. 'ézettség' ..
			((not args.viewers_type or args.viewers_type ~= '') and '<br />(' .. (args.viewers_type or 'millió') .. ')' or '')
		
		local cellNames = {
			{'összesen','overall','EpisodeNumber',EpisodeTable.abbr('Sor.','Epizód sorszáma a sorozatban')},
			{'összesen2','overall2','*',''},
			{'évad','season','EpisodeNumber2',EpisodeTable.abbr('Év.','Epizód sorszáma az évadban')},
			{'sorozat','series','EpisodeNumber2Series',EpisodeTable.abbr('Év.','Epizód sorszáma az évadban')},
			{'cím','title','Title','Cím'},
			{'röv1','aux1','Aux1',''},
			{'rendező','director','DirectedBy','Rendező'},
			{'író','writer','WrittenBy','Író'},
			{'röv2','aux2','Aux2',''},
			{'röv3','aux3','Aux3',''},
			{'sugárzás','airdate','OriginalAirDate','Premier'},
			{'','altdate','AltDate',''},
			{'vendégek','guests','Guests','Vendég(ek)'},
			{'','musicalguests','MusicalGuests','Musical/entertainment guest(s)'},
			{'gyártási szám','prodcode','ProdCode','Gyártási<br />szám'},
			{'nézettség','viewers','Viewers',"Nézettség"},
			{'röv4','aux4','Aux4',''}
		}
	
		for k,v in pairs(cellNames) do
			local thisCell = args[v[1]] or args[v[2]]
			if thisCell and (v[1] ~= 'series' or (v[1] == 'series' and used_season == false)) then
				if v[1] == 'season' then used_season = true end
				if (k <= 3 and thisCell == '') then thisCell = '5' end
				if (thisCell == '' and defaultwidths[v[1]]) then thisCell = defaultwidths[v[1]] end
				
				local thisCellT = args[v[1] .. 'C'] or args[v[2] .. 'C'] or args[v[1] .. 'T'] or args[v[2] .. 'T']
				local thisCellR = args[v[1] .. 'F'] or args[v[2] .. 'F'] or args[v[1] .. 'R'] or args[v[2] .. 'R']
				mainRow:node(EpisodeTable.cell(background, thisCell, thisCellT or v[4], thisCellR, textColor))
			end
		end
	
		-- Episodes
		local episodesArgs = args['epizódok'] or args.episodes
		if episodesArgs then
			if args.anchor then 
				episodesArgs = string.gsub(episodesArgs, "(id=\")(ep%w+\")", "%1" .. args.anchor .. "%2")
			end
			
			root:node(episodesArgs)
		end
	end
	


    local templateStyles = mw.getCurrentFrame():extensionTag{
		name = 'templatestyles', args = { src = 'Modul:Episode table/styles.css' }
	}
	return (((args.dontclose or '') ~= '') and mw.ustring.gsub(tostring(root), "</table>", "") or tostring(root)) .. categories .. templateStyles
end

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

local p = {}

function p.main(frame)
	local args = require('Modul:Arguments').getArgs(frame, {
		removeBlanks = false,
		wrappers = 'Sablon:Epizód táblázat'
	})
	local check = require('Modul:Check for unknown parameters')._check
	local tracking = ''
	local title = mw.title.getCurrentTitle()
	if title.namespace == 0 then
		tracking = check({
			['unknown']='[[Category:Pages using episode table with unknown parameters|_VALUE_'..title.text..']]',
			['preview']='Page using [[Sablon:Episode table]] with unknown parameter "_VALUE_"',
			['showblankpositional']='y',
			'airdate', 'airdateR', 'airdateT', 'altdate', 'AltDate', 'altdateR', 'AltDateR',
			'altdateT', 'AltDateT', 'anchor', 'aux1', 'Aux1', 'aux1R', 'Aux1R', 'aux1T',
			'Aux1T', 'aux2', 'Aux2', 'aux2R', 'Aux2R', 'aux2T', 'Aux2T', 'aux3', 'Aux3',
			'aux3R', 'Aux3R', 'aux3T', 'Aux3T', 'aux4', 'Aux4', 'aux4R', 'Aux4R', 'aux4T',
			'Aux4T', 'b', 'background', 'c', 'caption', 'ország', 'country', 'DirectedBy', 'DirectedByR',
			'DirectedByT', 'director', 'directorR', 'directorT', 'dontclose', 'EpisodeNumber',
			'EpisodeNumber2', 'EpisodeNumber2R', 'EpisodeNumber2Series', 'EpisodeNumber2SeriesR',
			'EpisodeNumber2SeriesT', 'EpisodeNumber2T', 'EpisodeNumberR', 'EpisodeNumberT',
			'epizódok', 'episodes', 'forceoverall', 'guests', 'Guests', 'guestsR', 'GuestsR', 'guestsT',
			'GuestsT', 'id', 'musicalguests', 'MusicalGuests', 'musicalguestsR', 'MusicalGuestsR',
			'musicalguestsT', 'MusicalGuestsT', 'OriginalAirDate', 'OriginalAirDateR',
			'OriginalAirDateT', 'overall', 'overall_type', 'overall2', 'overall2R',
			'overall2T', 'overallR', 'overallT', 'prodcode', 'ProdCode', 'prodcodeR',
			'ProdCodeR', 'prodcodeT', 'ProdCodeT', 'r', 'season', 'season_type', 'seasonR',
			'seasonT', 'series', 'series_type', 'seriesR', 'seriesT', 'show_caption',
			'subtitle', 'cím', 'title', 'Title', 'titleR', 'TitleR', 'titleT', 'TitleT', 'total_width',
			'nézettség', 'viewers', 'Viewers', 'viewers_type', 'viewersR', 'ViewersR', 'viewersT',
			'ViewersT', 'writer', 'writerR', 'writerT', 'WrittenBy', 'WrittenByR', 'WrittenByT'
		}, args)
	end
	return EpisodeTable.new(frame,args) .. tracking
end

function p.part(frame)
	local args = require('Modul:Arguments').getArgs(frame, {
		removeBlanks = false,
		wrappers = 'Sablon:Epizód táblázat/rész'
	})
	return EpisodeTable.part(frame,args)
end

function p.ref(frame)
	local args = require('Modul:Arguments').getArgs(frame, {
		removeBlanks = false,
	})
	return EpisodeTable.reference(rArgs,args.b)
end

return p