Module:Infobox

Revision as of 08:44, 12 June 2024 by imported>RheingoldRiver (Adding default set of pages)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Module:Infobox is the DRUID-style layout engine that turns the simple parameters of an infobox template (such as {{ItemInfobox}} or {{NPCInfobox}}) into a full, tabbed, styled infobox.

Overview

This module builds the boxes you see in the top corner of item, weapon, armour and boss/NPC pages. It is the local copy of the "DRUID" infobox framework: a generic system that takes a list of sections, a list of data fields inside each section, and an optional set of tabs (e.g. the Tiered / Untiered / Shiny tabs on item pages), and outputs the HTML/CSS the wiki's styles and gadgets know how to display.

Editors almost never call this module directly. Instead they fill in a friendly wrapper template:

  • {{ItemInfobox}} — weapons, abilities and armour. Its source contains the long Script error: The function "main ..." does not exist. call with all the section/field plumbing already wired up.
  • {{NPCInfobox}} — bosses and NPCs ({{BossInfobox}} is a redirect to it).

Those wrapper templates do all the work of mapping a handful of human-friendly parameters (title, images, Desc, Stats, Drop, etc.) onto the many low-level parameters this module expects. If you only want to document an item or a boss, edit the page that uses the wrapper template — you should not need to touch this module.

The module also tags every page that uses it with (used for maintenance and for loading the interactive tab/worn-toggle gadget).

Functions / entry points

