Module:Episode list/sandbox explained

local p =

-- This module requires the use of the following modules:local colorContrastModule = require('Module:Color contrast')local htmlColor = mw.loadData('Module:Color contrast/colors')local langModule = require("Module:Lang")local mathModule = require('Module:Math')local tableEmptyCellModule = require('Module:Table empty cell')local yesNoModule = require('Module:Yesno')

-- mw.html object for the generated row.local row

-- Variable that will decide the colspan= of the Short Summary cell.local nonNilParams = 0

-- Variable that will keep track if a TBA value was entered.local cellValueTBA = false

-- Variable that handles the assigned tracking categories.local trackingCategories = ""

-- List of tracking categories.local trackingCategoryList =

-- List of parameter names in this order.local cellNameList =

-- List of pairs which cannot be used togetherlocal excludeList =

-- List of cells that have parameter groupslocal parameterGroupCells = local firstParameterGroupCelllocal parameterGroupCellsAny = false

-- List of title parameter names in this order.-- List used for multi title lists.local titleList =

-- Local function which is used to retrieve the episode number or production code number,-- without any additional text.local function idTrim(val, search) local valFind = string.find(val, search)

if (valFind

nil) then return val else return string.sub(val, 0, valFind-1) endend

-- Local function which is used to validate that a parameter has an actual value.local function hasValue(param) if (param ~= nil and param ~= "") then return true else return false endend

-- Local function which is used to create a table data cell.local function createTableData(text, rowSpan, textAlign) if (rowSpan ~= nil and tonumber(rowSpan) > 1) then row:tag('td') :attr('rowspan', rowSpan) :wikitext(text) else row:tag('td') :css('text-align', textAlign) :wikitext(text) endend

-- Local function which is used to add a tracking category to the page.local function addTrackingCategory(category) trackingCategories = trackingCategories .. categoryend

-- Local function which is used to create a Short Summary row.local function createShortSummaryRow(args, lineColor) -- fix for lists in the Short Summary local shortSummaryText = args.ShortSummary

