Module:TestLua: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function tprint (tbl, indent) | |||
if not indent then indent = 0 end | |||
local toprint = string.rep(" ", indent) .. "{\r\n" | |||
indent = indent + 2 | |||
for k, v in pairs(tbl) do | |||
toprint = toprint .. string.rep(" ", indent) | |||
if (type(k) == "number") then | |||
toprint = toprint .. "[" .. k .. "] = " | |||
elseif (type(k) == "string") then | |||
toprint = toprint .. k .. "= " | |||
end | |||
if (type(v) == "number") then | |||
toprint = toprint .. v .. ",\r\n" | |||
elseif (type(v) == "string") then | |||
toprint = toprint .. "\"" .. v .. "\",\r\n" | |||
elseif (type(v) == "table") then | |||
toprint = toprint .. tprint(v, indent + 2) .. ",\r\n" | |||
else | |||
toprint = toprint .. "\"" .. tostring(v) .. "\",\r\n" | |||
end | |||
end | |||
toprint = toprint .. string.rep(" ", indent-2) .. "}" | |||
return toprint | |||
end | |||
function p.main(frame) | function p.main(frame) | ||
Line 6: | Line 32: | ||
local ret = mw.visualdata.query( 'Book', '[[title::+]]', {'title'}, {} ) | local ret = mw.visualdata.query( 'Book', '[[title::+]]', {'title'}, {} ) | ||
return | return print(tprint(ret)) | ||
end | end | ||
Revision as of 22:30, 14 November 2024
Documentation for this module may be created at Module:TestLua/doc
local p = {}
function tprint (tbl, indent)
if not indent then indent = 0 end
local toprint = string.rep(" ", indent) .. "{\r\n"
indent = indent + 2
for k, v in pairs(tbl) do
toprint = toprint .. string.rep(" ", indent)
if (type(k) == "number") then
toprint = toprint .. "[" .. k .. "] = "
elseif (type(k) == "string") then
toprint = toprint .. k .. "= "
end
if (type(v) == "number") then
toprint = toprint .. v .. ",\r\n"
elseif (type(v) == "string") then
toprint = toprint .. "\"" .. v .. "\",\r\n"
elseif (type(v) == "table") then
toprint = toprint .. tprint(v, indent + 2) .. ",\r\n"
else
toprint = toprint .. "\"" .. tostring(v) .. "\",\r\n"
end
end
toprint = toprint .. string.rep(" ", indent-2) .. "}"
return toprint
end
function p.main(frame)
-- local args = frame:getParent().args
local ret = mw.visualdata.query( 'Book', '[[title::+]]', {'title'}, {} )
return print(tprint(ret))
end
return p
-- =p.main({"add\ndfdd","b","c","text"})