پودمان:Infobox

از امامت‌پدیا، دانشنامهٔ امامت و ولایت

This template is intended as a meta-template: a template used for constructing other templates. It is not meant for use directly in an article, but can be used on a one-off basis if required. See راهنما:جعبه اطلاعات for an introduction to infoboxes and information on how to design them. See رده:الگوهای اطلاعاتی جعبه‌ای for specific infobox templates.

Usage

Usage is similar to {{navbox}}, but with an additional distinction. Each row on the table can contain either a header, or a label/data pair, or just a data cell. These are mutually exclusive states so if you define a row with both a header and a label/data pair, the label/data pair is ignored.

To insert an image somewhere other than at the top of the infobox, or to insert freeform data, use a row with only a data field.

Optional control parameters

name
If this parameter is present, "view/discuss/edit" links will be added to the bottom of the infobox, pointing to the named page. You may use the value {{subst:PAGENAME}}; however this is rarely what you want, because it will send users clicking these links in an infobox in an article to the template code rather than the data in the infobox that they probably want to change.
child
See the Embedding section for details. If this is set to "yes", this child infobox should be titled but have no name parameter. This parameter is empty by default, set it to "yes" to activate it.
subbox
See the Subboxes section for details. If this is set to "yes", this subbox should be titled but have no name parameter. This parameter is empty by default, set to "yes" to activate it. It has no effect if the child parameter is also set to "yes".
decat
If this is set to "yes", the current page will not be autocategorized in a maintenance category when the generated infobox has some problems or no visible data section. Leave empty by default or set to "yes" to activate it.

Content parameters

Title

There are two different ways to put a title on an infobox. One contains the title inside the infobox's border in the uppermost cell of the table, the other puts as a caption it on top of the table. You can use both of them together if you like, or just one or the other, or even neither (though this is not recommended):

title
Text to put in the caption over the top of the table (or as section header before the whole content of this table, if this is a child infobox).
above
Text to put within the uppermost cell of the table.
subheader(n)
additional title fields which fit below {{{title}}} and {{{above}}}, but before images. Subheader parameters should not be numbered more than 10 apart.

Examples:

Text in caption over infobox
Subheader of the infobox
{{Infobox
 | name = {{subst:PAGENAME}}
 | title      = Text in caption over infobox
 | subheader  = Subheader of the infobox
 | header = (the rest of the infobox goes here)
}}
Text in uppermost cell of infobox
Subheader of the infobox
Second subheader of the infobox
{{Infobox
 | name = {{subst:PAGENAME}}
 | above      = Text in uppermost cell of infobox
 | subheader  = Subheader of the infobox
 | subheader2 = Second subheader of the infobox
 | header = (the rest of the infobox goes here)
}}

Illustration images

image(n)
images to display at the top of the template. Use full image syntax, for example [[File:example.png|200px|alt=Example alt text]]. Image is centered by default. See ویکی‌پدیا:متن جایگزین برای تصاویر for more on alt text. Image parameters should not be numbered more than 10 apart.
caption(n)
Text to put underneath the images.

Main data

header(n)
Text to use as a header in row n. Header and data parameters should not be numbered more than 50 apart.
label(n)
Text to use as a label in row n.
data(n)
Text to display as data in row n. Header and data parameters should not be numbered more than 50 apart.

Note: for any given value for (n), not all combinations of parameters are permitted. The presence of a |header(n)= will cause the corresponding |data(n)= (and |rowclass(n)= |label(n)=, see below) to be ignored; the absence of a |data(n)= will cause the corresponding |label(n)= to be ignored. Valid combinations for any single row are:

  • |class(n)= |header(n)=
  • |rowclass(n)= |class(n)= |data(n)=
  • |rowclass(n)= |label(n)= |class(n)= |data(n)=

See the rendering of header4, label4, and data4 in the Examples section below.

Number ranges

To allow flexibility when the layout of an infobox is changed, it may be helpful when developing an infobox to use non-contiguous numbers for header and label/data rows. Parameters for new rows can then be inserted in future without having to renumber existing parameters. For example:

 | header3  = Section 1
 |  label5  = Label A
 |   data5  = Data A
 |  label7  = Label C
 |   data7  = Data C
 | header10 = Section 2
 |  label12 = Label D
 |   data12 = Data D

It is also possible to automatically renumber parameter names by using پودمان:IncrementParams.

Note that although there is no limitation on the number of rows available, row parameters should not be numbered more than 50 apart. See the #Limitations section below for more details.

Making data fields optional

A row with a label but no data is not displayed. This allows for the easy creation of optional infobox content rows. To make a row optional use a parameter that defaults to an empty string, like so:

 |  label5 = Population
 |   data5 = {{{population|}}}

This way if an article doesn't define the population parameter in its infobox the row won't be displayed.

