Difference between revisions of "Module:Demo"

From MINR.ORG WIKI
(reduce decoding when nowiki tags are not present)
(html entities are now visible in pre; allow modules to be demonstrated directly)
Line 1: Line 1:
 
local p = {}
 
local p = {}
  
function p.get(frame, arg, passArgs)
+
--creates a frame object that cannot access any of the parent's args
 +
--unless a table containing a list keys of not to inherit is provided
 +
function disinherit(frame, onlyTheseKeys)
 
frame = frame:getParent() or frame
 
frame = frame:getParent() or frame
 
local orphan = frame:newChild{}
 
local orphan = frame:newChild{}
--Allows parameters to be added (see example at Module:RoundN/doc and Module:RoundN/testcases/2)
+
orphan.getParent = frame.getParent --returns nil
if frame.args.passArgs or passArgs then
+
if onlyTheseKeys then
 
orphan.args = mw.clone(frame.args)
 
orphan.args = mw.clone(frame.args)
--Set arg to something else if you want to pass your source code to the child for some reason.
+
for _, k in ipairs(onlyTheseKeys) do
orphan.args[arg or 1] = nil
+
orphan.args[k] = nil
 +
end
 
end
 
end
orphan.getParent = function() return nil end
+
return orphan, frame
local farg = frame.args[arg or 1] or ''
+
end
--less decoded version if Template:Escape is used instead of nowiki:
+
 
if (farg):match('nowiki') then
+
function p.get(frame, arg, passArgs)
local noNoWiki = mw.text.nowiki(mw.text.decode(mw.text.unstripNoWiki(farg)))
+
local orphan, frame = disinherit(frame, passArgs and {arg or 1})
 +
local code, noWiki, preserve = frame.args[arg or 1] or ''
 +
if code:match'nowiki' then
 +
local placeholder, preserve = ('6'):char(), {}
 +
code = mw.text.unstripNoWiki(code)
 +
noWiki = code:gsub('%%', placeholder):gsub('&lt;', '<'):gsub('&gt;', '>')
 +
for k in noWiki:gmatch('&.-;') do
 +
if not preserve[k] then
 +
preserve[k] = true
 +
table.insert(preserve, (k:gsub('&', '&amp;')))
 +
noWiki = noWiki:gsub('(&.-;)', '%%%s')
 +
end
 +
end
 +
noWiki = mw.text.nowiki(noWiki):format(unpack(preserve)):gsub(placeholder, '%%')
 
end
 
end
 
return {
 
return {
source = noNoWiki or farg,
+
source = noWiki or code,
output = orphan:preprocess(noNoWiki and farg or mw.text.unstripNoWiki(farg)),
+
output = orphan:preprocess(code),
 
frame = frame
 
frame = frame
 
}
 
}
 
end
 
end
  
function p.main(frame)
+
function p.main(frame, demoTable)
local show = p.get(frame)
+
local show = demoTable or p.get(frame)
local fbr, br = show.frame.args.br, ''
+
local args = show.frame.args
if not fbr or tonumber(fbr) then
+
args.br = tonumber(args.br or 1) and ('<br>'):rep(args.br or 1) or args.br or ''
for k = 1, (fbr or 1) do
+
if show[args.result_arg] then
br = br .. '<br>'
+
return show[args.result_arg]
 +
end
 +
return string.format('<pre%s>%s</pre>%s%s', args.style and string.format(" style='%s'", args.style) or '', show.source, args.br, show.output)
 +
end
 +
 
 +
--passing of args into other module without preprocessing
 +
function p.module(frame)
 +
local orphan, frame = disinherit(frame, {
 +
'demo_template',
 +
'demo_module_func',
 +
'demo_main',
 +
'demo_br',
 +
'demo_result_arg'
 +
})
 +
local template = frame.args.demo_template and 'Template:'..frame.args.demo_template
 +
local demoFunc = frame.args.demo_module_func or 'main\n'
 +
local demoModule = require('Module:%'..frame.args.demo_module)[demoFunc:match('^%s*(.-)%s*$')]
 +
frame.args.br, frame.args.result_arg = frame.args.demo_br, frame.args.demo_result_arg
 +
