# Module:Demo

local p = {}  
--creates a 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(, onlyTheseKeys)
local parent = :getParent or
local orphan = parent:newChild{}
orphan.getParent = parent.getParent --returns nil
orphan.args = {}
if onlyTheseKeys then
local family = {parent, }
for f = 1, 2 do
for k, v in pairs(family[f] and family[f].args or {}) do
orphan.args[k] = orphan.args[k] or v
end
end
parent.args = mw.clone(orphan.args)
setmetatable(orphan.args, nil)
for _, k in ipairs(onlyTheseKeys) do
rawset(orphan.args, k, nil)
end
end
return orphan, parent
end  
function getSeparator(args, default)
local br = tonumber(args.br) and ('
'):rep(args.br) or args.br
local sep = args.sep or br or default
return #sep > 0 and ' ' .. sep .. ' ' or sep
end  
function p.get(, arg, passArgs)
local orphan, = disinherit(, passArgs and {arg or 1})
local code = .args[arg or 1] or ''
if code:match'UNIQ%-%-nowiki' then
code = mw.text.unstripNoWiki(code)
:gsub('&lt;', '<')
:gsub('&gt;', '>')
:gsub('&quot;', '"')
-- Replace `&#125;%-` with `}-` because of some server quirk leading to
-- =mw.text.unstripNoWiki(mw.getCurrentFrame:preprocess(''))
-- outputting `&#125;-` instead of `}-`, while it's ok with ``
:gsub('&#125;%-', '}-')
-- The same with `-&#123;`
:gsub('%-&#123;', '-{')
end
local kill_categories = .args.demo_kill_categories or .args.nocat
return {
source = code,
output = orphan:preprocess(code):gsub(kill_categories and '%[%[Category.-%]%]' or *, *),
=
}
end  
function p.main(, demoTable)
local show = demoTable or p.get
local args = show..args
if show[args.result_arg] then
return show[args.result_arg]
end
local yesno = require('Module:Yesno')
args.reverse = yesno(args.reverse, false)
args.sep = getSeparator(args, '')
local source = :extensionTag{
name = 'syntaxhighlight',
args = {
lang = 'wikitext',
style = args.style
},
content = show.source
}
return args.reverse and
show.output .. args.sep .. source or
source .. args.sep .. show.output
end  
-- Alternate function to return an inline result
function p.inline(, demoTable)
local show = demoTable or p.get
local args = show..args
if show[args.result_arg] then
return show[args.result_arg]
end
local yesno = require('Module:Yesno')
args.reverse = yesno(args.reverse, false)
args.sep = getSeparator(args, args.reverse and '←' or '→')
local source = :extensionTag{
name = 'syntaxhighlight',
args = {
lang = 'wikitext',
inline = true,
style = args.style
},
content = show.source
}
return args.reverse and
show.output .. args.sep .. source or
source .. args.sep .. show.output
end  
--passing of args into other module without preprocessing
function p.module
local orphan, = disinherit(, {
'demo_template',
'demo_module',
'demo_module_func',
'demo_main',
'demo_sep',
'demo_br',
'demo_result_arg',
'demo_kill_categories',
'nocat'
})
local template = .args.demo_template and 'Template:'...args.demo_template
local demoFunc = .args.demo_module_func or 'main\n'
local demoModule = require('Module:' .. .args.demo_module)[demoFunc:match('^%s*(.-)%s*$')]
.args.br, .args.result_arg = .args.demo_sep or .args.demo_br, .args.demo_result_arg
local kill_categories = .args.demo_kill_categories or .args.nocat
if demoModule then
local named = {insert = function(self, ...) table.insert(self, ...) return self end}
local source = {insert = named.insert, ''
table.insert(source, insertNamed, table.concat(named))
return p.main(orphan, {
source = table.concat(source), "<>'|=~",
output = tostring(demoModule(orphan)):gsub(kill_categories and '%[%[Category.-%]%]' or *, *),
=
})
else
return "ERROR: Invalid module function: "..demoFunc
end
end  
return p