Difference between revisions of "Module:Demo"

From MINR.ORG WIKI
(allow alternative instead of br)
(reduce decoding when nowiki tags are not present)
Line 3: Line 3:
 
function p.get(frame, arg, passArgs)
 
function p.get(frame, arg, passArgs)
 
frame = frame:getParent() or frame
 
frame = frame:getParent() or frame
orphan = frame:newChild{}
+
local orphan = frame:newChild{}
 
--Allows parameters to be added (see example at Module:RoundN/doc and Module:RoundN/testcases/2)
 
--Allows parameters to be added (see example at Module:RoundN/doc and Module:RoundN/testcases/2)
 
if frame.args.passArgs or passArgs then
 
if frame.args.passArgs or passArgs then
Line 11: Line 11:
 
end
 
end
 
orphan.getParent = function() return nil end
 
orphan.getParent = function() return nil end
 +
local farg = frame.args[arg or 1] or ''
 +
--less decoded version if Template:Escape is used instead of nowiki:
 +
if (farg):match('nowiki') then
 +
local noNoWiki = mw.text.nowiki(mw.text.decode(mw.text.unstripNoWiki(farg)))
 +
end
 
return {
 
return {
source = mw.text.nowiki(mw.text.decode(mw.text.unstripNoWiki(frame.args[arg or 1] or ''))),
+
source = noNoWiki or farg,
output = orphan:preprocess(mw.text.unstripNoWiki(frame.args[arg or 1] or '')),
+
output = orphan:preprocess(noNoWiki and farg or mw.text.unstripNoWiki(farg)),
 
frame = frame
 
frame = frame
 
}
 
}

Revision as of 19:31, 16 January 2015

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

local p = {}

function p.get(frame, arg, passArgs)
	frame = frame:getParent() or frame
	local orphan = frame:newChild{}
	--Allows parameters to be added (see example at Module:RoundN/doc and Module:RoundN/testcases/2)
	if frame.args.passArgs or passArgs then
		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.
		orphan.args[arg or 1] = nil
	end
	orphan.getParent = function() return nil end
	local farg = frame.args[arg or 1] or ''
	--less decoded version if Template:Escape is used instead of nowiki:
	if (farg):match('nowiki') then
		local noNoWiki = mw.text.nowiki(mw.text.decode(mw.text.unstripNoWiki(farg)))
	end
	return {
		source = noNoWiki or farg,
		output = orphan:preprocess(noNoWiki and farg or mw.text.unstripNoWiki(farg)),
		frame = frame
	}
end

function p.main(frame)
	local show = p.get(frame)
	local fbr, br = show.frame.args.br, ''
	if not fbr or tonumber(fbr) then
		for k = 1, (fbr or 1) do
			br = br .. '<br>'
		end
	else
		br = fbr
	end
	if show[show.frame.args.result_arg] then
		return show[show.frame.args.result_arg]
	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

return p