Spring til indhold

Modul:Sandkasse/Poul G/dump

Fra Wikipedia, den frie encyklopædi
require("Modul:No globals")

local function dump(x, mode)

	local tablePre, tableSep, tablePost = "{ ", ", ", " }"
	local tableItemPre, tableItemSep, tableItemPost = "[", "]=", ""
	
	local function d(x)
	
		if x == nil then
			return "nil"
		elseif type(x) == "string" then
			return '"' .. x .. '"'
		elseif type(x) == "table" then
			local buffer = {}
			for k, v in pairs(x) do
				table.insert(buffer, tableItemPre .. d(k) .. tableItemSep .. d(v) .. tableItemPost)
			end
			return tablePre .. table.concat(buffer, tableSep) .. tablePost
		else
			return "" .. x
		end
	
	end

	if mode == "pre" or mode == "code" then
		return "<" .. mode .. ">" .. d(x) .. "</" .. mode .. ">"
	elseif mode == "ol" or mode == "ul" then
		tablePre, tableSep, tablePost = "<" .. mode .. ">", "",  "</" .. mode .. ">"
		tableItemPre, tableItemSep, tableItemPost = "<li>", ": ", "</li>"
		return d(x)
	else -- default
		return d(x)
	end
	
end

return dump