มอดูล:Template wrapper2

จากวิกิพีเดีย สารานุกรมเสรี
Documentation icon คู่มือการใช้งานมอดูล[สร้าง]
require('Module:No globals');

--[[--------------------------< W R A P 2 >----------------------------------------------------------------------

Template entry point.  Call this function to 'execute' the working template

]]

local function wrap2 (frame)
	-- draft --
	local template = frame.args[1]; --frame.args[1] = nil -- setting frame.args[...] to nil will just make it fallback to call metatable.__index(), which is already the same as its initial behavior
	local mt = getmetatable(frame.args)
	setmetatable(frame.args, nil)
	-- clr positional arguments without metatable
	for k, v in ipairs(frame.args) do frame.args[k] = nil end
	--setmetatable(frame.args, mt)
	if mt then -- mt could only be table or nil
		setmetatable(frame.args, mt)
		local __index = mt.__index -- mt always be table here
		if type(__index)=='function' then mt.__index = function (t, k)
			--if k==1 then return nil end
			-- discard all positional arguments from frame.args
			if type(k)=='number' then return nil end
			return __index(t, k)
		end end
		local __pairs = mt.__pairs
		if type(__pairs)=='function' then mt.__pairs = function (t)
			local f, s, v = __pairs(t)
			return function (t, k)
				local v
				repeat k, v = f(t, k) until type(k)~='number'
				return k, v
			end, s, v
		end end
		local __ipairs = mt.__ipairs
		if type(__ipairs)=='function' then mt.__ipairs = function (t)
			local f, s, v = __pairs(t)
			return function () end
		end end
	end
	local args = require('Module:Arguments').
		getArgs(frame, {trim=false, removeBlanks=false})
	--local mt = getmetatable(args) -- getArgs() always return args with metatable
	--local __index = mt.__index
	--mt.__index = function (t, k)
	--	if k<1 then return __index(t, k) end
	--	--if k<1 then return nil end
	--	return __index(t, k+1)
	--end
	return frame:expandTemplate {title=template, args=args};					-- render the working template
end

--[[--------------------------< E X P O R T E D   F U N C T I O N S >------------------------------------------
]]

return {
	wrap2 = wrap2,
	};