Module:TestLua: Difference between revisions

From Wikisphere
Jump to navigation Jump to search
No edit summary
No edit summary
 
(4 intermediate revisions by the same user not shown)
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 32: Line 6:
   local ret = mw.visualdata.query( 'Book', '[[title::+]]', {'title'}, {} )
   local ret = mw.visualdata.query( 'Book', '[[title::+]]', {'title'}, {} )


   return print(tprint(ret))
   return mw.text.jsonEncode(ret)
end
end



Latest revision as of 22:41, 14 November 2024

Documentation for this module may be created at Module:TestLua/doc

local p = {}

function p.main(frame)
  -- local args = frame:getParent().args

  local ret = mw.visualdata.query( 'Book', '[[title::+]]', {'title'}, {} )

  return mw.text.jsonEncode(ret)
end

return p


-- =p.main({"add\ndfdd","b","c","text"})