Module:Icon List

local p = {} local h = {} local Data = mw.loadData("Module:Icon List/Data")

local File = require("Module:File") local Franchise = require("Module:Franchise") local Term = require("Module:Term") local TermList = require("Module:Term List") local utilsArg = require("Module:UtilsArg") local utilsString = require("Module:UtilsString")

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

function h.warn(msg, ...) local utilsError = require("Module:UtilsError") msg = string.format(msg, ...) utilsError.warn(msg) end

function p.Amounts(frame) local args, err = utilsArg.parse(frame:getParent.args, p.Templates["Amounts"]) local categories = err and err.categoryText or "" if err and err.args.game then return categories end local amounts, parseCategories = h.parseAmounts(args) categories = categories..parseCategories local list = h.printList(args, amounts, true) return list, categories end

function p.Main(frame) local args, err = utilsArg.parse(frame:getParent.args, p.Templates["Icon List"]) if err then return err.categoryText end local termLinks, terms = TermList.termList(args.game, args.items, {		link = true	}) local listItems = {} for i in ipairs(termLinks) do		local img = h.img(args.game, terms[i]) table.insert(listItems, {			img = img,			text = termLinks[i],			subject = terms[i],		}) end

return h.printList(args, listItems) end function h.printList(args, listItems, hasAmounts) local html = mw.html.create("div") :addClass("zw-icon-list")

local article = mw.title.getCurrentTitle.subpageText

for i, listItem in ipairs(listItems) do		-- Bold the item when shown on the item's article or a sandboxed version of it		-- See Amber Relic for example local isCurrentPage = false if listItem.subject == article or listItem.subject == Term.fetchTerm(article, args.game) then isCurrentPage = true end html:tag("span") :addClass("zw-icon-list__icon") :wikitext(listItem.img) :done local text = html:tag("span") :addClass("zw-icon-list__text") if isCurrentPage then text:addClass("zw-icon-list__text--current") end if listItem.amount then text:tag("span") :addClass("zw-icon-list__amount") :wikitext("× "..listItem.amount) :done end text:tag("span") :wikitext(listItem.text) :done end return tostring(html) end

function h.parseAmounts(args) local categories = "" local amountInputs = utilsString.split(args.amounts, '%s*,[%D+|%s*]') -- %D ensures that we don't split large numbers (e.g. 1,500) local amounts = {} for i, amountInput in ipairs(amountInputs) do		local amountStart, amountEnd, amount = string.find(amountInput, "^(%d+)%s") local item if not amount then item = amountInput else item = string.sub(amountInput, amountEnd+1) end

local amountValue = amount and string.gsub(amount, ",", "") amountValue = amount and tonumber(amountValue) or 1 local subject, info = TermList.separateAdditionalInfo(item)

local img = h.img(args.game, subject) local term = Term.printTerm(subject, args.game, {			link = true,			plural = amountValue ~= 1,		}) local text = term..info table.insert(amounts, { 			img = img, 			text = text, 			subject = subject, 			amount = amount		}) end return amounts, categories end

function h.img(game, subject) local file = Data.files[game] and Data.files[game][subject] local scalingFactor = Data.files.default.spriteScalingFactor local is2D = Franchise.graphics(game) == "2D" if not file then local fileType = is2D and "Sprite" or "Icon" file = string.format("File:%s %s %s.png", game, subject, fileType) end return File.image(file, {		size = "32x32px",		scale = is2D and scalingFactor or nil	}) end

p.Templates = { ["Amounts"] = { description = "Displays quantities of items.", purpose = "Displays quantities of items.", format = "inline", params = { [1] = {				name = "game", type = "string", required = true, enum = Franchise.enum, desc = "A game code." },			[2] = {				name = "amounts", type = "string", required = true, desc = " A comma-separated list of amounts in the format, where itemName is singular and refers to an article page on the wiki. The number may be omitted for unique items.  By default the generated filename is  , where fileType is   for 3D games and   for 2D games. The filename can be customed per item at Module:Icon List/Data. ", },		},		examples = { {"SSHD", "3 Ancient Flower, 4 Amber Relic, 1 Blue Bird Feather, 1 Goddess Plume"}, {"OoS", "20 Ore Chunk, 10 Ember Seed "}, {"PH", "1 Big Green Rupee, 1 Red Rupee"}, {"HWAoC", "1 Ethereal Stone, 5 Voltfruit, 10 Raw Bird Thigh, Desert Voe Headband"}, },	},	["Icon List"] = { description = "Displays a list of items alongside their icons.", purpose = "Displays lists of items alongside their icons.", params = { [1] = {				name = "game", required = true, enum = Franchise.enum, type = "string", desc = "A game code.", trim = true, nilIfEmpty= true, },			[2] = {				name = "items", required = true, type = "content", desc = "A comma-separated list of items.", tim = true, nilIfEmpty = true, split = true, },		},		examples = { {				desc = "Hateno Dye materials", args = {"BotW", "Chillshroom, Blue Nightshade, Winterwing Butterfly, Cold Darner, Hearty Lizard, Sapphire, Icy Lizalfos Tail, Ice Keese Wing"}, },			{				desc = "Monster Parts", args = {"BotW", "Ancient Screw, Bokoblin Horn, Chuchu Jelly, Hinox Toenail, Keese Wing, Lizalfos Horn, Moblin Horn, Octo Balloon, Octorok Tentacle"} },		},	}, }

return p