Gaan na inhoud

Module:debug

Vanuit Wiktionary, die vrye woordeboek.

Dokumentasie vir hierdie module kan geskep word by: Module:debug/doc

local export = {}

-- Convert a value to a string
function export.dump(value, prefix)
    local t = type(value)
    
    prefix = prefix or ""
    
    if t == "string" then
        return '"' .. value .. '"'
    elseif t == "table" then
        local str_table = {}
        
        table.insert(str_table, " {")
        
        for key, val in pairs(value) do
            table.insert(str_table, " " .. prefix .. "\t[" .. export.dump(key, prefix .. "\t") .. "] = " .. mw.ustring.gsub(export.dump(val, prefix .. "\t"), "^ ", "") .. ",")
        end
        
        table.insert(str_table, " " .. prefix .. "}")
        
        return table.concat(str_table, "\n")
    else
        return tostring(value)
    end
end

function export.highlight_dump(value, prefix)
	local dump = export.dump(value, prefix)
	
	-- Remove spaces at beginnings of lines (which are simply to force a <pre></pre> tag).
	dump = mw.ustring.gsub(dump, "^ ", "")
	dump = mw.ustring.gsub(dump, "\n ", "\n")
	
	dump = mw.getCurrentFrame():extensionTag{ name = "syntaxhighlight", content = dump, args = { lang = "lua" } }
	
	return dump
end
	

function export.track(key)
	local frame = mw.getCurrentFrame()
	if key then
		if type(key) ~= "table" then
			key = { key }
		end
		
		for i, value in pairs(key) do
			pcall(frame.expandTemplate, frame, { title = 'tracking/' .. value })
		end
	else
		error('No tracking key supplied to the function "' .. track .. '".')
	end
end

-- Trigger a script error from a template
function export.error(frame)
    error(frame.args[1] or "(no message specified)")
end

return export