본문으로 이동

모듈:Debug

위키문헌 ― 우리 모두의 도서관.
모듈 설명문서[만들기]
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, tsort, options)
	options = options or {}
	
	local func = options.modified and "modified_dump" or "dump"
	
	local dump = export[func](value, prefix, tsort)
	
	-- Remove spaces at beginnings of lines (which are simply to force a <pre></pre> tag).
	dump = dump:gsub("%f[^%z\n] ", "")
	
	return export.highlight(dump)
end

function export.track(key)
    local frame = mw.getCurrentFrame()
    pcall(frame.expandTemplate, frame, { title = '위키문헌 정비/' .. key })
end

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

function export.highlight(content, options)
	if type(content) == "table" then
		options = content
		options = {
			lang = options.lang or "lua",
			inline = options.inline and true
		}
		return function(content)
			return mw.getCurrentFrame():extensionTag{
				name = "syntaxhighlight",
				content = content,
				args = options
			}
		end
	else
		return mw.getCurrentFrame():extensionTag{
			name = "syntaxhighlight",
			content = content,
			args = {
				lang = options and options.lang or "lua",
				inline = options and options.inline and true or nil
			}
		}
	end
end


return export