For more complex fields with pre-formatted contents that would still be present even if the parameter wasn't set, you can wrap it all in an "#if" statement to make the whole thing vanish when the parameter is not used. For instance, the "#if" statement in the following example reads "#if:the parameter mass has been supplied |then display it, followed by 'kg'":

 |  label6 = Mass
 |   data6 = {{ #if: {{{mass|}}} | {{{mass}}} kg }}

For more on #if, see here.

Hiding headers when all data fields are hidden

You can also make headers optional in a similar way. Consider this example:

Example of an undesirable header
Undesirable header
Static header
Static itemStatic value
{{Infobox
 | title   = Example of an undesirable header
 | header1 = Undesirable header
 |  label2 = Item 1      |   data2 =
 |  label3 = Item 2      |   data3 =
 |  label4 = Item 3      |   data4 =
 | header5 = Static header
 |  label6 = Static item |   data6 = Static value
}}

If you want the first header to appear only if one or more of the data fields that fall under it are filled, one could use the following pattern as an example of how to do it:

Example of an optional header
Static header
Static itemStatic value
{{Infobox
 | title   = Example of an optional header
 | header1 = {{ #if: {{{item1|}}}{{{item2|}}}{{{item3|}}} | Optional header }}
 |  label2 = Item 1      |   data2 = {{{item1|}}}
 |  label3 = Item 2      |   data3 = {{{item2|}}}
 |  label4 = Item 3      |   data4 = {{{item3|}}}
 | header5 = Static header
 |  label6 = Static item |   data6 = Static value
}}

header1 will be shown if any of item1, item2, or item3 is defined. If none of the three parameters are defined the header won't be shown and no emty row appears before the next static content. The trick to this is that the "#if" returns false only if there is nothing whatsoever in the conditional section, so only if all three of item1, item2 and item3 are undefined will the if statement fail.

Note that such trick may be sometimes very complex to test if there are many data items whose value depends on complex tests (or when a data row is generated by a recursive invokation of this template as a subbox). Ideally, the Lua module supporting this template should now support a new way to make each header row autohideable by detecting if there is at least one non-empty data row after that header row (a parameter like "autohide header1 = yes", for example, would remove the need to perform the "#if" test so that we can just to define "header1 = Optional header"),

Footer

below
Text to put in the bottom cell. The bottom cell is intended for footnotes, see-also, and other such information.

Presentation parameters

Italic titles

Titles of articles with infoboxes may be made italic, in line with ویکی‌پدیا:قواعد نام‌گذاری, by passing the italic title parameter.

  • Turn on italic titles by passing |italic title={{{italic title|}}} from the infobox.
  • Turn off by default (notably because only Latin script may be safely rendered in this style and italic may be needed to distinguish foreign language from local English language only in that script, but would be difficult to read for other scripts) but allow some instances to be made italic by passing |italic title={{{italic title|no}}}
  • Do not make any titles italic by not passing the parameter at all.

CSS styling

bodystyle
Applies to the infobox table as a whole
titlestyle
Applies only to the title caption. Adding a background color is usually inadvisable since the text is rendered "outside" the infobox.
abovestyle
Applies only to the "above" cell at the top. The default style has font-size:125%; since this cell is usually used for a title, if you want to use the above cell for regular-sized text include "font-size:100%;" in the abovestyle.
imagestyle
Applies to the cell the image is in. This includes the text of the image caption, but you should set text properties with captionstyle instead of imagestyle in case the caption is moved out of this cell in the future.
captionstyle
Applies to the text of the image caption.
headerstyle
Applies to all header cells
labelstyle
Applies to all label cells
datastyle
Applies to all data cells
belowstyle
Applies only to the below cell

HTML classes and microformats

bodyclass
This parameter is inserted into the class attribute for the infobox as a whole.
titleclass
This parameter is inserted into the class attribute for the infobox's title caption.
aboveclass
This parameter is inserted into the class attribute for the infobox's above cell.
subheaderrowclass(n)
This parameter is inserted into the class attribute for the complete table row the subheader is on.
subheaderclass(n)
This parameter is inserted into the class attribute for the infobox's subheader.
imagerowclass(n)
These parameters are inserted into the class attribute for the complete table row their respective image is on.
imageclass
This parameter is inserted into the class attribute for the image.
rowclass(n)
This parameter is inserted into the class attribute for the specified row including the label and data cells.
class(n)
This parameter is inserted into the class attribute for the data cell of the specified row. If there's no data cell it has no effect.
belowclass
This parameter is inserted into the class attribute for the infobox's below cell.

This template supports the addition of microformat information. This is done by adding "class" attributes to various data cells, indicating what kind of information is contained within. Multiple class names may be specified, separated by spaces, some of them being used as selectors for custom styling according to a project policy or to the skin selected in user preferences, others beig used for microformats.

To flag an infobox as containing hCard information, for example, add the following parameter:

 | bodyclass = vcard

And for each row containing a data cell that's part of the vcard, add a corresponding class parameter:

 | class1 = fn
 | class2 = org
 | class3 = tel

...and so forth. "above" and "title" can also be given classes, since these are usually used to display the name of the subject of the infobox.

See Wikipedia:WikiProject Microformats for more information on adding microformat information to Wikipedia, and microformat for more information on microformats in general.

Examples

Notice how the row doesn't appear in the displayed infobox when a label is defined without an accompanying data cell, and how all of them are displayed when a header is defined on the same row as a data cell. Also notice that subheaders are not bold by default like the headers used to split the main data section, because this role is meant to be for the above cell :

Test Infobox
Above text
Subheader above image
Second subheader
Example alt text
Caption displayed below example.png
Header defined alone
Data defined alone
All three defined (header, label, data, all with same number)
Label and data defined (label)Label and data defined (data)
Below text
{{Infobox
 |name         = {{subst:PAGENAME}}
 |bodystyle    = 

 |titlestyle   = 
 |abovestyle = background:#cfc;
 |subheaderstyle =
 |title        = Test Infobox
 |above        = Above text
 |subheader    = Subheader above image
 |subheader2   = Second subheader

 |imagestyle   = 
 |captionstyle = 
 |  image      = [[File:example.png|200px|alt=Example alt text]]
 |caption      = Caption displayed below example.png

 |headerstyle  = background:#ccf;
 |labelstyle   = background:#ddf;
 |datastyle    = 

 |header1 = Header defined alone
 | label1 = 
 |  data1 = 
 |header2 = 
 | label2 = Label defined alone does not display (needs data, or is suppressed)
 |  data2 = 
 |header3 =
 | label3 = 
 |  data3 = Data defined alone
 |header4 = All three defined (header, label, data, all with same number)
 | label4 = does not display (same number as a header)
 |  data4 = does not display (same number as a header)
 |header5 =
 | label5 = Label and data defined (label)
 |  data5 = Label and data defined (data)

 |belowstyle = background:#ddf;
 |below      = Below text
}}

For this example, the bodystyle and labelstyle parameters are used to adjust the infobox width and define a default width for the column of labels:

Test Infobox
Label 1Data 1
Label 2Data 2
Label 3Data 3
Header 4
Label 5Data 5: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Below text
{{Infobox
 |name        = {{subst:PAGENAME}}
 |bodystyle   = width:20em

 |titlestyle  = 
 |title       = Test Infobox

 |headerstyle = 
 |labelstyle  = width:33%
 |datastyle   = 

 |header1 = 
 | label1 = Label 1
 |  data1 = Data 1
 |header2 = 
 | label2 = Label 2
 |  data2 = Data 2
 |header3 = 
 | label3 = Label 3
 |  data3 = Data 3
 |header4 = Header 4
 | label4 = 
 |  data4 = 
 |header5 = 
 | label5 = Label 5
 |  data5 = Data 5: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

 |belowstyle = 
 |below = Below text
}}

Embedding

One infobox template can be embedded into another using the |child= parameter. This feature can be used to create a modular infobox, or to create more well defined logical sections. Previously, it was necessary to use embedding in order to create infoboxes with more than 99 rows, but there is now no limit to the number of rows that can be defined in a single instance of {{infobox}}.

Top level title
First subsection
Label 1.1Data 1.1
Second subsection
Label 2.1Data 2.1
Below text
{{Infobox
 | title = Top level title
 | data1 = {{Infobox | decat = yes | child = yes
    | title = First subsection
    | label1= Label 1.1
    | data1 = Data 1.1
   }}
 | data2 = {{Infobox | decat = yes | child = yes
  |title = Second subsection
  | label1= Label 2.1
  | data1 = Data 2.1
  }}
 | belowstyle = 
 | below = Below text
}}

Note, in the examples above, the child infobox is placed in a data field, not a header field. Notice that the section subheadings are not in bold font if bolding is not explicitly specified. To obtain bold section headings, place the child infobox in a header field (but not in a label field because it would not be displayed!), either using

Top level title
First subsection
Label 1.1Data 1.1
Second subsection
Label 2.1Data 2.1
Below text
{{Infobox
 | title = Top level title
 | header1 = {{Infobox | decat = yes | child = yes
    | title = First subsection
    | label1= Label 1.1
    | data1 = Data 1.1
   }}
 | header2 = {{Infobox | decat = yes | child = yes
    | title = Second subsection
    | label1= Label 2.1
    | data1 = Data 2.1
   }}
 | belowstyle = 
 | below = Below text
}}

or,

Top level title
First subsection
Label 1.1Data 1.1
Second subsection
Label 2.1Data 2.1
Below text
{{Infobox
 | title = Top level title
 | header1 = First subsection
   {{Infobox | decat = yes | child = yes
    | label1 = Label 1.1
    |   data1 = Data 1.1
   }}
 | header2 = Second subsection
   {{Infobox | decat = yes | child = yes
    | label1 = Label 2.1
    |  data1 = Data 2.1
   }}
 | belowstyle = 
 | below = Below text
}}

Note that omitting the |title= parameter, and not including any text preceding the embedded infobox, may result in spurious blank table rows, creating gaps in the visual presentation.

Subboxes

An alternative method for embedding is to use |subbox=yes, which removes the outer border from the infobox, but preserves the interior structure. One feature of this approach is that the parent and child boxes need not have the same structure, and the label and data fields are not aligned between the parent and child boxes because they are not in the same parent table.

Main 1
Main 2
Sub 3-1
Sub 3-2
Label 3-3Data 3-3
Label 4-1Data 4-1
Label 5Data 5
Main 6
{{Infobox
 | headerstyle = background-color:#eee;
 | labelstyle  = background-color:#eee;
 | header1 = Main 1
 | header2 = Main 2
 |   data3 = {{Infobox | subbox = yes
    | headerstyle = background-color:#ccc;
    | labelstyle  = background-color:#ddd;
    | header1 = Sub 3-1
    | header2 = Sub 3-2
    |  label3 = Label 3-3    |   data3 = Data 3-3
   }}
 |   data4 = {{Infobox | subbox = yes
    | labelstyle  = background-color:#ccc;
    |  label1 = Label 4-1    |   data1 = Data 4-1
   }}
 |  label5 = Label 5 |   data5 = Data 5
 | header6 = Main 6
}}

Similar embedding technics may be used within content parameters of some other templates generating tables (such as Sidebar) :

{{نوار جانبی
 | navbar = off
 | headingstyle = background-color:#eee;
 | heading1 = Heading 1
 | heading2 = Heading 2
 | content3 = {{Infobox | subbox = yes
    | headerstyle = background-color:#ccc;
    | labelstyle = background-color:#ddd;
    | header1 = Sub 3-1
    | header2 = Sub 3-2
    |  label3 = Label 3-3 |   data3 = Data 3-3
   }}
 | content4 = {{Infobox | subbox = yes
    | labelstyle = background-color:#ccc;
    |  label1 = Label 4-1 |   data1 = Data 4-1
   }}
 | heading5 = Heading 5
}}

Note that the default padding of the parent data cell containing each subbox is still visible, so the subboxes are slightly narrower than the parent box and there's a higher vertical spacing between standard cells of the parent box than between cells of distinct subboxes.

Full blank syntax

(Note: there is no limit to the number of possible rows; only 20 are given below since infoboxes larger than that will be relatively rare. Just extend the numbering as needed. The microformat "class" parameters are also omitted as they are not commonly used.)

{{Infobox
| name           = {{subst:PAGENAME}}
| child          = {{{child|}}}
| subbox         = {{{subbox|}}}
| italic title   = {{{italic title|no}}}
| bodystyle      = 

| titlestyle     = 
| abovestyle     = 
| subheaderstyle = 
| title          = 
| above          = 
| subheader      = 

|   imagestyle   = 
| captionstyle   = 
|   image        = 
| caption        = 
|   image2       = 
| caption2       = 

| headerstyle    = 
|  labelstyle    = 
|   datastyle    = 
| header1  = 
|  label1  = 
|   data1  = 
| header2  = 
|  label2  = 
|   data2  = 
| header3  = 
|  label3  = 
|   data3  = 
| header4  = 
|  label4  = 
|   data4  = 
| header5  = 
|  label5  = 
|   data5  = 
| header6  = 
|  label6  = 
|   data6  = 
| header7  = 
|  label7  = 
|   data7  = 
| header8  = 
|  label8  = 
|   data8  = 
| header9  = 
|  label9  = 
|   data9  = 
| header10 = 
|  label10 = 
|   data10 = 
| header11 = 
|  label11 = 
|   data11 = 
| header12 = 
|  label12 = 
|   data12 = 
| header13 = 
|  label13 = 
|   data13 = 
| header14 = 
|  label14 = 
|   data14 = 
| header15 = 
|  label15 = 
|   data15 = 
| header16 = 
|  label16 = 
|   data16 = 
| header17 = 
|  label17 = 
|   data17 = 
| header18 = 
|  label18 = 
|   data18 = 
| header19 = 
|  label19 = 
|   data19 = 
| header20 = 
|  label20 = 
|   data20 = 

| belowstyle     =
| below          = 
}}

Limitations

Previously, the number of rows available to this template was limited to 99. This restriction has been lifted now that the template has been ported to a Lua module, but to maintain backwards compatibility there is now a limit on the number of rows apart by which parameters will be detected. If parameters are numbered more than 50 apart, the later parameters may be ignored (depending on the exact numbers involved), and if parameters are numbered more than 100 apart, the later parameters will definitely be ignored.

For example, this code will display properly:

Section 1
Label AData A
Data B
Label CData C
Section 2
Label DData D
{{Infobox
 | header3   = Section 1
 |  label48  = Label A
 |   data48  = Data A
 |   data92  = Data B
 |  label136 = Label C
 |   data136 = Data C
 | header177 = Section 2
 |  label215 = Label D
 |   data215 = Data D
}}

But in this code, only Section 1, Label A and Data A will be displayed, because Data B is numbered too far away from Data A (and then all the rest is ignored):

Section 1
Label AData A
{{Infobox
 | header3   = Section 1
 |  label48  = Label A
 |   data48  = Data A
 |   data102 = Data B
 |  label103 = Label C
 |   data103 = Data C
 | header115 = Section 2
 |  label120 = Label D
 |   data120 = Data D
}}

Note that parameter values may be blank - parameters are counted unless they are completely absent, in header() and data(n), from the template invocation:

Section 1
Label AData A
Data B
Label CData C
Section 2
Label DData D
{{Infobox
 | header3   = Section 1
 |  label48  = Label A
 |   data48  = Data A
 |   data70  =
 |   data102 = Data B
 |  label103 = Label C
 |   data103 = Data C
 | header115 = Section 2
 |  label120 = Label D
 |   data120 = Data D
}}

If only label(n) is specified (even with a non-empty value) but neither header(n) nor data(n), the parameter is ignored and considered as if it was missing:

Section 1
Label AData A
{{Infobox
 | header3   = Section 1
 |  label48  = Label A
 |   data48  = Data A
 |  label70  = Dummy label
 |   data102 = Data B
 |  label103 = Label C
 |   data103 = Data C
 | header115 = Section 2
 |  label120 = Label D
 |   data120 = Data D
}}

There are also similar limitations for image(n) and subheader(n) parameters: in those cases, parameters should not be numbered more than 10 apart.

Porting to other MediaWikis

The infobox template requires the Scribunto extension and HTMLTidy to be installed. It may not work with other MediaWikis. WikiProject Transwiki has a version of this template that has been modified to work on other MediaWikis.

See also



local p = {}
local args = {}
local origArgs = {}
local root
local empty_row_categories = {}
local category_in_empty_row_pattern = '%[%[%s*رده%s*:[^]]*]]'

local function fixChildBoxes(sval, tt)
	local function notempty( s ) return s and s:match( '%S' ) end
	
	if notempty(sval) then
		local marker = '<span class=special_infobox_marker>'
		local s = sval
		s = mw.ustring.gsub(s, '(<%s*[Tt][Rr])', marker .. '%1')
		s = mw.ustring.gsub(s, '(</[Tt][Rr]%s*>)', '%1' .. marker)
		if s:match(marker) then
			s = mw.ustring.gsub(s, marker .. '%s*' .. marker, '')
			s = mw.ustring.gsub(s, '([\r\n]|-[^\r\n]*[\r\n])%s*' .. marker, '%1')
			s = mw.ustring.gsub(s, marker .. '%s*([\r\n]|-)', '%1')
			s = mw.ustring.gsub(s, '(</[Cc][Aa][Pp][Tt][Ii][Oo][Nn]%s*>%s*)' .. marker, '%1')
			s = mw.ustring.gsub(s, '(<%s*[Tt][Aa][Bb][Ll][Ee][^<>]*>%s*)' .. marker, '%1')
			s = mw.ustring.gsub(s, '^(%{|[^\r\n]*[\r\n]%s*)' .. marker, '%1')
			s = mw.ustring.gsub(s, '([\r\n]%{|[^\r\n]*[\r\n]%s*)' .. marker, '%1')
			s = mw.ustring.gsub(s, marker .. '(%s*</[Tt][Aa][Bb][Ll][Ee]%s*>)', '%1')
			s = mw.ustring.gsub(s, marker .. '(%s*\n|%})', '%1')
		end
		if s:match(marker) then
			local subcells = mw.text.split(s, marker)
			s = ''
			for k = 1, #subcells do
				if k == 1 then
					s = s .. subcells[k] .. '</' .. tt .. '></tr>'
				elseif k == #subcells then
					local rowstyle = ' style="display:none"'
					if notempty(subcells[k]) then rowstyle = ''	end
					s = s .. '<tr' .. rowstyle ..'><' .. tt .. ' colspan=2>\n' ..
						subcells[k]
				elseif notempty(subcells[k]) then
					if (k % 2) == 0 then
						s = s .. subcells[k]
					else
						s = s .. '<tr><' .. tt .. ' colspan=2>\n' ..
							subcells[k] .. '</' .. tt .. '></tr>'
					end
				end
			end
		end
		-- the next two lines add a newline at the end of lists for the PHP parser
		-- [[Special:Diff/849054481]]
		-- remove when [[:phab:T191516]] is fixed or OBE
		s = mw.ustring.gsub(s, '([\r\n][%*#;:][^\r\n]*)$', '%1\n')
		s = mw.ustring.gsub(s, '^([%*#;:][^\r\n]*)$', '%1\n')
		s = mw.ustring.gsub(s, '^([%*#;:])', '\n%1')
		s = mw.ustring.gsub(s, '^(%{%|)', '\n%1')
		return s
	else
		return sval
	end
end

-- Returns the union of the values of two tables, as a sequence.
local function union(t1, t2)

	local vals = {}
	for k, v in pairs(t1) do
		vals[v] = true
	end
	for k, v in pairs(t2) do
		vals[v] = true
	end
	local ret = {}
	for k, v in pairs(vals) do
		table.insert(ret, k)
	end
	return ret
end

-- Returns a table containing the numbers of the arguments that exist
-- for the specified prefix. For example, if the prefix was 'data', and
-- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
local function getArgNums(prefix)
	local nums = {}
	for k, v in pairs(args) do
		local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
		if num then table.insert(nums, tonumber(num)) end
	end
	table.sort(nums)
	return nums
end

-- Adds a row to the infobox, with either a header cell
-- or a label/data cell combination.
local function addRow(rowArgs)
	
	if rowArgs.header and rowArgs.header ~= '_BLANK_' then
		root
			:tag('tr')
				:addClass(rowArgs.rowclass)
				:cssText(rowArgs.rowstyle)
				:tag('th')
					:attr('colspan', '2')
					:addClass('infobox-header')
					:addClass(rowArgs.class)
					:addClass(args.headerclass)
					:css('text-align', 'center')
					-- @deprecated next; target .infobox-<name> .infobox-header
					:cssText(args.headerstyle)
					:cssText(rowArgs.rowcellstyle)
					:wikitext(fixChildBoxes(rowArgs.header, 'th'))
		if rowArgs.data then
			root:wikitext(
				'[[رده:صفحه‌هایی که از الگوهای جعبه اطلاعات با سلول‌های داده نادیده گرفته‌شده استفاده می‌کنند]]'
			)
		end
	elseif rowArgs.data and rowArgs.data:gsub(
		category_in_empty_row_pattern, ''
		):match('^%S') then
		local row = root:tag('tr')
		row:addClass(rowArgs.rowclass)
		row:cssText(rowArgs.rowstyle)
		if rowArgs.label then
			row
				:tag('th')
					:attr('scope', 'row')
					:addClass('infobox-label')
					-- @deprecated next; target .infobox-<name> .infobox-label
					:cssText(args.labelstyle)
					:cssText(rowArgs.rowcellstyle)
					:wikitext(rowArgs.label)
					:done()
		end

		local dataCell = row:tag('td')
		dataCell
			:attr('colspan', not rowArgs.label and '2' or nil)
			:addClass(not rowArgs.label and 'infobox-full-data' or 'infobox-data')
			:addClass(rowArgs.class)
			-- @deprecated next; target .infobox-<name> .infobox(-full)-data
			:cssText(rowArgs.datastyle)
			:cssText(rowArgs.rowcellstyle)
			:wikitext(fixChildBoxes(rowArgs.data, 'td'))
	else
		table.insert(empty_row_categories, rowArgs.data or '')
	end
end

local function renderTitle()
	if not args.title then return end

	root
		:tag('caption')
			:addClass('infobox-title')
			:addClass(args.titleclass)
			-- @deprecated next; target .infobox-<name> .infobox-title
			:cssText(args.titlestyle)
			:wikitext(args.title)
end

local function renderAboveRow()
	if not args.above then return end

	root
		:tag('tr')
			:tag('th')
				:attr('colspan', '2')
				:addClass('infobox-above')
				:addClass(args.aboveclass)
				:css('text-align', 'center')
				-- @deprecated next; target .infobox-<name> .infobox-above
				:cssText(args.abovestyle)
				:wikitext(fixChildBoxes(args.above,'th'))
end

local function renderBelowRow()
	if not args.below then return end

	root
		:tag('tr')
			:tag('td')
				:attr('colspan', '2')
				:addClass('infobox-below')
				:addClass(args.belowclass)
				:css('text-align', 'center')
				-- @deprecated next; target .infobox-<name> .infobox-below
				:cssText(args.belowstyle)
				:wikitext(fixChildBoxes(args.below,'td'))
end

local function addSubheaderRow(subheaderArgs)
	if subheaderArgs.data and
		subheaderArgs.data:gsub(category_in_empty_row_pattern, ''):match('^%S') then
		local row = root:tag('tr')
		row:addClass(subheaderArgs.rowclass)

		local dataCell = row:tag('td')
		dataCell
			:attr('colspan', '2')
			:addClass('infobox-subheader')
			:addClass(subheaderArgs.class)
			:cssText(subheaderArgs.datastyle)
			:cssText(subheaderArgs.rowcellstyle)
			:wikitext(fixChildBoxes(subheaderArgs.data, 'td'))
	else
		table.insert(empty_row_categories, subheaderArgs.data or '')
	end
end

local function renderSubheaders()
	if args.subheader then
		args.subheader1 = args.subheader
	end
	if args.subheaderrowclass then
		args.subheaderrowclass1 = args.subheaderrowclass
	end
	local subheadernums = getArgNums('subheader')
	for k, num in ipairs(subheadernums) do
		addSubheaderRow({
			data = args['subheader' .. tostring(num)],
			-- @deprecated next; target .infobox-<name> .infobox-subheader
			datastyle = args.subheaderstyle,
			rowcellstyle = args['subheaderstyle' .. tostring(num)],
			class = args.subheaderclass,
			rowclass = args['subheaderrowclass' .. tostring(num)]
		})
	end
end

local function addImageRow(imageArgs)

	if imageArgs.data and
		imageArgs.data:gsub(category_in_empty_row_pattern, ''):match('^%S') then
		
		local row = root:tag('tr')
		row 
			:addClass(imageArgs.rowclass)
			:css('text-align', 'center')

		local dataCell = row:tag('td')
		dataCell
			:attr('colspan', '2')
			:addClass('infobox-image')
			:addClass(imageArgs.class)
			:css('text-align', 'center')
			:cssText(imageArgs.datastyle)
			:wikitext(fixChildBoxes(imageArgs.data, 'td'))
	else
		table.insert(empty_row_categories, imageArgs.data or '')
	end
end

local function renderImages()
	if args.image then
		args.image1 = args.image
	end
	if args.caption then
		args.caption1 = args.caption
	end
	local imagenums = getArgNums('image')
	for k, num in ipairs(imagenums) do
		local caption = args['caption' .. tostring(num)]
		local data = mw.html.create():wikitext(args['image' .. tostring(num)])
		if caption then
			data
				:tag('div')
					:addClass('infobox-caption')
					-- @deprecated next; target .infobox-<name> .infobox-caption
					:cssText(args.captionstyle)
					:wikitext(caption)
		end
		addImageRow({
			data = tostring(data),
			-- @deprecated next; target .infobox-<name> .infobox-image
			datastyle = args.imagestyle,
			class = args.imageclass,
			rowclass = args['imagerowclass' .. tostring(num)]
		})
	end
end

-- When autoheaders are turned on, preprocesses the rows
local function preprocessRows()
	if not args.autoheaders then return end
	
	local rownums = union(getArgNums('header'), getArgNums('data'))
	table.sort(rownums)
	local lastheader
	for k, num in ipairs(rownums) do
		if args['header' .. tostring(num)] then
			if lastheader then
				args['header' .. tostring(lastheader)] = nil
			end
			lastheader = num
		elseif args['data' .. tostring(num)] and
			args['data' .. tostring(num)]:gsub(
				category_in_empty_row_pattern, ''
			):match('^%S') then
			local data = args['data' .. tostring(num)]
			if data:gsub(category_in_empty_row_pattern, ''):match('%S') then
				lastheader = nil
			end
		end
	end
	if lastheader then
		args['header' .. tostring(lastheader)] = nil
	end
end

-- Gets the union of the header and data argument numbers,
-- and renders them all in order
local function renderRows()

	local rownums = union(getArgNums('header'), getArgNums('data'))
	table.sort(rownums)
	for k, num in ipairs(rownums) do
		addRow({
			header = args['header' .. tostring(num)],
			label = args['label' .. tostring(num)],
			data = args['data' .. tostring(num)],
			datastyle = args.datastyle,
			class = args['class' .. tostring(num)],
			rowclass = args['rowclass' .. tostring(num)],
			-- @deprecated next; target .infobox-<name> rowclass
			rowstyle = args['rowstyle' .. tostring(num)],
			rowcellstyle = args['rowcellstyle' .. tostring(num)]
		})
	end
end

local function renderNavBar()
	if not args.name then return end

	root
		:tag('tr')
			:tag('td')
				:attr('colspan', '2')
				:addClass('infobox-navbar')
				:css('align', 'center')
				:wikitext(require('Module:Navbar')._navbar{
					args.name,
					mini = 1,
				})
end

local function renderItalicTitle()
	local italicTitle = args['italic title'] and mw.ustring.lower(args['italic title'])
	if italicTitle == '' or italicTitle == 'force' or italicTitle == 'yes' then
		root:wikitext(mw.getCurrentFrame():expandTemplate({title = 'italic title'}))
	end
end

-- Categories in otherwise empty rows are collected in empty_row_categories.
-- This function adds them to the module output. It is not affected by
-- args.decat because this module should not prevent module-external categories
-- from rendering.
local function renderEmptyRowCategories()
	for _, s in ipairs(empty_row_categories) do
		root:wikitext(s)
	end
end

-- Render tracking categories. args.decat == turns off tracking categories.
local function renderTrackingCategories()
	if args.decat == 'yes' then return end
	if args.child == 'yes' then
		if args.title then
			root:wikitext(
				'[[رده:صفحه‌هایی که از الگوهای جعبه اطلاعات جاسازی‌شده با پارامتر عنوان استفاده می‌کنند]]'
			)
		end
	elseif #(getArgNums('data')) == 0 and mw.title.getCurrentTitle().namespace == 0 then
		root:wikitext('[[رده:مقاله‌هایی که از الگوهای جعبه اطلاعات بدون ردیف‌های داده استفاده می‌کنند]]')
	end
end

--[=[
Loads the templatestyles for the infobox.

TODO: load base templatestyles here rather than in MediaWiki:Common.css
We aren't doing it here yet because there are 4-5000 pages with 'raw' infobox
tables. See [[Mediawiki_talk:Common.css/to_do#Infobox]] and/or come help :).
When we do this we should clean up the inline CSS below too.
Will have to do some bizarre conversion category like with sidebar.
	
]=]
local function loadTemplateStyles()
	local frame = mw.getCurrentFrame()
	
-- See function description
--	local base_templatestyles = frame:extensionTag{
--		name = 'templatestyles', args = { src = cfg.i18n.templatestyles }
--	}
	
	local templatestyles = ''
	if args['templatestyles'] then templatestyles = frame:extensionTag{
			name = 'templatestyles', args = { src = args['templatestyles'] }
		}
	end
	
	local child_templatestyles = ''
	if args['child templatestyles'] then child_templatestyles = frame:extensionTag{
			name = 'templatestyles', args = { src = args['child templatestyles'] }
		}
	end
	
	local grandchild_templatestyles = ''
	if args['grandchild templatestyles'] then grandchild_templatestyles = frame:extensionTag{
			name = 'templatestyles', args = { src = args['grandchild templatestyles'] }
		}
	end

	return table.concat({
--		base_templatestyles, -- see function description
		templatestyles,
		child_templatestyles,
		grandchild_templatestyles
	})	
	
end

-- Specify the overall layout of the infobox, with special settings if the
-- infobox is used as a 'child' inside another infobox.
local function _infobox()
	if args.child ~= 'yes' then
		root = mw.html.create('table')

		root
			:addClass(args.subbox == 'yes' and 'infobox-subbox' or 'infobox')
			:addClass(args.bodyclass)
			-- @deprecated next; target .infobox-<name>
			:cssText(args.bodystyle)

		renderTitle()
		renderAboveRow()
	else
		root = mw.html.create()

		root
			:wikitext(args.title)
	end

	renderSubheaders()
	renderImages()
	preprocessRows()
	renderRows()
	renderBelowRow()
	renderNavBar()
	renderItalicTitle()
	renderEmptyRowCategories()
	renderTrackingCategories()

	return loadTemplateStyles() .. tostring(root)
end

-- If the argument exists and isn't blank, add it to the argument table.
-- Blank arguments are treated as nil to match the behaviour of ParserFunctions.
local function preprocessSingleArg(argName)
	if origArgs[argName] and origArgs[argName] ~= '' then
		args[argName] = origArgs[argName]
	end
end

-- Assign the parameters with the given prefixes to the args table, in order, in
-- batches of the step size specified. This is to prevent references etc. from
-- appearing in the wrong order. The prefixTable should be an array containing
-- tables, each of which has two possible fields, a "prefix" string and a
-- "depend" table. The function always parses parameters containing the "prefix"
-- string, but only parses parameters in the "depend" table if the prefix
-- parameter is present and non-blank.
local function preprocessArgs(prefixTable, step)
	if type(prefixTable) ~= 'table' then
		error("Non-table value detected for the prefix table", 2)
	end
	if type(step) ~= 'number' then
		error("Invalid step value detected", 2)
	end

	-- Get arguments without a number suffix, and check for bad input.
	for i,v in ipairs(prefixTable) do
		if type(v) ~= 'table' or type(v.prefix) ~= "string" or
			(v.depend and type(v.depend) ~= 'table') then
			error('Invalid input detected to preprocessArgs prefix table', 2)
		end
		preprocessSingleArg(v.prefix)
		-- Only parse the depend parameter if the prefix parameter is present
		-- and not blank.
		if args[v.prefix] and v.depend then
			for j, dependValue in ipairs(v.depend) do
				if type(dependValue) ~= 'string' then
					error('Invalid "depend" parameter value detected in preprocessArgs')
				end
				preprocessSingleArg(dependValue)
			end
		end
	end

	-- Get arguments with number suffixes.
	local a = 1 -- Counter variable.
	local moreArgumentsExist = true
	while moreArgumentsExist == true do
		moreArgumentsExist = false
		for i = a, a + step - 1 do
			for j,v in ipairs(prefixTable) do
				local prefixArgName = v.prefix .. tostring(i)
				if origArgs[prefixArgName] then
					-- Do another loop if any arguments are found, even blank ones.
					moreArgumentsExist = true
					preprocessSingleArg(prefixArgName)
				end
				-- Process the depend table if the prefix argument is present
				-- and not blank, or we are processing "prefix1" and "prefix" is
				-- present and not blank, and if the depend table is present.
				if v.depend and (args[prefixArgName] or (i == 1 and args[v.prefix])) then
					for j,dependValue in ipairs(v.depend) do
						local dependArgName = dependValue .. tostring(i)
						preprocessSingleArg(dependArgName)
					end
				end
			end
		end
		a = a + step
	end
end

-- Parse the data parameters in the same order that the old {{infobox}} did, so
-- that references etc. will display in the expected places. Parameters that
-- depend on another parameter are only processed if that parameter is present,
-- to avoid phantom references appearing in article reference lists.
local function parseDataParameters()

	preprocessSingleArg('autoheaders')
	preprocessSingleArg('child')
	preprocessSingleArg('bodyclass')
	preprocessSingleArg('subbox')
	preprocessSingleArg('bodystyle')
	preprocessSingleArg('title')
	preprocessSingleArg('titleclass')
	preprocessSingleArg('titlestyle')
	preprocessSingleArg('above')
	preprocessSingleArg('aboveclass')
	preprocessSingleArg('abovestyle')
	preprocessArgs({
		{prefix = 'subheader', depend = {'subheaderstyle', 'subheaderrowclass'}}
	}, 10)
	preprocessSingleArg('subheaderstyle')
	preprocessSingleArg('subheaderclass')
	preprocessArgs({
		{prefix = 'image', depend = {'caption', 'imagerowclass'}}
	}, 10)
	preprocessSingleArg('captionstyle')
	preprocessSingleArg('imagestyle')
	preprocessSingleArg('imageclass')
	preprocessArgs({
		{prefix = 'header'},
		{prefix = 'data', depend = {'label'}},
		{prefix = 'rowclass'},
		{prefix = 'rowstyle'},
		{prefix = 'rowcellstyle'},
		{prefix = 'class'}
	}, 50)
	preprocessSingleArg('headerclass')
	preprocessSingleArg('headerstyle')
	preprocessSingleArg('labelstyle')
	preprocessSingleArg('datastyle')
	preprocessSingleArg('below')
	preprocessSingleArg('belowclass')
	preprocessSingleArg('belowstyle')
	preprocessSingleArg('name')
	-- different behaviour for italics if blank or absent
	args['italic title'] = origArgs['italic title']
	preprocessSingleArg('decat')
	preprocessSingleArg('templatestyles')
	preprocessSingleArg('child templatestyles')
	preprocessSingleArg('grandchild templatestyles')
end

-- If called via #invoke, use the args passed into the invoking template.
-- Otherwise, for testing purposes, assume args are being passed directly in.
function p.infobox(frame)
	if frame == mw.getCurrentFrame() then
		origArgs = frame:getParent().args
	else
		origArgs = frame
	end
	
	parseDataParameters()
	
	return _infobox()
end

-- For calling via #invoke within a template
function p.infoboxTemplate(frame)
	origArgs = {}
	for k,v in pairs(frame.args) do origArgs[k] = mw.text.trim(v) end
	
	parseDataParameters()
	
	return _infobox()
end
return p