본문으로 이동

모듈:glossary/data/dump

위키낱말사전, 말과 글의 누리

이 모듈에 대한 설명문서는 모듈:glossary/data/dump/설명문서에서 만들 수 있습니다

local export = {}

local isArray = require("Module:table").isArray
local highlight = require("Module:debug").highlight

local function nextVal(t)
	if type(t) ~= "table" then
		return nil
	end
	local _, value = next(t)
	return value
end

local function containsTable(t)
	for k, v in pairs(t) do
		if type(v) == "table" then
			return true
		end
	end
	return false
end

-- Convert a value to a string
function export.dump(value, prefix, tsort)
	local t = type(value)
	
	prefix = prefix or ""
	
	if t == "string" then
		return '"' .. value .. '"'
	elseif t == "table" then
		local str_table = {}
		
		local array = isArray(value)
		if array then
			local str_array = {}
			local containsTable = containsTable(value)
			table.insert(containsTable and str_table or str_array, "{")
			
			for i, val in ipairs(value) do
				table.insert(str_array, " " .. export.dump(val, nil, tsort) .. ",")
			end
			
			table.insert(containsTable and str_table or str_array, "}")
			table.insert(str_table, table.concat(str_array, containsTable and "\n" or ""))
		else
			table.insert(str_table, " {")
			
			for key, val in require("Module:table").sortedPairs(value, tsort) do
				table.insert(str_table, prefix .. "\t[" .. export.dump(key, prefix .. "\t") .. "] = " .. export.dump(val, prefix .. "\t"):gsub("^ ", "") .. ",")
			end
		
			table.insert(str_table, prefix .. "}")
		end
		
		return table.concat(str_table, "\n")
	else
		return tostring(value)
	end
end

function export.show(frame)
	return highlight(export.dump(mw.loadData("Module:Unicode data/scripts")))
end

function export.dump_data_module(frame)
	local module_name = frame.args[1]
	if module_name and module_name ~= "" then
		local success, module = pcall(mw.loadData, "Module:" .. module_name)
		if success then
			local to_be_dumped
			if frame.args[2] then
				to_be_dumped = module[frame.args[2]]
				if to_be_dumped == nil then
					error("The index " .. frame.args[2] .. " is nil in [[Module:" .. module_name .. "]].")
				end
			else
				to_be_dumped = module
			end
			return require "Module:debug".highlight_dump(to_be_dumped)
		end
	end
end
		

return export