이 모듈은 틀을 디버깅하고 오류를 역추적하는 데 사용되는 모듈입니다.

dump 편집

dump(value)

함수를 제외한 값을 문자열 표현식으로 바꿔줍니다. 문자열은 루아 구문으로 되어있으며 가능하다면 이 함수의 출력값을 받아 루아 모듈에 입력할 수 있습니다. 테이블은 재귀적으로 작동되며, 탭은 스페이스로 변환됩니다.

highlight_dump 편집

highlight_dump(value)

dump와 같은 방식으로 동작하지만, 루아 구문에 강조 표시를 남기며, 탭을 스페이스로 변환하지 않고 유지합니다.

error 편집

{{#invoke:debug|error|message}}

틀에서 호출된 함수의 스크립트 오류를 잡아냅니다. 일일히 틀을 루아와 대응하여 비교해야 할 필요가 없을 경우 유용하게 사용될 수 있습니다.


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 = 'tracking/' .. 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