Module:Infobox/doc

From the Tesseract Wiki, the wiki for all things Marvel Cinematic Universe
Jump to navigation Jump to search

This is the documentation page for Module:Infobox

Information icon-grey.svg
This is a documentation subpage for Module:Infobox.
It contains usage information, categories, and other content that is not part of the original module page.
Module:Infobox requires Module:Edit button.
Function list
L 30 — hasContent
L 37 — subjectName
L 42 — image
L 51 — numbers
L 57 — wrapContent
L 87 — Infobox.nilParam
L 97 — button_width
L 106 — Infobox.isDefined
L 135 — Infobox.new
L 185 — Infobox:setAddTSWInfoboxClass
L 197 — Infobox:setAddTSWDataboxClass
L 213 — Infobox:create
L 310 — Infobox:defineName
L 326 — Infobox:defineLinks
L 345 — Infobox:setMaxButtons
L 383 — Infobox.addRow
L 467 — Infobox.customButtonPlacement
L 474 — Infobox.setDefaultVersionSMW
L 479 — Infobox.addButtonsRow
L 491 — Infobox.addButtonsCaption
L 504 — Infobox.pad
L 519 — Infobox.wikitext
L 528 — Infobox.append
L 540 — Infobox.caption
L 573 — Infobox.attr
L 579 — Infobox.float
L 584 — Infobox.css
L 590 — Infobox.addClass
L 596 — Infobox.addClasses
L 611 — Infobox.tag
L 627 — Infobox:useSMW
L 639 — Infobox:useSMWOne
L 652 — Infobox:useSMWSubobject
L 660 — Infobox:useSMWElement
L 672 — Infobox:finish
L 754 — smwDefault
L 1072 — Infobox:defineParams
L 1101 — Infobox:noSwitch
L 1112 — Infobox.maxVersion
L 1182 — Infobox:cleanParams
L 1477 — Infobox:linkParams
L 1499 — Infobox:param
L 1575 — Infobox:paramDefined
L 1621 — Infobox:paramGrep
L 1686 — Infobox.paramRead
L 1752 — Infobox:categoryData
L 1757 — Infobox:addDropLevelVars
L 1813 — Infobox.tostring

Creating a template step-by-step

Import Module:Infobox

local infobox = require('Module:Infobox')

Unpack the frame arguments

function p.main(frame)
	local args = frame:getParent().args
	...

Define an Infobox object

Main article: Module:Infobox § new
	local infoobar = infobox.new(args)

Map your arguments to functions

	infoobar:defineParams {
			...
	}

Name your infobox

	-- note: we don't use the "Template:" namespace prefix
	infoobar:defineName('Infobox FooBar')

Give your infobox custom behaviour

	infoobar:addClass(...)
	infoobar:useSMW({ ... })
	infoobar:defineLinks({ ... })

Parse your arguments

	infoobar:cleanParams()

Initialise your HTML

	infoobar:create()

Give your infobox a caption

Or don't.

	infoobar:caption()

Format your table

	infoobar:addRow{ ... }
		:addRow{ ... }

Finalise your template

	infoobar:finish()

Functions

Public functions

For the example snippets below, template is an arbitrary infobox initiated as:

	local template = Infobox.new(args)

Priority

Certain functions rely on information created by other functions, and thus must be run in a particular order:

0
  • new
1
  • defineParams
2
  • cleanParams
  • setMaxButtons
  • noSwitch
3
  • create
  • maxVersion
4
  • other functions
5
  • finish

nilParam

	Infobox.nilParam()

Returns the value of the placeholder value Infobox.nil_param. This value is generally not seen, but it is used to fill switchbox data as nil causes unwanted behaviour.

isDefined

	Infobox.isDefined(arg)

Checks whether the value arg can be considered as defined.

new

	Infobox.new(args)

Creates a new infobox object with args as a table of values passed from the template invocation.

This function also creates the top-level wrapper and sets a metatable for the infobox.

create

	template:create()

Creates the HTML tags used for the infobox itself. Will run maxVersion() if it has not been run already.

defineName

	template:defineName(arg)

Defines the name of the infobox. This should be the base pagename; i.e. no "Template:" namespace prefix.

defineLinks

	template:defineLinks(tbl)

Defines any number of infobox bottom links in a table whose elements are table variables formatted as such:

	{
		{ link, label },
		...
	}

link and label should be string variables that will, in a basic sense, define [[Link|label]].

In a more technical sense, link and label help form a string for string.format(), with the module using the equivalent of (but not exactly):

	string.format('[[['..link..'|'..label..']]]',...)

