Module:Data Table

local p = {} local h = {}

local Franchise = require("Module:Franchise") local Sequences = require("Module:Sequences") local Term = require("Module:Term") local TermList = require("Module:Term List") local utilsArg = require("Module:UtilsArg") local utilsLayout = require("Module:UtilsLayout") local utilsMarkup = require("Module:UtilsMarkup") local utilsTable = require("Module:UtilsTable")

local CATEGORY_INVALID_ARGS = require("Module:Constants/category/invalidArgs")

function p.Main(frame) local args, err = utilsArg.parse(frame:getParent.args, p.Templates["Data Table"]) local categories = err and err.categoryText or "" local columnHeaders, columnTags = h.extractColumnTags(args.columns) local dataRows = {} local currentRow = {} local columnIndex = 1 for i, cell in ipairs(args.cells) do		if cell[1] == "-" and i ~= 1 then table.insert(dataRows, currentRow) currentRow = {} columnIndex = 1 elseif cell[1] ~= "-" then cell = h.formatCell(args.game, columnTags[columnIndex], cell) table.insert(currentRow, cell) columnIndex = columnIndex + 1 end end table.insert(dataRows, currentRow)

local wikitable = utilsLayout.table({		sortable = true,		headers = args.columns or {},		rows = dataRows,	}) return wikitable, categories end

function h.extractColumnTags(columns) local columnHeaders = {} local columnTags = {} for i, column in ipairs(columns) do		columns[i] = string.gsub(column, "%s*%[(%w+)%]$", function(tag)			columnTags[i] = tag			return ""		end) end return columns, columnTags end

function h.formatCell(game, tag, cellData) local frame = mw.getCurrentFrame local cell = {} if tag == "Amount" or tag == "Amounts" then local amounts = utilsTable.map(cellData, function(itemAmount)			local amountStart, amountEnd, amount = string.find(itemAmount, "^(%d+)%s")			local item = string.sub(itemAmount, amountEnd+1)				return frame:expandTemplate({ title = "Item Amount", args = {game, amount, item} })			end) cell.content = h.list(amounts) elseif tag == "Rupees" then if #cellData == 1 then cell.sortValue = cellData[1] end local amounts = utilsTable.map(cellData, function(amount)			return frame:expandTemplate({ title = "Rupee", args = {game, amount} })		end) cell.content = h.list(amounts) elseif tag == "SortValue" then cell.sortValue = Sequences.sortValue(game, nil, cellData[1]) cell.content = Term.link(cellData[1], game) if #cellData > 1 then local utilsError = require("Module:UtilsError") utilsError.warn(string.format("Tag  cannot be used on cells containing multiple values:  ", table.concat(cellData, ", "))) cell.content = cell.content.."" end elseif tag == "Term" or tag == "Terms" then local termLinks = TermList.termList(game, cellData, { link = true }) cell.content = h.list(termLinks) else cell.content = cellData[1] end return cell end

function h.list(items) return #items > 1 and utilsMarkup.list(items) or items[1] end

p.Templates = { ["Data Table"] = { wip = true, format = "block", purpose = " Displays tabular data in a way that is mobile-friendly. Minimizes the amount of boilerplate wikitext that most tables require. Automatically center-aligns columns and applies templates such as, , , etc.  The table data can be stored in Cargo so that the table (or a subset of its rows) can be displayed on other relevant pages.", boilerplate = { separateRequiredParams = false, },		paramOrder = {"storeAs", "game", "columns", "..."}, params = { storeAs = { type = "string", suggested = true, desc = "If present, the data will be stored in the DataTables Cargo table so that it can be retrieved on other pages using .", },			game = { type = "string", required = true, enum = Franchise.enum, desc = "A game code. Used to automatically apply game-based templates (e.g., , ) to table cells.", },			columns = { type = "content", required = true, desc = "Comma-separated list of column headers. One tag can be appended to each column to indicate how the template should handle data in that column. See below.", trim = true, nilIfEmpty= true, split = true, },			["..."] = {				name = "cells", placeholder = "cell", required = true, type = "content", desc = "Cell values. Type  to separate rows, as shown in the examples below.", trim = true, split = true, }		}	} }

return p