Module:Gallery List

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

local Franchise = require("Module:Franchise") 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 utilsString = require("Module:UtilsString") local utilsTable = require("Module:UtilsTable")

local CATEGORY_INVALID_ARGS = ""

-- Generally the latest remake is the default tab but some remakes don't have enough high quality images uploaded yet local DEFAULT_TAB_DENYLIST = { ["OoT3D"] = true, ["MM3D"] = true, ["TWWHD"] = true, ["TPHD"] = true, ["SSHD"] = true, }

-- Enhanced ports may have the same assets as the original game -- There's no need to show a separate tab in those cases local ENHANCED_PORTS = { ["OoTMQ"] = true, --["FSAE"] = true -- turns out FSAE does have its own sprites }

local doubleDigits = false

function p.Main(frame) local args, err = utilsArg.parse(frame:getParent.args, p.Templates["Gallery List"]) local categories = err and err.categoryText or "" local subjectType = args.subjectType local pagename = mw.title.getCurrentTitle.text

local tabData = {} local defaultTab = 1 local allEntries = {} for i, game in ipairs(Franchise.enumGames) do		local isListing = utilsString.endsWith(pagename, " in "..Franchise.shortName(game)) local rawEntries = args[game] rawEntries = rawEntries and utilsTable.filter(rawEntries, utilsString.notEmpty) if rawEntries and #rawEntries > 0 then local gallery, entries = h.printGallery(rawEntries, subjectType, game, utilsTable.clone(args)) allEntries = utilsTable.concat(allEntries, entries) local subjects = utilsTable.map(entries, "subject") subjects = utilsTable.unique(subjects) table.insert(tabData, {				label = Franchise.display(game),				content = gallery			}) if not DEFAULT_TAB_DENYLIST[game] then defaultTab = #tabData end if args.storeAs then TermList.storeSequence(game, args.storeAs, subjects) end if not isListing then local baseGame = Franchise.baseGame(game) for i, subject in ipairs(subjects) do					frame:expandTemplate({						title = "Location Features/Store",						args = {							feature = subjectType,							baseGame = baseGame,							game = game,							subject = subject,						}					}) end for j, remake in ipairs(Franchise.remakes(game)) do					if not args[remake] and not ENHANCED_PORTS[remake] then local gallery = h.printGallery(rawEntries, subjectType, game, utilsTable.clone(args)) table.insert(tabData, {							label = Franchise.display(remake),							content = gallery,						}) if not DEFAULT_TAB_DENYLIST[remake] then defaultTab = #tabData end end end end end end local tabs = utilsLayout.tabs(tabData, {		default = defaultTab,		tabOptions = {			collapse = true,		},	}) local html = mw.html.create("div") :addClass("zw-gallery-list") :wikitext(tabs)

local result = tostring(html)..h.hatnote(allEntries) return result, categories end

function p.parseEntry(rawEntry, game, fileType, options) local options = options or {} local subject, info = utilsMarkup.separateMarkup(rawEntry) local tags, info = h.parseTags(info) local link local term if options.useTerms ~= false then link = Term.link(subject, game, options) term = Term.fetchTerm(subject, game, options) or subject else link = string.format("%s", subject) term = subject end local entry = utilsTable.merge({}, tags, {		link = link,		info = info,		term = term,		subject = subject,	})

entry.file = tags.file or h.file(game, entry, fileType, options)

return entry end

-- TODO(@PhantomCaleb) -- Don't forget to update documentation for p.parseEntry when done function h.parseTags(info) local info, variant = h.parseVariant(info) local tags = { variant = variant, }	if variant and string.find(variant, "^File:") then tags.file = variant end return tags, info end

local VARIANT_REGEX = "^%s*%[([^%]]+)%]" function h.parseVariant(str) local str, markup = utilsMarkup.separateMarkup(str) local variant = string.match(markup, VARIANT_REGEX) markup = string.gsub(markup, VARIANT_REGEX, "") return str..markup, variant end