The template name itself can be called up to 5 times by using "%s", in either link or label.

setMaxButtons

	template:setMaxButtons(n)

Changes the behaviour of buttons by defining the maximum number of buttons that are allowed to appear. If the version count exceeds the button count, their buttons will be replaced with a drop down menu. If not run, the maximum button count will be 5.

cleanParams

	template:cleanParams()

Parses the parameters with their mapped functions.

defineParams

	template:defineParams{
		...
	}

Maps parameters to functions as defined by a table formatted as such:

	{ name = <param>, func = <func>, dupes = true },

param should be a string that names the parameter as it will be in references for use in other functions.

func should be a function or instructions on how to find a function.

dupes is a meta configuration that allows the parameter to be duplicated in switch data. Normally, these duplicate values are not needed, and they will be deleted if they are the same as the default values after all parameters have been parsed. Some parameters require duplication to function properly, such as display altering parameters defined with linkParams().

If duplicates are not needed, this parameter should be deleted.

Predefined functions

If func is a string, then the module will attempt to use a predefined function.

Function Use
name Uses the parameter named "name". If "name" is blank or undefined, it will use the page name.
release Uses the parameters named "release" and "update" and formats it as such:
  • If both are defined: <release> ([[Update:<update>|Update]])
  • If only release is defined: <release> (Update unknown)
  • If release is not defined: ? (edit)
removal Uses the parameters named "removal" and "removalupdate" and formats it the same as release
hasContent If the parameter is defined and not blank, it will be used. Otherwise, ? (edit) will be used.
image Currently functions the same as hasContent
numbers Removes commas from the parameter and attempts to cast it to a number. If it works, the number is used, otherwise ? (edit)
makeChiselLinks Produces Chisel links (e.g. for use in an "Advanced data" section) based on given IDs, falling back to a name-based search if none provided.

Parameters should be passed in this order:

  1. Name of the person/object/thing
  2. Separator between links (e.g. <br>, &nbsp;&bull;&nbsp;, etc.)
  3. The type of link (e.g. bestiary, mrnd, etc.)
  4. The ID (or comma-separated list of IDs) for that link

The last two groups of parameters can be repeated if the given infobox supports multiple types of IDs.

User-defined functions

If func is a function, then that function will be used to parse the parameter. User-defined functions allow for more robust formatting and guidelines.

As a general practice, user-defined functions should be located under the main functions in the module. It is best to document these functions so that they may be changed by other editors if necessary.

In user-defined functions, circumstances where the edit button should be shown should return nil, which is automatically handled by the module.

Simple and complex definitions

Parameters may be mapped to functions in a straightforward manner by simply definining a name of or reference to a function, such as:

	template:defineParams{
		{ name = 'foo', func = 'hasContent' },
		{ name = 'bar', func = barFunc }
	}

Simple definitions only pass the parameter from the master list named <name>. Some parameters need more interaction with other parameters. To do this, we require a complex definition. Complex definitions point to a user-defined function and map what parameters to pass. Complex definitions can also pass literal values or the uncleaned value with the use of flags.

Complex functions are defined as table values formatted as such:

	func = { name = <funcname>, params = { <paramname>, ... }, flag = <flags> }

