Jump to content

Module:Dump/doc: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
dumping a navbox
m change source to syntaxhighlight
Line 20: Line 20:


If there is a problem debugging a module, it can be helpful to use a sandbox copy of the module to display the contents of a table to confirm that it contains expected data. The following shows how a module can dump a table.
If there is a problem debugging a module, it can be helpful to use a sandbox copy of the module to display the contents of a table to confirm that it contains expected data. The following shows how a module can dump a table.
<source lang="Lua">
<syntaxhighlight lang="Lua">
local function main()
local function main()
local example = {
local example = {
Line 32: Line 32:


return { main = main }
return { main = main }
</syntaxhighlight>
</source>


With the above code in Module:Example, the result could be displayed by previewing:
With the above code in Module:Example, the result could be displayed by previewing:
Line 44: Line 44:
A module can use [[:mw:Extension:Scribunto/Lua reference manual#HTML library|mw.html]] to generate html. For testing, it may be useful to display the formatted result. The following shows how a module can create and dump html text.
A module can use [[:mw:Extension:Scribunto/Lua reference manual#HTML library|mw.html]] to generate html. For testing, it may be useful to display the formatted result. The following shows how a module can create and dump html text.


<source lang="Lua">
<syntaxhighlight lang="Lua">
local function main()
local function main()
local tbl = mw.html.create('table')
local tbl = mw.html.create('table')
Line 67: Line 67:


return { main = main }
return { main = main }
</syntaxhighlight>
</source>


With the above code in Module:Example, the result could be displayed by previewing:
With the above code in Module:Example, the result could be displayed by previewing:
Line 92: Line 92:


The <code>main()</code> function in the code above could be modified to return the html table by replacing the last two lines with:
The <code>main()</code> function in the code above could be modified to return the html table by replacing the last two lines with:
<source lang="Lua">
<syntaxhighlight lang="Lua">
return html
return html
</syntaxhighlight>
</source>


In that case, the result could be displayed by previewing the following (the <code>1=</code> is needed if the text contains "<code>=</code>"):
In that case, the result could be displayed by previewing the following (the <code>1=</code> is needed if the text contains "<code>=</code>"):

Revision as of 13:25, 7 July 2020

This module can dump a table by displaying its contents as text. It can also display formatted html text. That may help develop other modules.

An alternative is to use mw.dumpObject() but the result from this module is clearer and is close to valid Lua source.

Dump of a Wikidata entity

When working with Wikidata, it can be useful to examine a table representing an entity.

For example, Southern African Large Telescope is d:Q833639. That entity can be viewed as a Lua table by previewing:

{{#invoke:dump|wikidata|Q833639}}

To do that, edit your sandbox and replace its contents with the above line, then click Show preview. Module talk:Dump shows this example.

If wanted, the width of each indented column can be set, for example to 2 spaces:

{{#invoke:dump|wikidata|Q833639|indent=2}}

A property such as diameter (P2386) can also be displayed:

{{#invoke:dump|wikidata|P2386}}

Dump of a table for another module

If there is a problem debugging a module, it can be helpful to use a sandbox copy of the module to display the contents of a table to confirm that it contains expected data. The following shows how a module can dump a table.

local function main()
	local example = {
		link = true,
		fruit = { yellow = 'bannana', red = 'cherry' },
		11, 22, 33,
	}
	local dump = require('Module:Dump')._dump
	return dump(example, 'example')
end

return { main = main }

With the above code in Module:Example, the result could be displayed by previewing:

{{#invoke:example|main}}

The module contains a complex table for testing. The table can be displayed by previewing:

{{#invoke:dump|testcase}}

Dump of a formatted html string

A module can use mw.html to generate html. For testing, it may be useful to display the formatted result. The following shows how a module can create and dump html text.

local function main()
	local tbl = mw.html.create('table')
	tbl
		:addClass('wikitable')
		:tag('caption'):wikitext('Table demonstration'):done()
		:tag('tr')
			:tag('th'):wikitext('Month'):done()
			:tag('th'):wikitext('Amount'):done()
			:done()
		:tag('tr')
			:tag('td'):wikitext('January'):done()
			:tag('td'):wikitext('$100<br>loss'):done()
			:done()
		:tag('tr')
			:tag('td'):wikitext('February'):done()
			:tag('td'):wikitext('$200')
	local html = tostring(tbl)
	local dumphtml = require('Module:Dump')._dumphtml
	return dumphtml(html)
end

return { main = main }

With the above code in Module:Example, the result could be displayed by previewing:

{{#invoke:example|main}}

The result is:

<table class="wikitable">
    <caption>Table demonstration</caption>
    <tr>
        <th>Month</th>
        <th>Amount</th>
    </tr>
    <tr>
        <td>January</td>
        <td>$100<br>loss</td>
    </tr>
    <tr>
        <td>February</td>
        <td>$200</td>
    </tr>
</table>

The main() function in the code above could be modified to return the html table by replacing the last two lines with:

	return html

In that case, the result could be displayed by previewing the following (the 1= is needed if the text contains "="):

{{#invoke:dump|dumphtml|1={{#invoke:example|main}}}}

Dumping a navbox

Previewing the following examples in a sandbox may be useful to examine the results of a template, such as {{navbox}}, that generates html.

{{#invoke:dump|dumphtml|1=
  {{navbox/sandbox
  |group1 = Group1
  |list1 = List1
  |group2 = Group2
  |list2 = List2
  |group3 = Group3
  |list3 = List3
  }}
}}

The dumphtml procedure only works reliably with valid html. In the following example, extra text (<div>) is inserted at the start because the output from a subgroup (child) navbox starts with </div>.

{{#invoke:dump|dumphtml|1=<div>
  {{navbox/sandbox|subgroup
  |group1 = Group1
  |list1 = List1
  |group2 = Group2
  |list2 = List2
  |group3 = Group3
  |list3 = List3
  }}
}}

Global table _G

In Lua, _G is a global variable which is a table holding information about all global variables. The _G table can be displayed by previewing (both G and _G work):

{{#invoke:dump|testcase|G}}

If wanted, the width of each indented column can be set, for example to 2 spaces:

{{#invoke:dump|testcase|G|indent=2}}