Module:Term List

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
This is the main module for the following templates: In addition, this module exports the following functions.

termList

termList(game, pages, [options])

Parameters

Returns

  • A list the same size as pages with Module:Term#printTerm applied to each page.
  • A list the same size as pages with Module:Term#fetchTerm applied to each page. The page name is returned as-is if it stores no terms.

Examples

#InputOutputResultStatus
Link's relatives
1
termList(
  "TWWHD",
  {
    "Link's Grandma",
    "Aryll <small>(little sister)</small>",
  },
  { link = true }
)
{
  "[[Link's Grandma#The Wind Waker|Grandma]]",
  "[[Aryll#The Wind Waker|Aryll]] <small>(little sister)</small>",
}
table
Green check.svg
{"Grandma", "Aryll"}
table
Green check.svg

local p = {}
local h = {}

local Franchise = require("Module:Franchise")
local Term = require("Module:Term")
local utilsArg = require("Module:UtilsArg")
local utilsCargo = require("Module:UtilsCargo")
local utilsMarkup = require("Module:UtilsMarkup")
local utilsString = require("Module:UtilsString")

function p.Main(frame)
	local args, err = utilsArg.parse(frame:getParent().args, p.Templates["Term List"])
	local categories = err and err.categoryText or ""
	
	local pages = args.pages or {}
	local game = args.game or "Series"
	
	local terms = p.termList(game, pages, args)
	if args.listType == "bullet" then
		terms = utilsMarkup.bulletList(terms)
	elseif args.listType == "plain" then
		terms = utilsMarkup.list(terms)
	else
		terms = table.concat(terms, ", ")
	end
	
	return terms, categories
end

function p.termList(game, entries, options)
	options = options or {}

	local formattedTerms = {}
	local terms = {}
	local pages = {}
	for i, page in ipairs(entries) do
		local page, additionalInfo = utilsMarkup.separateMarkup(page)
		formattedTerms[i] = Term.printTerm(page, game, options)..additionalInfo
		formattedTerms[i] = string.gsub(formattedTerms[i], " %+", "&nbsp;+") -- a hack for terms like "Air Potion +" to prevent the + from wrapping on a separate line
		terms[i] = Term.fetchTerm(page, game, options) or page
		terms[i] = string.gsub(terms[i], [['']], "") -- Remove formatting which may be stored with book terms, e.g. ''Cuccodex''
		pages[i] = page
	end
	if options.storeAs then
		p.storeSequence(game, options.storeAs, pages)
	end

	return formattedTerms, terms, pages
end

function p.storeSequence(game, sequence, pages)
	local queryResult = utilsCargo.query("Sequences", "_pageName", {
		where = utilsCargo.allOf(
			{
				game = game,
				sequence = sequence
			},
			string.format("_pageName != '%s'", mw.title.getCurrentTitle().fullText)
		)
	})
	local pageAlreadyStoringSequence = queryResult[1] and queryResult[1]._pageName
	if pageAlreadyStoringSequence then
		local utilsError = require("Module:UtilsError")
		utilsError.warn(string.format("The page [[%s]] is already storing sequence <code>%s</code> for game <code>%s</code>. This sequence will not be stored.", pageAlreadyStoringSequence, sequence, game))
		return
	end
	
	local seenPages = {} -- prevents duplicates
	local ordinality = 1
	for i, page in ipairs(pages) do
		if not seenPages[page] then
			seenPages[page] = true
			mw.getCurrentFrame():expandTemplate({
				title = "Term List/Store",
				args = {
					game = game,
					sequence = sequence,
					subject = page,
					ordinality = ordinality,
				}
			})
			ordinality = ordinality + 1
		end
	end
end

function p.Schemas(frame)
	return {
		termList = {
			game = {
				required = true,
				type = "string",
				desc = "A [[Data:Franchise]] game code, or the string <code>Series</code>.",
			},
			pages = {
				required = true,
				type = "array",
				items = { type = "string" },
				desc = "An array of wiki page names.",
			},
			options = {
				type = "record",
				desc = "See [[Module:Term#fetchTerm]] and [[Module:Term#printTerm]] for available options.",
			},
		},
	}
end

function p.Documentation() 
	return {
		termList = {
			params = {"game", "pages", "options"},
			returns = {
				"A list the same size as <code>pages</code> with [[Module:Term#printTerm]] applied to each page.",
				"A list the same size as <code>pages</code> with [[Module:Term#fetchTerm]] applied to each page. The page name is returned as-is if it stores no terms.",
			},
			cases = {
				{
					desc = "[[Link]]'s relatives",
					args = {"TWWHD", {"Link's Grandma", "Aryll <small>(little sister)</small>"}, { link = true }},
					expect = {
						{
							"[[Link's Grandma#The Wind Waker|Grandma]]",
							"[[Aryll#The Wind Waker|Aryll]] <small>(little sister)</small>",
						},
						{"Grandma", "Aryll"},
					},
				},
			}
		}
	}
end

p.Templates = {
	["Term List"] = {
		purpose = "Applies {{Template|Term}} or {{Template|Plural}} to a list of page names.",
		paramOrder = {1, 2, "link", "plural", "listType", "storeAs"},
		params = {
			[1] = {
				name = "game",
				required = true,
				enum = Franchise.enum({ includeSeries = true }),
				type = "string",
				desc = "A game code.",
				nilIfEmpty= true,
			},
			[2] = {
				name = "pages",
				required = true,
				type = "content",
				desc = "Comma-separated list of wiki page names.",
				trim = true,
				nilIfEmpty= true,
				split = true,
			},
			link = {
				type = "boolean",
				desc = "If present, the <code>link</code> option of {{Template|Term}} is applied.",
				canOmit = true,
			},
			plural = {
				type = "boolean",
				desc = "If present, {{Template|Plural}} is used instead of {{Template|Term}}.",
				canOmit = true,
			},
			listType = {
				type = "string",
				enum = {"bullet", "comma", "plain"},
				default = "comma",
				desc = "Sets the list formatting.",
				canOmit = true,
			},
			storeAs = {
				type = "string",
				desc = "Stores the list in the [[Special:CargoTables/Sequences|Sequences]] Cargo table for use by [[Template:Sort Value]] and [[Module:Sequences]].",
				canOmit = true,
				trim = true,
				nilIfEmpty = true,
			},
		}
	},
	["Term List/Store"] = {
		purpose = "Stores in-game sequences that can be used by [[Template:Sort Value]], [[Module:Sequences]], and maybe some day [[Template:Translation Page]].",
		storesData = true,
		usage = "This template is automatically used via [[Template:Gallery List]] or [[Template:Term List]] when the <code>storeAs</code> parameter is set.",
		paramOrder = {"game", "sequence", "subject", "ordinality"},
		params = {
			game = {
				type = "string",
				required = true,
				desc = "A game code from [[Data:Franchise]].",
			},
			sequence = {
				type = "string",
				required = true,
				desc = "A sequence name.",
			},
			subject = {
				type = "wiki-page-name",
				required = true,
				desc = "An article name on the wiki.",
			},
			ordinality = {
				type = "number",
				required = true,
				desc = "The position of <code>subject</code> in the given sequence.",
			},
		},
	},
}

return p