Module:Data Table/Tags

local File = require("Module:File") local Franchise = require("Module:Franchise") local GalleryList = require("Module:Gallery List") local Sequences = require("Module:Sequences") local Term = require("Module:Term") local TermList = require("Module:Term List") local utilsMarkup = require("Module:UtilsMarkup") local utilsString = require("Module:UtilsString") local utilsTable = require("Module:UtilsTable")

local frame = mw.getCurrentFrame

local playerNameOutput local function playerName return playerNameOutput or frame:expandTemplate({		title = "Player Name"	}) end

local function list(items, bulletList) if bulletList then return utilsMarkup.bulletList(items) elseif #items > 1 then return utilsMarkup.list(items) else return items[1] end end

local function value(templateName, hasGameParam) return function(cell, tableArgs, tagArgs) local values = utilsString.split(cell, '%s*,[%D+|%s*]') local amountGame = tableArgs.game or tagArgs[1] local format = tagArgs[2] local cellSize = 0 local sortValue values = utilsTable.map(values, function(value)			local value, info = TermList.separateAdditionalInfo(value)			if #values == 1 then				sortValue = value:gsub(",", "")			end			cellSize = math.max(cellSize, value:len)			local args = {value, format}			if hasGameParam then				table.insert(args, 1, amountGame)			end			local text = frame:expandTemplate({ title = templateName, args = args, })			return text..info		end) local valuesList = list(values) return { text = valuesList, size = cell:len, sortValue = sortValue }	end end

local function terms(tagName) return function(cell, tableArgs, tagArgs) cell = string.gsub(cell, "%[Player Name%]", "Link") local pages if tagName == "Term" or tagName == "Plural" then pages = {cell} else pages = utilsString.split(cell) end pages = utilsTable.map(pages, GalleryList.stripVariantSyntax) local termLinks, terms = TermList.termList(tagArgs[1] or tableArgs.game, pages, {			plural = tagName == "Plural",			link = true,		}) local cellSize = 0 for i in ipairs(termLinks) do			local term = terms[i] cellSize = math.max(term:len, cellSize) end local isBulletList = tagName == "TermList" or tagName == "PluralList" local list = list(termLinks, isBulletList) local sortValue if #termLinks == 1 then sortValue = terms[1] end return { text = list, sortValue = sortValue, size = cellSize }	end end

local spanningCell local spanCount function computeRowspan(cell) if not spanningCell then spanningCell = cell spanCount = 1 elseif tostring(cell.content) == tostring(spanningCell.content) then cell.skip = true spanCount = spanCount + 1 else spanningCell.rowspan = spanCount spanningCell = cell spanCount = 1 end if cell.isLastRow then spanningCell.rowspan = spanCount end end

local p = { -- Tags in the source table which should not be automatically brought over to copies of the table nonCopiedTags = {"Width"}, -- Each function must return a table containing: -- text (required), the text to display in the cell -- size (optional), an estimation of the cell's width in characters -- sortValue (optional), a column sort value — column is unsortable if false, sorted by its content if nil contentTags = { [""] = function(cell) -- default: no tags local text = string.gsub(cell, "%[Player Name%]", playerName) return { sortValue = string.gsub(cell, "%[Player Name%]", "Link"), text = cell, size = cell:len, }		end, ["NotApplicable"] = function(cell) -- special case: cell starts with N/A local text, info = TermList.separateAdditionalInfo(cell) local tooltip = utilsMarkup.tooltip("—", "Not Applicable") text = text:gsub("N/A", tooltip) return { text = text, sortValue = text, size = cell:len, }		end,

-- Tags ["Amounts"] = function(cell, tableArgs, tagArgs) local text = frame:expandTemplate({				title = "Amounts",				args = {tableArgs.game, cell}			})

local cellSize = 0 local amounts = utilsString.split(cell, '%s*,[%D+|%s*]') for i, amount in ipairs(amounts) do				local item = string.gsub(amount, "$[%d,]+ ", "") local subject, info = TermList.separateAdditionalInfo(item) cellSize = math.max(cellSize, subject:len) end

return { text = text, size = cellSize, sortValue = false, }		end, ["Defense"] = value("Defense", true), ["Description"] = function(cell, tableArgs, tagArgs) local text = string.gsub(cell, "%[Player Name%]", playerName) text = "\n"..text.."\n" -- bullet lists don't work without the newlines return { text = text, size = cell:len, sortValue = false, }		end, ["Effect"] = value("Effect", false), ["HeartAmount"] = function(cell, tableArgs, tagArgs) local text = frame:expandTemplate({				title = "HeartAmount",				args = {cell, "true"}			}) return { text = text, size = 12, sortValue = cell }		end, ["IconList"] = function(cell, tableArgs, tagArgs) local cellSize = 0 local listItems = utilsString.split(cell) for i, listItem in ipairs(listItems) do				cellSize = math.max(listItem:len, cellSize) end local iconList = frame:expandTemplate({				title = "Icon List",				args = {tableArgs.game, cell}			}) return { text = iconList, size = cellSize, sortValue = false, }		end, ["Image"] = function(cell, tableArgs, tagArgs) cell = string.gsub(cell, "%[Player Name%]", "Link") local entries = GalleryList.parseEntries(nil, tableArgs.game, {cell}, {				useTerms = false,				fileType = tagArgs[1] or Franchise.graphics(tableArgs.game) == "2D" and "Sprite" or "Icon",			}) local image = File.image(entries[1].file, {				size = tagArgs[2] or "64x64px",			}) return { text = image, size = 20, sortValue = false, }		end, ["List"] = function(cell, tableArgs, tagArgs) local listItems = utilsString.split(cell, '%s*,[%D+|%s*]') local cellSize = 0 for i, listItem in ipairs(listItems) do				cellSize = math.max(cellSize, listItem:len) end local list = list(listItems, true) return { text = list, sortValue = false, }		end, ["Mon"] = value("Mon", false), ["Name"] = function(cell, tableArgs, tagArgs) local cellSize = cell:len local text = string.gsub(cell, "%[Player Name%]", playerName) text = GalleryList.stripVariantSyntax(text) return { text = text, }		end, ["Plural"] = terms("Plural"), ["PluralList"] = terms("PluralList"), ["Rupees"] = value("Rupee", true), ["SortValue"] = function(cell, tableArgs, tagArgs) cell = string.gsub(cell, "%[Player Name%]", "Link") local sortValue = Sequences.sortValue(tableArgs.game, nil, cell) local termLink = Term.link(cell, tableArgs.game) return { size = cell:len, text = termLink, sortValue = sortValue, }		end, ["Term"] = terms("Term"), ["Terms"] = terms("Terms"), ["TermList"] = terms("TermList"), },	attributeTags = { ["ID"] = function(cell, tableArgs, tagArgs) cell.id = string.gsub(cell.raw, "%[Player Name%]", "Link") end, ["Nowrap"] = function(cell, tableArgs, tagArgs) cell.content:addClass("data-table__cell-content--nowrap") end, ["Rowspan"] = function(cell, tableArgs, tagArgs) computeRowspan(cell) end, ["Unsortable"] = function(cell, tableArgs, tagArgs) cell.sortValue = nil end, ["Width"] = function(cell, tableArgs, tagArgs) cell.styles = cell.styles or {} cell.styles["width"] = tagArgs[1] end, } }

return p