if demoModule then
 +
local source = template and {'{{', template and '' or '#invoke:', frame.args.demo_template or frame.args.demo_module, template and '' or '|', demoFunc}
 +
for k, v in pairs(frame.args) do
 +
table.insert(
 +
source,
 +
tonumber(k) and
 +
'|'..v
 +
or ('|%s = %s'):format(k, v)
 +
)
 
end
 
end
 +
return p.main(frame, {
 +
source = mw.text.nowiki(table.concat(source)),
 +
output = demoModule(orphan),
 +
frame = frame
 +
})
 
else
 
else
br = fbr
+
return "ERROR: Invalid module function: "..demoFunc
end
 
if show[show.frame.args.result_arg] then
 
return show[show.frame.args.result_arg]
 
 
end
 
end
return string.format('<pre%s>%s</pre>%s%s', show.frame.args.style and string.format(" style='%s'", show.frame.args.style) or '', show.source, br, show.output)
 
 
end
 
end
  
 
return p
 
return p

Revision as of 00:23, 27 January 2015

Documentation for this module may be created at Module:Demo/doc

local p = {}

--creates a frame object that cannot access any of the parent's args
--unless a table containing a list keys of not to inherit is provided
function disinherit(frame, onlyTheseKeys)
	frame = frame:getParent() or frame
	local orphan = frame:newChild{}
	orphan.getParent = frame.getParent --returns nil
	if onlyTheseKeys then
		orphan.args = mw.clone(frame.args)
		for _, k in ipairs(onlyTheseKeys) do
			orphan.args[k] = nil
		end
	end
	return orphan, frame
end

function p.get(frame, arg, passArgs)
	local orphan, frame = disinherit(frame, passArgs and {arg or 1})
	local code, noWiki, preserve = frame.args[arg or 1] or ''
	if code:match'nowiki' then
		local placeholder, preserve = ('6'):char(), {}
		code = mw.text.unstripNoWiki(code)
		noWiki = code:gsub('%%', placeholder):gsub('&lt;', '<'):gsub('&gt;', '>')
		for k in noWiki:gmatch('&.-;') do
			if not preserve[k] then
				preserve[k] = true
				table.insert(preserve, (k:gsub('&', '&amp;')))
				noWiki = noWiki:gsub('(&.-;)', '%%%s')
			end
		end
		noWiki = mw.text.nowiki(noWiki):format(unpack(preserve)):gsub(placeholder, '%%')
	end
	return {
		source = noWiki or code,
		output = orphan:preprocess(code),
		frame = frame
	}
end

function p.main(frame, demoTable)
	local show = demoTable or p.get(frame)
	local args = show.frame.args
	args.br = tonumber(args.br or 1) and ('<br>'):rep(args.br or 1) or args.br or ''
	if show[args.result_arg] then
		return show[args.result_arg]
	end
	return string.format('<pre%s>%s</pre>%s%s', args.style and string.format(" style='%s'", args.style) or '', show.source, args.br, show.output)
end

--passing of args into other module without preprocessing
function p.module(frame)
	local orphan, frame = disinherit(frame, {
		'demo_template',
		'demo_module_func',
		'demo_main',
		'demo_br',
		'demo_result_arg'
	})
	local template = frame.args.demo_template and 'Template:'..frame.args.demo_template
	local demoFunc = frame.args.demo_module_func or 'main\n'
	local demoModule = require('Module:%'..frame.args.demo_module)[demoFunc:match('^%s*(.-)%s*$')]
	frame.args.br, frame.args.result_arg = frame.args.demo_br, frame.args.demo_result_arg
	if demoModule then
		local source = template and {'{{', template and '' or '#invoke:', frame.args.demo_template or frame.args.demo_module, template and '' or '|', demoFunc}
		for k, v in pairs(frame.args) do
			table.insert(
				source,
				tonumber(k) and
					'|'..v
					or ('|%s = %s'):format(k, v)
			)
		end
		return p.main(frame, {
			source = mw.text.nowiki(table.concat(source)),
			output = demoModule(orphan),
			frame = frame
		})
	else
		return "ERROR: Invalid module function: "..demoFunc
	end
end

return p