Module:Descriptions/Store

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
This is the main module for the following templates:
local p = {}
local h = {}

local DataTable = require("Module:Data Table")
local utilsMarkup = require("Module:UtilsMarkup")
local utilsString = require("Module:UtilsString")
local utilsTable = require("Module:UtilsTable")

local function parseFile(subject, game, fileType)
	local Franchise = require("Module:Franchise")
	local GalleryList = require("Module:Gallery List")
	local fileType = fileType or Franchise.graphics(game) == "2D" and "Sprite" or "Icon"
	local entry = GalleryList.parseEntry(subject, game, fileType)
	return entry.file
end

function p.DataTableStore(frame)
	return DataTable.Main(frame, {
		params = p.Templates["Data Table/Descriptions"].params,
		storeFn = h.storeDescriptions,
		requiredColumns = {"Description"},
	})
end

function h.storeDescriptions(args, rows)
	local columns, tags = DataTable.extractColumnTags(args.columns)
	local subjectColumn = args.subjectColumn or columns[1]
	local descriptionColumn = args.descriptionColumn or "Description"
	local contextColumn = args.contextColumn or "Context"
	
	local columnIndex = utilsTable.findIndex(columns, function(column)
		return column == subjectColumn
	end)
	local imageTag = columnIndex and utilsTable.find(tags[columnIndex], function(tag)
		return tag.name == "Image"
	end)

	for i, row in ipairs(rows) do
		local context = contextColumn and mw.text.killMarkers(row[contextColumn] or "")
		local description = mw.text.killMarkers(row[descriptionColumn] or "")
		local subject, subjectDisplayName = h.extractSubject(row[subjectColumn])
		
		local file
		if subject ~= "" and imageTag then
			file = parseFile(subject, args.game, imageTag.args[1])
		end
		
		if description ~= "" and subject ~= "" then
			mw.getCurrentFrame():expandTemplate({
				title = "Descriptions/Store",
				args = {
					game = args.game,
					descriptionType = args.descriptionType,
					subject = subject,
					context = context,
					subjectDisplayName = subjectDisplayName,
					description = description,
					image = file,
				},
			})
		end
	end
end

function h.extractSubject(cell)
	if cell == nil then
		return ""
	elseif not string.find(cell, "^%[%[") then
		return utilsMarkup.separateMarkup(cell)
	else
		local linkParts = utilsString.split(string.match(cell, "^%[%[([^]]+)]]"), "|")
		return unpack(linkParts)
	end
end

p.Templates = {
	["Data Table/Descriptions"] = {
		format = "block",
		params = {
			descriptionType = {
				type = "string",
				trim = true,
				nilIfEmpty = true,
				desc = "",
			},
			subjectColumn = {
				type = "string",
				trim = true,
				nilIfEmpty = true,
				desc = "",
			},
			contextColumn = {
				type = "string",
				trim = true,
				nilIfEmpty = true,
				desc = "",
			},
		},
	},
	["Descriptions/Store"] = {
		format = "block",
		paramOrder = {"game", "descriptionType", "subject", "context", "image", "subjectDisplayName", "description"},
		boilerplate = {
			separateRequiredParams = false,
		},
		params = {
			game = {
				required = true,
				type = "string",
				desc = "A game code from [[Data:Franchise]].",
			},
			descriptionType = {
				required = true,
				type = "string",
				desc = "The type of description, e.g. <code>Inventory</code>."
			},
			subject = {
				required = true,
				type = "wiki-page-name",
				desc = "A wiki article name referring to the subject being described.",
			},
			context = {
				type = "content",
				desc = "If the given subject has multiple descriptions of the same <code>type</code> in <code>game</code>, use this field to describe when the given description appears.",
			},
			image = {
				type = "wiki-page-name",
				desc = "A wiki file name referring to the image that accompanies the description, if applicable."
			},
			subjectDisplayName = {
				type = "content",
				desc = "The name of the subject as displayed alongside the description. Defaults to <code>subject</code>.",
			},
			description = {
				required = true,
				type = "content",
				desc = "The description of the subject.",
			},
		}
	}
}

return p