if (shortSummaryText:match('^[*:;#]') or shortSummaryText:match('^

scope="row" if (nonNilParams

0) then if (isSerial) then row:tag('th') :addClass('summary') :attr('scope', 'row') :attr('rowspan', numberOfParameterGroups) :css('text-align', textAlign) :wikitext(titleText) else row:tag('th') :addClass('summary') :attr('scope', 'row') :css('text-align', textAlign) :wikitext(titleText) end else if (isSerial) then row:tag('td') :addClass('summary') :attr('rowspan', numberOfParameterGroups) :css('text-align', textAlign) :wikitext(titleText) else row:tag('td') :addClass('summary') :css('text-align', textAlign) :wikitext(titleText) end end

nonNilParams = nonNilParams + 1end

----local function createCells(args, isSerial, currentRow, onInitialPage, title, numberOfParameterGroups) for k, v in ipairs(cellNameList) do if (v

'ProdCode') then if (currentRow

1 or (currentRow > 1 and parameterGroupCells[v])) then createProductionCodeCell(args, v, numberOfParameterGroups) end elseif (v

'Title') then if (currentRow

1 or (currentRow > 1 and parameterGroupCells[v])) then local isSerial = not args.Title_2 and true or false createTitleCell(args, numberOfParameterGroups, currentRow, isSerial) end elseif excludeList[v] and args[excludeList[v]] then -- Ignore this parameter set as multiple conflicting parameters were used elseif (args[v] and (v ~= 'EpisodeNumber' and v ~= 'EpisodeNumber2')) then -- Set empty cells to TBA/TBD if (args[v]

) then createEmptyCell(args, v, false) elseif (v

'WrittenBy' and title.namespace

0) then if ((string.find(args[v], "Story") ~= nil or string.find(args[v], "Teleplay") ~= nil) and string.find(args[v], "8202")

nil) then --   is the hairspace added through addTrackingCategory(trackingCategoryList["raw_unformatted_storyteleplay"]) end end

-- If serial titles need to be centered and not left, then this should be removed. local textAlign = "center" if (v

'Aux1' and isSerial) then textAlign = "left" end

local thisRowspan if (not parameterGroupCells[v] and parameterGroupCellsAny) then thisRowspan = numberOfParameterGroups else thisRowspan = 1 end

if (currentRow

1 or (currentRow > 1 and parameterGroupCells[v])) then createTableData(args[v], thisRowspan, textAlign) end nonNilParams = nonNilParams + 1 checkUsageOfDateTemplates(args, v, onInitialPage, title) end

if (args[v]

"TBA") then cellValueTBA = true end endend

-- Local function which is used to create a table row header for either the-- EpisodeNumber or EpisodeNumber2 column cells.local function createTableRowEpisodeNumberHeader(episodeNumber, numberOfParameterGroups, episodeText, separateEpisodeNumbers) local epID = string.match(episodeNumber, "^%w+") row:tag('th') :attr('scope', 'row') :attr('rowspan', not separateEpisodeNumbers and numberOfParameterGroups or 1) :attr('id', epID and 'ep' .. epID or ) :css('text-align', 'center') :wikitext(episodeText)end

----local function getEpisodeText(episodeNumber) if (episodeNumber

) then return tableEmptyCellModule._main else

local episodeNumber1 local episodeNumber2

-- Used for double episodes that need a visual "–"" or "


"" added. local divider

episodeNumber = episodeNumber:gsub('%s*
%s*', '


')

if (episodeNumber:match('^(%w+)%s*


%s*(%w+)$')) then episodeNumber1, episodeNumber2 = episodeNumber:match('^(%w+)%s*
%s*(%w+)$') divider = "
" elseif (episodeNumber:match('^(%w+)%s*
.-
%s*(%w+)$')) then -- 3 or more elements episodeNumber1, episodeNumber2 = episodeNumber:match('^(%w+)%s*
.-
%s*(%w+)$') divider = "
" elseif (mw.ustring.match(episodeNumber, '^(%w+)%s*[%s%-–/&]%s*(%w+)$')) then episodeNumber1, episodeNumber2 = mw.ustring.match(episodeNumber, '^(%w+)%s*[%s%-–/&]%s*(%w+)$') divider = "–" else episodeNumber1, episodeNumber2 = mw.ustring.match(episodeNumber, '^(%w+)%s*[%s%-–/&].-[%s%-–/&]%s*(%w+)$') -- 3 or more elements divider = "–" end

if (not episodeNumber1) then return episodeNumber elseif (not episodeNumber2) then return string.match(episodeNumber, '%w+') else return episodeNumber1 .. divider .. episodeNumber2 end endend

-- Local function which is used to create EpisodeNumber2 and EpisodeNumber3 cells.local function _createEpisodeNumberCellSecondary(episodeValue, numberOfParameterGroups, separateEpisodeNumbers) if (episodeValue) then local episodeText = getEpisodeText(episodeValue)

if (nonNilParams

0) then createTableRowEpisodeNumberHeader(episodeValue, numberOfParameterGroups, episodeText, separateEpisodeNumbers) else createTableData(episodeText, not separateEpisodeNumbers and numberOfParameterGroups or 1, "center") end

nonNilParams = nonNilParams + 1

endend

-- Local function which is used to create seconday episode number cells.local function createEpisodeNumberCellSecondary(args, numberOfParameterGroups, separateEpisodeNumbers) _createEpisodeNumberCellSecondary(args.EpisodeNumber2, numberOfParameterGroups, separateEpisodeNumbers) _createEpisodeNumberCellSecondary(args.EpisodeNumber3, numberOfParameterGroups, separateEpisodeNumbers)end

-- Local function which is used to create an EpisodeNumber cell.local function createEpisodeNumberCell(args, numberOfParameterGroups, separateEpisodeNumbers) if (args.EpisodeNumber) then local episodeText = getEpisodeText(args.EpisodeNumber) createTableRowEpisodeNumberHeader(args.EpisodeNumber, numberOfParameterGroups, episodeText, separateEpisodeNumbers) nonNilParams = nonNilParams + 1 endend

-- Local function which is used to create a single row of cells.-- This is the standard function called.local function createSingleRowCells(args, numberOfParameterGroups, multiTitleListEnabled, onInitialPage, title) createEpisodeNumberCell(args, 1, false) createEpisodeNumberCellSecondary(args, 1, false) createCells(args, false, 1, onInitialPage, title, numberOfParameterGroups)end

-- Local function which is used to create a multiple row of cells.-- This function is called when part of the row is rowspaned.local function createMultiRowCells(args, numberOfParameterGroups, onInitialPage, title, topColor) local EpisodeNumberSplit = (args.EpisodeNumber_1 and true or false) for i = 1, numberOfParameterGroups do args = extractDataFromNumberedSerialArgs(args, i, numberOfParameterGroups, title) if (EpisodeNumberSplit or (not EpisodeNumberSplit and i

1)) then createEpisodeNumberCell(args, numberOfParameterGroups, EpisodeNumberSplit) createEpisodeNumberCellSecondary(args, numberOfParameterGroups, EpisodeNumberSplit) end createCells(args, true, i, onInitialPage, title, numberOfParameterGroups) if (i ~= numberOfParameterGroups) then row = row:done -- Use done to close the 'tr' tag in rowspaned rows. :tag('tr') :css('background', topColor) end endend

-- Local function which is used to retrieve the NumParts value.local function getnumberOfParameterGroups(args) for k, v in ipairs(cellNameList) do local numberedParameter = v .. "_" .. 1 if (args[numberedParameter]) then parameterGroupCells[v] = true parameterGroupCellsAny = true if not firstParameterGroupCell then firstParameterGroupCell = k end end end

if (hasValue(args.NumParts)) then return args.NumParts, true else return 1, false endend

-- Local function which is used to retrieve the Top Color value.local function getTopColor(args, rowColorEnabled, onInitialPage) local episodeNumber = mathModule._cleanNumber(args.EpisodeNumber) or 1 if (args.TopColor) then if (string.find(args.TopColor, "#")) then return args.TopColor else return '#' .. args.TopColor end else return 'inherit' endend

-- Local function which is used to retrieve the Row Color value.local function isRowColorEnabled(args) local rowColorEnabled = yesNoModule(args.RowColor, false)

if (args.RowColor and string.lower(args.RowColor)

'on') then rowColorEnabled = true end

return rowColorEnabledend

-- Local function which is used to retrieve the Line Color value.local function getLineColor(args) -- Default color to light blue local lineColor = args.LineColor or 'CCCCFF'

-- Add # to color if necessary, and set to default color if invalid if (htmlColor[lineColor]

nil) then lineColor = '#' .. (mw.ustring.match(lineColor, '^[%s#]*([a-fA-F0-9]*)[%s]*$') or ) if (lineColor

'#') then lineColor = '#CCCCFF' end end

return lineColorend

-- Local function which is used to check if the table is located on the page-- currently viewed, or on a transcluded page instead.-- If it is on a transcluded page, the episode summary should not be shown.local function isOnInitialPage(args, sublist, pageTitle, initiallistTitle) -- This should be the only check needed, however, it was previously implemented with two templates -- with one of them not requiring an article name, so for backward compatability, the whole sequence is kept. local onInitialPage

-- Only sublist had anything about hiding, so only it needs to even check if (sublist) then onInitialPage = mw.uri.anchorEncode(pageTitle)

mw.uri.anchorEncode(initiallistTitle) -- avoid processing ghost references if (not onInitialPage) then args.ShortSummary = nil end else if (initiallistTitle

"") then onInitialPage = true else onInitialPage = mw.uri.anchorEncode(pageTitle)

mw.uri.anchorEncode(initiallistTitle) end end

return onInitialPageend

-- Local function which does the actual main process.local function _main(args, sublist) local title = mw.title.getCurrentTitle local pageTitle = title.text local initiallistTitle = args['1'] or

-- Is this list on the same page as the page directly calling the template? local onInitialPage = isOnInitialPage(args, sublist, pageTitle, initiallistTitle)

-- Need just this parameter removed if blank, no others if (hasValue(args.ShortSummary)

false) then args.ShortSummary = nil end

local lineColor = getLineColor(args) local rowColorEnabled = isRowColorEnabled(args) local topColor = getTopColor(args, rowColorEnabled, onInitialPage)

local root = mw.html.create -- Create the root mw.html object to return local textColor = '#333' if topColor

'inherit' then textColor = 'inherit' end

row = root:tag('tr') -- Create the table row and store it globally :addClass('vevent') :addClass('module-episode-list-row') :css('text-align', 'center') :css('background', topColor) :css('color', textColor)

local numberOfParameterGroups, multiTitleListEnabled = getnumberOfParameterGroups(args)

if (multiTitleListEnabled) then createMultiRowCells(args, numberOfParameterGroups, onInitialPage, title, topColor) else createSingleRowCells(args, numberOfParameterGroups, multiTitleListEnabled, onInitialPage, title) end

-- add these categories only in the mainspace and only if they are on the page where the template is used if (onInitialPage and title.namespace

0) then addLineColorTrackingCategories(args) addTopColorTrackingCategories(args) end

if (cellValueTBA

true and title.namespace

0) then addTrackingCategory(trackingCategoryList["tba_values"]) end

-- Do not show the summary if this is being transcluded on the initial list page -- Do include it on all other lists if (onInitialPage and args.ShortSummary) then local bottomWrapper = createShortSummaryRow(args, lineColor) return tostring(root) .. tostring(bottomWrapper) .. trackingCategories else return tostring(root) .. trackingCategories endend

-- Local function which handles both module entry points.local function main(frame, sublist) local getArgs = require('Module:Arguments').getArgs local args

-- Most parameters should still display when blank, so don't remove blanks if (sublist) then args = getArgs(frame,) else args = getArgs(frame,) end

-- args['1'] = mw.getCurrentFrame:getParent:getTitle return _main(args, sublist, frame)end

--1= — required; The title of the article where the Episode Table is located at. -- |EpisodeNumber= — suggested; The overall episode number in the series. -- |EpisodeNumber2= — suggested; The episode number in the season. -- |Title= — suggested; The English title of the episode. -- |RTitle= — optional; Unformatted parameter that can be used to add a reference after "Title", or can be used as a "raw title" to replace "Title" completely. -- |AltTitle= — optional; An alternative title, such as the title of a foreign show's episode in its native language, or a title that was originally changed. -- |TranslitTitle= — optional; The title of the episode transliteration (Romanization) to Latin characters. -- |RAltTitle= — optional; Unformatted parameter that can be used to add a reference after "AltTitle", or can be used as a "raw title" to replace "AltTitle" completely. -- |NativeTitle= — optional; The title of the episode in the native language. -- |NativeTitleLangCode — optional; The language code of the native title language. -- |Aux1= — optional; General purpose parameter. The meaning is specified by the column header. This parameter is also used for Serial episode titles, such as those used in Doctor Who. -- |DirectedBy= — optional; Name of the episode's director. May contain links. -- |WrittenBy= — optional; Primary writer(s) of the episode. May include links. -- |Aux2= — optional; General purpose parameter. The meaning is specified by the column header. -- |Aux3= — optional; General purpose parameter. The meaning is specified by the column header. -- |OriginalAirDate= — optional; This is the date the episode first aired on TV, or is scheduled to air. -- |AltDate= — optional; The next notable air date, such as the first air date of an anime in English. -- |Guests= — optional; List of Guests for talk shows. Cannot be used simultaneously with Aux1. -- |MusicalGuests= — optional; List of MusicalGuests for talk shows. Cannot be used simultaneously with Aux2. -- |ProdCode= — optional; The production code in the series. When defined, this parameter also creates a link anchor, prefixed by "pc"; for example, List of episodes#pc01. -- |Viewers= — optional; Number of viewers who watched the episode. Should include a reference. -- |Aux4= — optional; General purpose parameter. The meaning is specified by the column header. -- |ShortSummary= — optional; A short 100–200 word plot summary of the episode. -- |LineColor= — optional; Colors the separator line between episode entries. If not defined the color defaults to "#CCCCFF" and the article is placed in Category:Episode list using the default LineColor. Use of "#", or anything but a valid hex code will result in an invalid syntax. -- |TopColor= — discouraged; Colors the main row of information (that is, not the ShortSummary row). Articles using this parameter are placed in Category:Episode lists with row deviations. -- |RowColor= — optional; Switch parameter that must only be defined when the EpisodeNumber= entry is not a regular number (e.g. "12–13" for two episodes described in one table entry). If the first episode number is even, define pass "on". If the first episode number is odd, pass "off".--function p.sublist(frame) return main(frame, true)end

--Public function which is used to create an Episode rowfor an Episode Table used for lists of episodes where all tables are on the same page.

For tables which are on different pages see p.sublist.

For complete parameter documentation, see the documentation at p.sublist.--

function p.list(frame) return main(frame, false)end

return p