Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

// request.cf · coarse context

A page that knows where it met you.

Only coarse request metadata is shown. This demo does not display or persist visitor IP addresses.

Country
US
Cloudflare location
CMH
Connection
HTTP/2
Language
Not provided

Ray ID: a22d50959baed96e

Jump to content

Module:Sandbox/DePiep/element

From Wikipedia, the free encyclopedia
local p = {}
local data1 = ''
local e2id

function eToId(e)
	return e	
end

function p.element(frame)
	local args = frame.args  -- arguments in #invoke
	local first = args[1] or 'no value for first'  -- first unnamed argument
	local dummy = args['dummy'] or 'no value for dummy'
	
	local eId = eToId(e)
	
	local e = {}
	
	-- Make a copy of the args table. The values will actually be present in
	-- argsCopy, so the # operator will work on it. The # operator doesn't work
	-- on the args table directly, because it doesn't actually contain any
	-- values. (The values are found through the use of metatables.)
	local argsCopy = {}
	for k, v in pairs(args) do
		argsCopy[k] = v
	end
	
	table.insert(e, '')
	return '*Value of #args (I don\'t think this does anything useful): ' .. #args ..
		'\n*Value of #argsCopy: ' .. #argsCopy ..
		'\n*First unnamed argument: ' .. first ..
		'\n*Dummy argument: ' .. dummy
end

function p.metatable()
	-- This function shows how metatables work with the # operator.
	-- The arguments table (frame.args) uses something similar to this, so
	-- this example should explain why # doesn't work on it. 
	local ret = ''

	-- At first, t1 is just a blank table.
	local t1 = {}
	ret = ret .. 't1 with no metatable:'
	ret = ret .. '\n* First unnamed argument: ' .. (t1[1] or 'no value for first')
	ret = ret .. '\n* Length: ' .. #t1
	
	-- t2 contains the actual values we want.
	local t2 = {'the first t2 value', 'the second t2 value'}
	ret = ret .. '\n\n'
	ret = ret .. 't2, which holds the arguments:'
	ret = ret .. '\n* First unnamed argument: ' .. (t2[1] or 'no value for first')
	ret = ret .. '\n* Length: ' .. #t2
	
	-- Now we attatch a metatable to t1. We haven't given any instructions to
	-- the metatable yet, so it won't do anything for now.
	local metatable = {}
	setmetatable(t1, metatable)
	ret = ret .. '\n\n'
	ret = ret .. 't1 with a blank metatable:'
	ret = ret .. '\n* First unnamed argument: ' .. (t1[1] or 'no value for first')
	ret = ret .. '\n* Length: ' .. #t1
	
	-- And now we add an __index metamethod to the metatable. This example
	-- tells Lua to look up values in t2 if the value in t1 is nil.
	-- t1 still doesn't contain any actual values, so its length is still
	-- reported as 0.
	metatable.__index = t2
	ret = ret .. '\n\n'
	ret = ret .. 't1 with an __index metamethod set to t2:'
	ret = ret .. '\n* First unnamed argument: ' .. (t1[1] or 'no value for first')
	ret = ret .. '\n* Length: ' .. #t1

	-- Now we write a new value to t1. This value is actually present in t1, so
	-- Lua doesn't look up the value in t2, and it is picked up by the #
	-- operator.
	t1[1] = 'A new first value for t1'
	ret = ret .. '\n\n'
	ret = ret .. 't1 with an __index metamethod set to t2, and a value written to the first field:'
	ret = ret .. '\n* First unnamed argument: ' .. (t1[1] or 'no value for first')
	ret = ret .. '\n* Length: ' .. #t1
	
	return ret
end

return p