Module:Variants

From Final Fantasy XIV Online Wiki
Jump to navigation Jump to search
Documentation for Module:Variants [view] [edit] [history] [purge] (How does this work?)

This module implements {{Variants}}, which displays a list of alternate icons for an article subject, and defines semantic data allowing the alternate icons to be used with {{Game icon}}.



local getArgs = require("Module:Arguments").getArgs
local Icon = require("Module:Icon")

--- Converts empty string to nil, preserves all other values
local function notempty(str)
	if str == "" then
		return nil
	end
	return str
end

local p = {}

function p.main(frame)
	local args = getArgs(frame)

	-- read SMW properties of current page, so we can set some of the same
	-- properties on each variant
	local title = mw.title.getCurrentTitle().prefixedText
	local pageData = (mw.smw.ask({
		'[[' .. title .. ']]',
		'?Has canonical name#',
		'?Has internal ID',
		'?Has patch',
	}) or {})[1] or {}
	mw.log("Parent title: " .. title)
	mw.log("Parent page data: ")
	mw.logObject(pageData)

	-- read arguments (`icon1`, `description2`, etc.) into a list of variants
	local variants = {}
	local i = 1
	local variantArgs = {"name", "icon", "context", "description", "notes"}
	local variantDefaults = {
		name = pageData["Has canonical name"]
	}
	while true do
		mw.log('in the loop')
		-- read args for this variant into an object
		local variant = {}
		for _, prop in ipairs(variantArgs) do
			variant[prop] = notempty(args[prop .. i]) or variantDefaults[prop]
		end

		-- every variant needs a context; once we hit a variant with no context,
		-- we've hit the end of the arguments and break the loop
		if not variant.context then
			break
		end

		-- further processing of arguments into variant data
		variant.frameType = 'debuff' -- TODO
		variant.icon = "File:" .. variant.icon:gsub("^File:", "") -- auto-add File: prefix

		-- add to list and process the next variant's args
		table.insert(variants, variant)
		i = i + 1
	end

	-- process the variant data
	local table = mw.html.create('table'):addClass("wikitable")
		:node(mw.html.create('tr')
			:node(mw.html.create('th'):wikitext(''))
			:node(mw.html.create('th'):wikitext('Context'))
			:node(mw.html.create('th'):wikitext('Description'))
			:node(mw.html.create('th'):wikitext('Notes'))
		)

	for _, variant in ipairs(variants) do
		-- add semantic data subobject for the variant
		frame:callParserFunction("#subobject", {
			variant.context or "", -- use autogenerated subobject ID if no `context`

			-- data from variant args
			["Has game icon"] = variant.icon,
			["Has game icon frame type"] = variant.frameType,
			["Has game description"] = variant.description,
			["Has canonical name"] = variant.name,

			-- extra data from parent page tacked onto all variants unconditionally
			-- TODO: this data is not actually going to be reflective of the variant,
			--       it is only here to aid debugging. we should figure out whether
			--       we want to keep this soon
			["Has internal ID"] = pageData["Has internal ID"],
			["Has patch"] = pageData["Has patch"],
		})

		-- render table row
		table:node(mw.html.create('tr')
			:node(mw.html.create('td'):wikitext(
				tostring(Icon.gameIconWithLabel({
					icon = variant.icon,
					frameType = variant.frameType,
					size = 'small',
					text = variant.name,
				}))
			))
			:node(mw.html.create('td'):wikitext(variant.context))
			:node(mw.html.create('td'):wikitext(variant.description))
			:node(mw.html.create('td'):wikitext(variant.notes))
		)
	end

	return tostring(table)
end

return p