모듈:CGI2: 두 판 사이의 차이

리버티게임, 모두가 만들어가는 자유로운 게임
백괴게임>Gustmd7410
편집 요약 없음
편집 요약 없음
 
(사용자 2명의 중간 판 5개는 보이지 않습니다)
9번째 줄: 9번째 줄:


function encode(t)
function encode(t)
return mw.text.encode(mw.text.jsonEncode(params):sub(2, -2) .. ',')
if #t > 0 then
return mw.text.encode(mw.text.jsonEncode(t):sub(2, -2) .. ',')
else
return ''
end
end
end



2024년 2월 5일 (월) 00:20 기준 최신판


모듈 설명문서[보기] [편집] [역사] [새로 고침]

{{CGI2}}를 좀 더 편리하게 사용할 수 있도록 도와주는 모듈입니다.


local getArgs = require('모듈:Arguments').getArgs
local p = {}
local actions = {
	set = "entry",
	del = "key",
	default = "entry",
	restrict = "key"
}

function encode(t)
	if #t > 0 then
		return mw.text.encode(mw.text.jsonEncode(t):sub(2, -2) .. ',')
	else
		return ''
	end
end

function set(action, frame)
	local args = getArgs(frame:getParent())
	local params = {}
	
	for key, value in pairs(args) do
		local payload
		
		if(actions[action] == "entry") then payload = {key, value}
		elseif(actions[action] == "key") then payload = value
		end
		
		table.insert(params, {[action] = payload})
	end
	
	return encode(params)
end

function p.toGET(frame)
	local params = mw.text.jsonDecode('[' .. mw.text.decode(frame.args[1]):sub(1, -2) .. ']')
	
	for index, wrapper in pairs(params) do
		if(wrapper.set) then
			for i, str in pairs(wrapper.set) do
				wrapper.set[i] = mw.uri.encode(str)
			end
			
			params[index] = table.concat(wrapper.set, "=")
		end
	end
	
	return table.concat(params, '&')
end

function p.fromGET(frame)
	local params = mw.text.split(frame.args.get, "&")
	
	for index, entry in pairs(params) do
		local payload = mw.text.split(entry, "=")
		
		for i, str in pairs(payload) do
			payload[i] = mw.uri.decode(str)
		end
		
		params[index] = {set = payload}
	end
	
	return encode(params)
end

return setmetatable(p, {
	__index = function(t, action)
		if(actions[action]) then
			return function(frame)
				return set(action, frame)
			end
		end
	end
})