Module:TableParser
TableParser
is a utility module for parsing two-dimensional tables using a syntax similar to MediaWiki's own table syntax.
DEPRECATED
This module is now deprecated, and this page only exists for archival purposes. Please use Module:TableArguments instead.
Example
Wikicode
... {{#invoke:SomeModule|print_table |a|b|c|d |- |e|f|g |- |h|i|j|k }} ...
Lua
... TableParser.parse(args, function (idx, row) print(("* row %s: %s):format(idx, table.concat(row, " "))) end) ...
Description
The module has a single function, parse
, which accepts the arguments args
, row_fn
, and optionally delim
. The function scans all the numbered indices of args
, treating each instance of delim
as a row delimiter. If unspecified, delim
defaults to -
, just like in normal Wikitable syntax.
For each scanned row, TableParser
will call row_fn(idx, row)
, where idx
is the index of the row, starting from 1, and row
is a Lua table containing the contents of that row.
parse
will return a table containing the non-nil results of each call to row_fun
.
Test Case
Test Case 1
- row 1: a b c
- row 2: = d e
- row 3: = f
Should print out:
- row 1: a b c
- row 2: = d e
- row 3: = f
Test Case 2 (Custom Delimiter)
- row 1: a b c -
- row 2: d e -
- row 3: f
Should print out:
- row 1: a b c -
- row 2: d e -
- row 3: f
See Also
-- Lua is extremely stupid
local function maybe_insert(tbl, x)
if x ~= nil then
table.insert(tbl, x)
end
end
local function parse(args, row_fn, delim)
local delim = delim or "-"
local idx = 1
local row = {}
local res = {}
for _, arg in ipairs(args) do
if arg == delim then
maybe_insert(res, row_fn(idx, row))
idx = idx + 1
row = {}
else
table.insert(row, arg)
end
end
maybe_insert(res, row_fn(idx, row))
return res
end
return {parse=parse}