function h.file(game, entry, fileType, options) local suffix = fileType == "" and "" or " "..fileType local variant = entry.variant if variant then if tonumber(variant) then suffix = suffix .. " " .. variant else -- numbers can be placed in quotes to escape the above functionality -- e.g. Items in Spirit Tracks uses Quiver ["50"] to produce `File:ST Quiver 50 Icon.png` instead of `File:ST Quiver Icon 50.png` variant = utilsString.trim(variant, ")			suffix = " " .. variant .. suffix		end	end	local file	if variant == "No Image" then		file = "File:No Image.png"	elseif options.useTerms == false then		file = string.format("File:%s %s%s.png", game, entry.subject, suffix)	else		local term = entry.term		term = string.gsub(term, "#", "") -- filenames can't have # so we strip them from the terms		term = string.gsub(term, "", "") -- same goes for any formatting in the term, e.g. Cuccodex''		file = string.format("File:%s %s%s.png", game, entry.term, suffix)	end	return file end

function h.printGallery(rawEntries, subjectType, game, options) options = options or {} local fileType = options.fileType or h.fileType(subjectType, game) local entries = {} local galleryContent = "" for i, rawEntry in ipairs(rawEntries) do		local entry = p.parseEntry(rawEntry, game, fileType) galleryContent = galleryContent..entry.file.."|"..entry.link..entry.info.."\n" table.insert(entries, entry) end local size = Data.sizes[game] and Data.sizes[game][subjectType] or {} local gallery = mw.getCurrentFrame:extensionTag({		name = "gallery",		content = galleryContent,		args = {			caption = options.caption,			widths = options.widths or size.widths,			heights = options.heights or size.heights,			perrow = options.perrow,		}	}) return gallery, entries end

function h.fileType(subjectType, game) local graphics = Franchise.graphics(game) if subjectType == "Locations" then return "" elseif graphics == "2D" then return "Sprite" elseif subjectType == "Items" then return "Icon" elseif subjectType == "Treasures" then return "Icon" elseif subjectType == "Zonai Devices" then return "Icon" else return "Model" end end

-- TODO(@PhantomCaleb) function h.hatnote(entries) local sublocations = utilsTable.filter(entries, "sublocation") if #sublocations == 0 then return "" end sublocations = utilsTable.map(entries, "sublocation") sublocations = utilsTable.unique(sublocations) sublocations = table.sort(sublocations) for i, sublocation in ipairs(sublocations) do		sublocations[i] = Term.link(sublocation, game) end sublocations = mw.text.listToText(sublocations) local hatnote = mw.getCurrentFrame:expandTemplate({		title = "Hatnote",		args = "Parts of this section are derived from the following location(s): "..sublocations	}) end

function p.Schemas return { parseEntry = { subject = { required = true, type = "string", desc = "A wiki article name referring to the subject for which a filename is being generated.", },			game = { required = true, type = "string", desc = "A game code from Data:Franchise.", },			fileType = { required = true, type = "string", desc = "A file type such as  or  .", },			options = { type = "record", desc = "The options object is passed along to Module:Term and Module:Term, which is used to create the return object unless  is false.", properties = { {						name = "useTerms", type = "boolean", },				},			},		},		Data = { type = "record", required = true, properties = { {					name = "sizes", desc = "Sets the size of gallery thumbnails based on the  and   parameters of Template:Gallery List.", required = true, type = "map", keyPlaceholder = "game", keys = { type = "string" }, values = { type = "map", keyPlaceholder = "subjectType", keys = { type = "string" }, values = { type = "record", properties = { {									name = "widths", type = "string", required = true, desc = "A value in pixels corresponding to the  ." },								{									name = "heights", type = "string", required = true, desc = "A value in pixels corresponding to the  ." },							},						},					},				},			},		}	} end

function p.Documentation return { parseEntry = { desc = "Allows other modules such as Module:Data Table to use Template:Gallery List's filename generation syntax.", params = {"subject", "game", "fileType", "options"}, returns = "An object containing a filename, a term link, a term string, and the  received minus any Template:Gallery List syntax.", cases = { outputOnly = true, {					args = {"Candy (The Minish Cap)", "TMC", "Sprite"}, expect = { info = "", file = "File:TMC Candy Sprite.png", subject = "Candy (The Minish Cap)", term = "Candy", link = "Candy", }				},				{					args = {"Ore Chunk [Red] (10) ", "OoS", "Sprite"}, expect = { file = "File:OoS Ore Chunk Red Sprite.png", subject = "Ore Chunk", term = "Ore Chunk", link = "Ore Chunk", variant = "Red", info = " (10) ", }				},				{					args = {"Stained Glass ", "TMC", ""}, expect = { file = "File:TMC Stained Glass Artwork.png", subject = "Stained Glass", term = "Stained Glass", link = "Stained Glass", info = "", variant = "File:TMC Stained Glass Artwork.png", }				},				{					args = {"Hyper Pico Bloom [No Image]", "TMC", ""}, expect = { file = "File:No Image.png", term = "Hyper Pico Bloom", subject = "Hyper Pico Bloom", link = "Hyper Pico Bloom", info = "", variant = "No Image", }				},				{					args = {"Deku Tree", "TWW", "Figurine Model", { useTerms = false }}, expect = { file = "File:TWW Deku Tree Figurine Model.png", subject = "Deku Tree", term = "Deku Tree", link = "Deku Tree", info = "", },				},				{					args = {"Bomb", "ALttP", "30 Sprite", { plural = true } }, expect = { file = "File:ALttP Bombs 30 Sprite.png", subject = "Bomb", term = "Bombs", link = "Bombs", info = "", }				},			},		},	} end

function p.templateData local paramOrder = {1, "fileType", "storeAs", "caption", "perrow", "widths", "heights"} local params = { [1] = {			name = "subjectType", required = true, type = "string", enum = {"Animals", "Bosses", "Characters", "Creatures", "Enemies", "Equipment", "Locations", "Items", "Materials", "Objects", "Treasures", "Zonai Devices"}, desc = "The type of subject being listed.", trim = true, nilIfEmpty = true, },		fileType = { type = "string", desc = "Sets a custom filename suffix for gallery entries", trim = true, },		storeAs = { type = "string", desc = "Stores the list in the Sequences Cargo table for use by Template:Sort Value and Module:Sequences.", canOmit = true, trim = true, nilIfEmpty = true, },		caption = { type = "string", desc = "Caption text for the gallery.", trim = true, nilIfEmpty = true, canOmit= true, },		perrow = { type = "number", desc = "Maximum number of thumbnails to show per row in the gallery.", trim = true, nilIfEmpty = true, canOmit = true, },		widths = { type = "string", desc = "A value in pixels. Sets the width of gallery entries, overriding any default set in Module:Gallery List/Data", trim = true, nilIfEmpty = true, canOmit = true, },		heights = { type = "string", desc = "A value in pixels. Sets the heights of gallery entries, overriding any default set in Module:Gallery List/Data", trim = true, nilIfEmpty = true, canOmit = true, }	}	for i, game in ipairs(Franchise.enumGames) do		if Franchise.type(game) ~= "" then -- only games featured on the main page for now, to avoid adding too many parameters table.insert(paramOrder, game) params[game] = { type = "string", desc = "Comma-separated list of wiki page names referring to subjects in "..Franchise.display(game), trim = true, split = true, }		end end return { format = "block", params = params, paramOrder = paramOrder, } end

p.Templates = { ["Gallery List"] = p.templateData }

return p