Open main menu
Home
Random
Donate
Recent changes
Special pages
Community portal
Preferences
About Stockhub
Disclaimers
Search
User menu
Talk
Contributions
Create account
Log in
Editing
Module:Dump/doc
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
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 <code>mw.dumpObject()</code> 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: :<code><nowiki>{{#invoke:dump|wikidata|Q833639}}</nowiki></code> To do that, edit [[Special:MyPage/sandbox|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 two spaces or to use tab characters: :<code><nowiki>{{#invoke:dump|wikidata|Q833639|indent=2}}</nowiki></code> :<code><nowiki>{{#invoke:dump|wikidata|Q833639|indent=tab}}</nowiki></code> A property such as {{Q|P2386}} can also be displayed: :<code><nowiki>{{#invoke:dump|wikidata|P2386}}</nowiki></code> == 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. <syntaxhighlight lang="Lua"> local p = {} function p.main(frame) local example = { link = true, fruit = { yellow = 'bannana', red = 'cherry' }, 11, 22, 33, } local dump = require('Module:Dump')._dump return dump(example, 'example') end return p </syntaxhighlight> With the above code in Module:Example, the result could be displayed by previewing: :<code><nowiki>{{#invoke:example|main}}</nowiki></code> The module contains a complex table for testing. The table can be displayed by previewing: :<code><nowiki>{{#invoke:dump|testcase}}</nowiki></code> == Dump of a formatted html string == 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. <syntaxhighlight lang="Lua"> 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 } </syntaxhighlight> With the above code in Module:Example, the result could be displayed by previewing: :<code><nowiki>{{#invoke:example|main}}</nowiki></code> The result is: <pre> <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> </pre> The <code>main()</code> function in the code above could be modified to return the html table by replacing the last two lines with: <syntaxhighlight lang="Lua"> return html </syntaxhighlight> In that case, the result could be displayed by previewing the following (the <code>1=</code> is needed if the text contains "<code>=</code>"): :<code><nowiki>{{#invoke:dump|dumphtml|1={{#invoke:example|main}}}}</nowiki></code> === Dumping a navbox === Previewing the following examples in a sandbox may be useful to examine the results of a template, such as {{tl|navbox}}, that generates html. <pre> {{#invoke:dump|dumphtml|1= {{navbox/sandbox |group1 = Group1 |list1 = List1 |group2 = Group2 |list2 = List2 |group3 = Group3 |list3 = List3 }} }} </pre> The dumphtml procedure only works reliably with valid html. In the following example, extra text (<code><div></code>) is inserted at the start because the output from a subgroup (child) navbox starts with <code></div></code>. <pre> {{#invoke:dump|dumphtml|1=<div> {{navbox/sandbox|subgroup |group1 = Group1 |list1 = List1 |group2 = Group2 |list2 = List2 |group3 = Group3 |list3 = List3 }} }} </pre> == Dump of arguments == [[Special:ExpandTemplates]] is useful if there is a need to view the wikitext returned by a template or module. However, ExpandTemplates does not always show exactly what a module would receive. For example, the following template gives the output shown in ExpandTemplates, but the wikitext passed to a module would actually contains [[WP:UNIQ|strip markers]]. :<code><nowiki>{{convert|1+2/3<ref>Example</ref>|ft|in}}</nowiki></code> :ExpandTemplates output, rearranged on multiple lines for clarity: ::<code><nowiki><templatestyles src="Fraction/styles.css"></templatestyles></nowiki></code> ::<code><nowiki><span class="frac" role="math">1<span class="sr-only">+</span></nowiki></code> ::<code><nowiki><span class="num">2</span>⁄<span class="den">3</span></span></nowiki></code> ::<code><nowiki><ref>Example</ref></nowiki></code> ::<code><nowiki> feet (20 in)</nowiki></code> The <code>args</code> function shows what a module receives in its arguments. :<code><nowiki>{{#invoke:dump|args|1={{convert|1+2/3<ref>Example</ref>|ft|in}}}}</nowiki></code> The output follows. For clarity, it has been rearranged on multiple lines and each [[delete character]] has been replaced with <code>β’</code>. ::<code><nowiki>β’'"`UNIQ--templatestyles-00000002-QINU`"'β’</nowiki></code> ::<code><nowiki><span class="frac" role="math">1<span class="sr-only">+</span></nowiki></code> ::<code><nowiki><span class="num">2</span>⁄<span class="den">3</span></span></nowiki></code> ::<code><nowiki>β’'"`UNIQ--ref-00000001-QINU`"'β’</nowiki></code> ::<code><nowiki> feet (20 in)</nowiki></code> == Dump of parameters == A template might invoke the <code>main</code> function in the example [[#Dump of a table for another module|above]]. Any parameters passed to the template or the module can be displayed for debugging. That would be to investigate an unexpected result in a page, for example, [[Albedo]]. To see what parameters are received by a module used in that article, edit the module and insert the following line at the start of the main function: <syntaxhighlight lang="Lua"> if true then return require('Module:Dump').parameters(frame) end </syntaxhighlight> Do not save the changes. Instead, enter the name of the article (for example, Albedo) in the box under "Preview page with this module", then click Show preview. Any parameters passed to the module in its frame and parent frame are displayed where the result from the module would normally appear. == Dump of structured data == A Lua program could execute <syntaxhighlight lang="Lua"> local data = mw.ext.data.get('Wikipedia statistics/data.tab') </syntaxhighlight> to read a table of data from [[c:Data:Wikipedia statistics/data.tab]]. An edit in a sandbox can be previewed to see what data the program would receive. To do that, preview the following wikitext:<br> <code><nowiki>{{#invoke:dump|Wikipedia statistics/data.tab}}</nowiki></code> The dump module accepts any text as the parameter and will apply special processing if the text is recognized. Structured data is identified as text ending with <code>.tab</code>. == Global table _G == In Lua, <code>_G</code> is a global variable which is a table holding information about all global variables. The <code>_G</code> table can be displayed by previewing (both <code>G</code> and <code>_G</code> work): :<code><nowiki>{{#invoke:dump|testcase|G}}</nowiki></code> If wanted, the width of each indented column can be set, for example to 2 spaces: :<code><nowiki>{{#invoke:dump|testcase|G|indent=2}}</nowiki></code>
Summary:
Please note that all contributions to Stockhub may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Stockhub:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Stockhub entities used in this page
diameter
: Miscellaneous (e.g. aliases, entity existence), Label: en-gb
Templates used on this page:
Template:Q
(
edit
)
Template:Tl
(
edit
)
Module:Wd
(
edit
)
Module:Wd/i18n
(
edit
)