A basic example for complex functions is:

	template:defineParams{
		{ name = 'foo', func = { name = fooFunc, params = { 'bar', 'baz' } },
	}
	-- ...
	-- ...
	function fooFunc(arg1,arg2)
		return arg1..arg2
	end

In this example, we have a parameter named "foo", but we use the parameters passed to the template named "bar" and "baz". Parameters are passed in the order listed, so in the above, the parameter bar is used for arg1, and baz is used for arg2.

Flags define the behaviour of the parameters named. The following flag behaviours exist:

  • d - Looks for the cleaned and parsed version of the parameter. If not cleaned, it will use the value passed to the template. If neither exist, it will use nil. This is the default behaviour if no flag is defined.
  • p - Looks only at the value passed to the template. If it does not exist, it will use nil.
  • r or l - Uses the literal (or raw) value.

If flag is a string, then the behaviour defined by it will apply to every parameter passed. If flag is a table, then each flag will only define the behaviour of the respective parameter.

For example:

	template:defineParams{
		{ name = 'foo', func = {
					name = fooFunc,
					params = { 'foo', 'bar', 'baz' },
					flag = { 'd', 'p', 'l' },
				},
		}

In the above snippet, foo will use the default behaviour and bar will only look for the value passed to the template. The third parameter will use the string literal 'baz'.

Definition order

Parameters are defined in the order that they are coded. If a parameter relies on the cleaned value of another parameter, then the parameter dependent on the other will need to be defined after in the definition table.

addRow

	Infobox.addRow(template, tbl)
	-- or
	template:addRow(tbl)

Adds a new row to the template with columns and behaviour defined by tbl, which should be a table that holds cells defined as table variables, formatted as such:

	{
		{ celltype, label, <attr1> = <value1>, <attr2> = <value2> ... },
		...
	}

celltype and label are string values that define the fundamental display of the cell.

celltype Output
th Creates a <th> tag where the content is the value of label
td Creates a <td> tag where the content is the value of label
argh Creates a <th> tag where the content is the value of the parameter named label
argd Creates a <td> tag where the content is the value of the parameter named label

The attributes are any of the available attributes defined inside the function. All functions that are ultimately run are documented in the Lua reference manual and are run on the tag for the specific table cell they are called on.

attr Type Use
attr table Passes the value to mw.html.attr()
css table Passes the value to mw.html.css()
colspan number Uses the value to run mw.html.attr('colspan',#)
rowspan number Uses the value to run mw.html.attr('rowspan',#)
title string Uses the value to run mw.html.attr('title',text)
class string Passes the value to mw.html.addClass()
class table Passes every value in the table to mw.html.addClass()

If the template is a switch infobox, then data-attr-param="<paramname>" will be added to the table cell.

This function will return the template, allowing further self functions to be performed.

addButtonsRow

	Infobox.addButtonsRow(template, { colspan = 2 })
	-- or
	template:addButtonsRow({ colspan = 2 })
	-- or
	template:addButtonsRow{ colspan = 2 }

Places the switch infobox buttons, if there are any, where this is called. The row is not generated if there are no switch infobox buttons. Overrides default button placement (which is just above the table - though because there are no wrappers this usually means they hover in the center of the page). Set the colspan to the total colspan of the infobox.

wikitext

	Infobox.wikitext(template, txt)
	-- or
	template:wikitext(txt)

Appends wikitext to the top-level wrapper. Templates, etc. passed directly from Lua code will need explicit preprocessing prior to display properly.

This function will return the template, allowing further self functions to be performed.

caption

	Infobox.caption(template)
	-- or
	template:caption()

Adds a caption to the infobox based off the subject name, using the following priority for the text:

  • name parameter
  • name1 parameter (if switch infobox)
  • {{PAGENAME}}

If the template is a switch infobox, this will also add data-attr-param="name" to the caption.

This function will return the template, allowing further self functions to be performed.

addButtonsCaption

	Infobox.addButtonsRow(template)
	-- or
	template:addButtonsRow()

Places the switch infobox buttons, if there are any, into a caption. The caption is not generated if there are no switch infobox buttons. Overrides default button placement (which is just above the table - though because there are no wrappers this usually means they hover in the center of the page). The buttons are placed into a new caption - if used in conjunction with the above caption function, the new caption will be in the same relative position as the code (i.e. if caption is called before addButtonsCaption, the normal caption will be above the buttons caption.

attr

	Infobox.attr(template, arg)
	-- or
	template:attr(arg)

Passes arg to mw.html.attr().

This function will return the template, allowing further self functions to be performed.

float

	Infobox.float(template, dir)
	-- or
	template:float(dir)

Changes the direction of the CSS style float for the top level wrapper. By default, all infoboxes float right.

This function will return the template, allowing further self functions to be performed.

css

	Infobox.css(template, ...)
	-- or
	template:css(...)

Passes the arguments to mw.html.css().

This function will return the template, allowing further self functions to be performed.

addClass

	Infobox.addClass(template, arg)
	-- or
	template:addClass(arg)

Passes arg to mw.html.addClass().

This function will return the template, allowing further self functions to be performed.

addClasses

	Infobox.addClasses(template, ...)
	-- or
	template:attr(...)

Passes every argument to mw.html.addClass() individually.

This function will return the template, allowing further self functions to be performed.

tag

	Infobox.tag(template, arg)
	-- or
	template:tag(arg)

Passes arg to mw.html.tag().

This function will return the tag (not the template), allowing further self functions to be performed.

useSMW

	template:useSMW(tbl)

Tells the module to create properties for parameters, as well as defining those mappings with tbl, a table whose elements form an associated array formatted as such:

	{
		parameter = property,
		...
	}

When defined, properties will be defined for two separate properties: both "Property:<name>" and "Property:All <name>". The "All <name>" properties exist to create storage for all applicable values. Keep this in mind, as "Property:<name>" will only ever store the default value.

noSwitch

	template:noSwitch()

Forces the template to use only a single version, the default.

maxVersion

	Infobox.maxVersion(template)
	-- or
	template:maxVersion()

Returns the number of versions used by the infobox. When run the first time, this function will check and define the version count first. Subsequent calls will simply return the count.

linkParams

	template:linkParams{
			paramName = linkedParam,
			...
	}

Links two parameters where one parameter (paramName) is contents of a cell and linkedParam becomes classes of that cell's row. It will only have an effect on switch infoboxes. Both parameters will need to be defined in the infobox with definedParams. They should be functions of the parameter they operator on and include dupes = true in their definitions.

<div class="infobox-switch-resources hidden">
	<span data-attr-param="param">
		<span data-attr-index="0" data-addclass="linkedParam[d]">paramName[d]</span>
		<span data-attr-index="1" data-addclass="linkedParam[1]">paramName[1]</span>
		<span data-attr-index="2" data-addclass="linkedParam[2]">paramName[2]</span>
	</span>
</div>

From this, the switch infobox javascript will add the contents of data-addclass to class attribute of the row of the table when the infobox is switched. You will also need to define the classes you are using in global CSS.

If the parameter is a th or td element, the class is added to the parent tr. Otherwise, it is added to the element directly (e.g. caption element).


finish

Finalises the template and adds remaining required HTML.

param

	template:param(arg, flag)

Returns the value of the parameter named arg. The exact format and values are determined by the flag:

  • d or empty - default value of the parameter
  • f or full - all values of the parameter as a table
  • s or switches - table of all switch values
  • s# - switch value at index #
  • r - if defined, switch value 1, otherwise the default value

Note that this function returns the actual table used by the template. If a switch value is equal to Infobox.nil_param for flag s#, then nil will be returned instead; however, when calling specific indices from the tables returned by f or r, the value of Infobox.nil_param will be returned without extra parsing.

paramDefined

	template:paramDefined(arg, flag)

Looks to see if the parameter named arg is defined at any index, based on instructions from flag:

  • 0 or empty - default value of parameter
  • # - value of switch at index #
  • all - true if the parameter is defined with a default or at any switch value

paramGrep

	template:paramGrep(arg, query)

Performs a function or search on every possible value of the parameter named arg. query can be either a function, a string, or a number. If a match is found or the function query returns true, paramGrep will end its search.

If query is a function, that function must pass a single argument (the parameter), and return either true or false.

If query is a string, then that string will be compared to each value of the parameter. If they match exactly, disregarding casing, then true will be returned. To perform a pattern match on strings, you will need to define query as a function that returns a boolean value based on string.find or mw.ustring.find.

If query is a number (or any other type), then it will be compared to each value for a match.

Matches are only checked if the type of query matches the value of the parameter; e.g., it is not valid to search for a number (ex: 4) inside a string (ex: '455').

paramRead

	Infobox.paramRead(arg, query)

Performs the same function as paramGrep; however, instead of arg being a name of a parameter to look for, it is the table itself. Useful for functions where only the table of parameters is passed, but not the whole infobox.

categoryData

	template:categoryData()

Returns fundamental category data collected during Infobox:cleanParams().

Fields in the category data include:

  • one_defined - true if at least one possible value of the parameter is defined
  • all_defined - false if at least one possible value of the parameter is not defined

addDropLevelVars

Adds page-wide variables (accessible with the parser function #var or the lua extension mw.ext.VariablesLua) for use by drop tables. Can be placed at any point after cleanParams.

	template:addDropLevelVars(level_type, param_name)
  • level_type is the type of level, as used in the variable name, for example 'combat', 'thieving', etc.
  • param_name is the name of the parameter to assign into the variable, as set by defineParams.

Variables are generated for every version for which a versioned dropversion parameter is present, plus a default version if any versions are missing that parameter. The default parameter will collect all other values. If there are multiple infoboxes on the page (or the variable is already defined), it will merge the variable, and sort the values. Values are comma-separated.

The generated variables are under name DropLevel_{level_type}_{dropversion_name}. The default name for the default variable is DEFAULT, which can be overridden by setting an unversioned dropversion parameter.

Example names:

  • DropLevel_combat_DEFAULT
  • DropLevel_hunter_anachronia

The generated variables (name and value) are logged to the lua console - you can view it by previewing the page then expanding the the lua logs below the edit box.

Rules

Defined parameters

Parameters are to be considered "defined" if they meet all of the following criteria:

  • Is not nil
  • Contains at least one non-whitespace character (string.find(param,'%S')
  • Is not equal to Infobox.nil_param
  • Does not contain the string 'action=edit'