Function (#invoke) What it does Called by
p.main The main entry point. Reads all parameters, splits the sections/fields/tabs, assigns the infobox a unique id, optionally sets the page's main image, and renders the whole infobox. Returns the infobox plus the tracking category. {{ItemInfobox}}, {{NPCInfobox}}
p.arraymap A Lua re-implementation of Page Forms' #arraymap: splits argument 1 on a separator, substitutes each piece into a pattern, and re-joins. A general utility; not the main rendering path. (helper; available for templates that need arraymap behaviour)
p.preprocess Expands (preprocesses) the wikitext passed as its first argument. A small utility for templates that need to force expansion of a value. (helper)

Everything else in the module is internal (the h.* helper functions): splitting argument lists, building each section, handling tabs, choosing

vs markup, escaping CSS class names, and so on. These are not #invoke entry points.

How it's used

The wrapper template holds the real call. For example, {{ItemInfobox}} contains (abbreviated):

{{#invoke:Infobox|main
|kind=item
|class=item-infobox
|sep=;
|images=...        (built from the template's |images= parameter)
|tabs=...          (the tab labels, e.g. Tiered / Untiered)
|sections=Description;Stats;Abilities;Reskins;Info
|Description=Unholy;Voidbound;Bloodshot;Royal;...;Desc
|Stats=Type;Attack;Defense;...;Range;Velocity
|Abilities=Ability;SP
|Reskins=ReskinList
|Info=Drop;TP;Classes
...
}}

Key concepts visible there:

  • |sep=; — the separator used to split lists (this infobox uses a semicolon instead of the default comma, because field values themselves contain commas).
  • |sections=... — the ordered list of sections to render.
  • |<Section>=field1;field2;... — the data fields that belong to each section.
  • |<field>_label=, |<field>_nolabel=true, etc. — per-field display options.
  • |tabs= / |<tab>_<field>= — values that differ per tab.

Ordinary editors should not write Script error: The function "..." does not exist. by hand. Put item or boss data into {{ItemInfobox}} / {{NPCInfobox}} and let those templates call this module.

Notes

  • Automatic Shiny tab. If an infobox has exactly one image whose file name starts with Ut- (the untiered-item naming scheme, e.g. Ut-Onyxblade.png) and a matching File:ShinyUt-...png exists on the wiki, the module automatically adds a second image and a "Shiny" tab (labels become Untiered / Shiny). This is why untiered items get a Shiny toggle without any extra parameters.
  • Automatic Worn variant. For any image File:Name.ext, if a File:NameWorn.ext exists, the module adds a "Show Worn" toggle button so armour can show its worn-on-character sprite.
  • Main image. Unless |setmainimage=no, the first image is registered as the page's main image (used for social/share previews) via #setmainimage; this fails silently if the wiki lacks that parser function.
  • div vs table. By default the module emits
    -based markup (USE_DIVS = true at the top of the file). This can be overridden globally in the module or per-infobox with |useDivs=yes/no.
  • Unique ids. Each infobox on a page gets an incrementing DRUID_INFOBOX_ID (via the Variables extension if present, otherwise a fallback) so multiple infoboxes and their tabs don't collide.
  • Optional hooks. If Module:Infobox/Hooks exists, its functions are called at defined points (onCastArgsStart/End, onMakeOutputStart/End) to let a wiki customise behaviour without editing this module.
  • Related: {{ItemInfobox}}, {{NPCInfobox}}/{{BossInfobox}}, and the per-item display modules {{Equipment}}m and {{Shinies}}m (which collect {{ItemInfobox}} blocks from category templates), and {{Reskins}}m (which feeds reskin data into {{ItemInfobox}}).

local counter

-- default value to show if a param is missing in some but not all tabs.
-- set to `nil` (not in quotes) to remove such rows altogether in the tabs where they're missing
local TABBED_NONEXIST = ''

local h = {}
local p = {}

function p.arraymap(frame)
	-- a lua implementation of Page Forms' arraymap
	local args = h.overwrite()
	local items = h.split(args[1], args[2] or ',')
	for i, item in ipairs(items) do
		items[i] = args[4]:gsub(args[3], item)
	end
	return table.concat(items, args[5] or ',')
end

function p.preprocess(frame)
    return frame:preprocess(frame.args[1] or frame:getParent().args[1])
end

function p.main(frame)
	h.increment()
	local args = h.overwrite()
	local sep = args.sep or ','
	h.castArgs(args, sep)
    h.setMainImage(args.images[1])
	return h.makeInfobox(args, sep)
end

function h.increment()
	counter = mw.getCurrentFrame():callParserFunction('#var', {'DRUID_INFOBOX_ID', 0}) + 1
	mw.getCurrentFrame():callParserFunction('#vardefine', {'DRUID_INFOBOX_ID', counter})
end

function h.castArgs(args, sep)
	args.tabs = h.split(args.tabs or args.image_labels, sep)
	args.images = h.getImages(args, sep)
	args.sections = h.split(args.sections, sep)
	for _, section in ipairs(args.sections) do
		args[section] = h.split(args[section], sep)
	end
end

function h.getImages(args, sep)
	if args.image and not args.images then
		args.images = args.image
	end
	if args.images then
		return h.split(args.images, sep)
	end
	if not args.tabs then return {} end
	local ret = {}
	for _, key in ipairs(args.tabs) do
		if args[key .. '_image'] then
			ret[#ret+1] = args[key .. '_image']
		end
	end
	return ret
end

function h.setMainImage(file)
    if not file then return end
	mw.getCurrentFrame():callParserFunction{
		name = '#setmainimage',
		args = { file:gsub('File:', '') },
	}
end

function h.makeInfobox(args, sep)
	local out = mw.html.create('table')
		:addClass('druid-infobox')
		:addClass('druid-container')
		:attr('id', 'druid-container-' .. counter)
	if args.kind then out:addClass('druid-container-' .. h.escape(args.kind)) end
	if args.title then
		out:tag('tr')
			:tag('th')
				:addClass('druid-title')
				:attr('colspan', 2)
				:wikitext(args.title)
	end
	h.printImages(out, args.images, args)
	for _, section in ipairs(args.sections) do
		-- cannot begin tagging here because we don't know if any applicable args are present
		local cols = args[section .. '_columns']
		local makeSection = cols and h.makeGridSection or h.makeSection
		out:node(makeSection(section, args[section], args, tonumber(cols)))
	end
	return out
end

function h.printImages(out, images, args)
	if #images == 0 and #args.tabs == 0 then return end
	-- burden is on the user to format this as an image. this should be done in the infobox template,
	-- with something like |image={{#if:{{{image|}}}|[[File:{{{image|}}}{{!}}300px{{!}}link=]]}}
	local td = out:tag('tr')
		:tag('td')
		:attr('colspan', 2)
	h.printTabs(td, args.tabs, images, args)
	if #images == 0 then return end
	if #images == 1 then
		td:addClass('druid-main-image')
			:wikitext(images[1])
		return
	end
	td:addClass('druid-main-images')
	local imagesContainer = td:tag('div')
		:addClass('druid-main-images-files')
	for i, item in ipairs(images) do
		local container = imagesContainer:tag('div')
			:addClass('druid-main-images-file')
			:addClass('druid-toggleable')
			:attr('data-druid', counter .. '-' .. i)
			:wikitext(item)
		local labelText = args[item .. '_label'] or item or ('[[Category:Infoboxes missing image labels]]Image ' .. i)
		if args[labelText .. '_caption'] then
			container:tag('div')
				:addClass('druid-main-images-caption')
				:wikitext(args[labelText .. '_caption'])
		end
		if i == 1 then
			container:addClass('focused')
		end
	end
end

function h.printTabs(td, tabs, images, args)
	if #tabs == 0 and #images <= 1 then return end
	local container = td:tag('div')
		:addClass('druid-main-images-labels')
		:addClass('druid-tabs')
	if #tabs == 0 then
		for i, _ in ipairs(images) do
			local labelText = '[[Category:Infoboxes missing image labels]]Image ' .. i
			h.printTab(container, labelText, i)
		end
		return
	end
	for i, item in ipairs(tabs) do
		local labelText = args[item .. '_label'] or item
		h.printTab(container, labelText, i)
	end
end

function h.printTab(container, text, i)
	local label = container:tag('div')
		:addClass('druid-main-images-label')
		:addClass('druid-tab')
		:addClass('druid-toggleable')
		:attr('data-druid', counter .. '-' .. i)
		:wikitext(text)
	if i == 1 then
		label:addClass('focused')
	end
end

function h.makeGridSection(section, sectionFields, args, numCols)
	local shouldPrint = false
	local node = mw.html.create()
	h.printSectionHeader(node, section, args)
	local tr = node:tag('tr')
		:attr('data-druid-section-row', h.escape(section))
	if args[section .. '_collapsed'] then
		tr:addClass('druid-collapsed')
	end
	local grid = tr:tag('td')
		:attr('colspan', 2)
		:addClass('druid-grid-section')
		:addClass('druid-grid-section-' .. h.escape(section))
		:tag('div')
			:addClass('druid-grid')
	local row = 1
	local col = 1
	local itemContainer
	for _, item in ipairs(sectionFields) do
		if args[item] then
			shouldPrint = true
			itemContainer = grid:tag('div')
				:addClass('druid-grid-item')
				:addClass('druid-grid-item-' .. h.escape(item))
				:css('grid-column', col)
				:css('grid-row', row)
			if not args[item .. '_nolabel'] then
				h.printLabel(itemContainer:tag('div'), item, args)
			end
			h.printData(itemContainer:tag('div'), item, args)
			
			if col == numCols then
				row = row + 1
				col = 1
			else
				col = col + 1
			end
		end
	end
	grid:css('grid-template-columns', ('repeat(%s, 1fr)'):format(row > 1 and numCols or col - 1))
	if not shouldPrint then return nil end
	itemContainer:css('grid-column', ('%s / -1'):format(col - 1))
	return node
end

function h.makeSection(section, sectionFields, args)
	local shouldPrint = false
	local node = mw.html.create()
	h.printSectionHeader(node, section, args)
	for _, item in ipairs(sectionFields) do
		if h.shouldPrint(item, args) then
			shouldPrint = true
			local tr = node:tag('tr')
				:addClass('druid-row')
				:addClass('druid-row-' .. h.escape(item))
				:attr('data-druid-section-row', h.escape(section))
			if args[section .. '_collapsed'] then
				tr:addClass('druid-collapsed')
			end
			if args[item .. '_wide'] or args[item .. '_nolabel'] then
				local td = h.printData(tr:tag('td'), item, args)
				td
					:attr('colspan', 2)
					:addClass('druid-data-wide')
			else
				h.printLabel(tr:tag('th'), item, args)
				h.printData(tr:tag('td'), item, args)
			end
		end
	end
	if not shouldPrint then return nil end
	return node
end

function h.shouldPrint(item, args)
	if args[item] then return true end
	for _, key in ipairs(args.tabs) do
		if args[key .. '_' .. item] then
			return true
		end
	end
	return false
end

function h.printLabel(node, item, args)
	return node
		:addClass('druid-label')
		:addClass('druid-label-' .. h.escape(item))
		:wikitext(args[item .. '_display'] or args[item .. '_label'] or item)
end

function h.printData(node, item, args)
	if not args.tabs or #args.tabs == 0 then
		h.printSimpleData(node, item, args)
		return node
	end
	if not h.hasComplexData(item, args) then
		h.printSimpleData(node, item, args)
		return node
	end
	for i, label in ipairs(args.tabs) do
		local div = node:tag('div')
			:addClass('druid-toggleable-data')
			:addClass('druid-toggleable')
			:attr('data-druid', counter .. '-' .. i)
		if h.getTabbedContent(args, label, item) then
			div:wikitext('\n\n' .. h.getTabbedContent(args, label, item))
		else
			div:addClass('druid-toggleable-data-empty')
		end
		if i == 1 then div:addClass('focused') end
	end
	return node
end

function h.getTabbedContent(args, label, item)
	return args[label .. '_' .. item] or args[item] or TABBED_NONEXIST
end

function h.printSimpleData(node, item, args)
	node:addClass('druid-data')
		:addClass('druid-data-' .. h.escape(item))
		:wikitext('\n\n' .. args[item])
end

function h.hasComplexData(item, args)
	for _, v in ipairs(args.tabs) do
		if args[v .. '_' .. item] then return true end
	end
	return false
end

function h.printSectionHeader(node, section, args)
	if args[section .. '_nolabel'] then return end
	local tr = node:tag('tr')
		:attr('data-druid-section', h.escape(section))
	local th = tr:tag('th')
		:attr('colspan', 2)
		:addClass('druid-section')
		:addClass('druid-section-' .. h.escape(section))
	if args[section .. '_collapsible'] then
		tr:addClass('druid-collapsible')
		if args[section .. '_collapsed'] then
			tr:addClass('druid-collapsible-collapsed')
		end
	end
	local emptySections = {}
	for _, label in ipairs(args.tabs) do
		local hasLabel = false
		for _, item in ipairs(args[section] or {}) do
			if h.getTabbedContent(args, label, item) then
				hasLabel = true
			end
		end
		if not hasLabel then emptySections[label] = true end
	end
	if not next(emptySections) then
		th:wikitext(args[section .. '_label'] or section)
		return
	end
	for i, label in ipairs(args.tabs) do
		local div = th:tag('div')
			:addClass('druid-toggleable-heading')
			:addClass('druid-toggleable')
			:attr('data-druid', counter .. '-' .. i)
			:wikitext(args[section .. '_label'] or section)
		-- we are going to print the section content even in empty nodes
		-- for compatibility with browsers without :has, where hiding empty rows won't happen
		if emptySections[label] then
			div:addClass('druid-toggleable-heading-empty')
		end
		if i == 1 then
			div:addClass('focused')
		end
	end
end

function h.overwrite()
	-- this is a generic utility function that collects args from the invoke call & the parent template.
	-- normally, you merge args with parent template overwriting the invoke call, but
	-- since we'll be putting markup/formatting into our invoke call,
	-- we actually want to overwrite what the user sent.
	local f = mw.getCurrentFrame()
	local origArgs = f.args
	local parentArgs = f:getParent().args

	local args = {}
	
	for k, v in pairs(parentArgs) do
		v = mw.text.trim(v)
		if v ~= '' then
			args[k] = v
		end
	end
	
	for k, v in pairs(origArgs) do
		v = mw.text.trim(tostring(v))
		if v ~= '' then
			args[k] = v
		end
	end
	
	return args
end

-- generic utility functions
-- these would normally be provided by other modules, but to make installation easy
-- I'm including everything here

function h.split(text, pattern, plain)
	if not text then
		return {}
	end
	local ret = {}
	for m in h.gsplit(text, pattern, plain) do
		ret[#ret+1] = m
	end
	return ret
end

function h.gsplit( text, pattern, plain )
	if not pattern then pattern = ',' end
	if not plain then
		pattern = '%s*' .. pattern .. '%s*'
	end
	local s, l = 1, text:len()
	return function ()
		if s then
			local e, n = text:find( pattern, s, plain )
			local ret
			if not e then
				ret = text:sub( s )
				s = nil
			elseif n < e then
				-- Empty separator!
				ret = text:sub( s, e )
				if e < l then
					s = e + 1
				else
					s = nil
				end
			else
				ret = e > s and text:sub( s, e - 1 ) or ''
				s = n + 1
			end
			return ret
		end
	end, nil, nil
end

function h.escape(s)
	s = s:gsub(' ', '')
		:gsub('"', '')
		:gsub("'", '')
		:gsub("%?", '')
		:gsub("%%", '')
		:gsub("%[", '')
		:gsub("%]", '')
		:gsub("{", '')
		:gsub("}", '')
		:gsub("!", '')
	return s
end

return p