Editing Module:Demo

From MINR.ORG WIKI

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
 
local p = {}
 
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)
 
local parent = frame:getParent() or frame
 
local orphan = parent:newChild{}
 
orphan.getParent = parent.getParent --returns nil
 
orphan.args = {}
 
if onlyTheseKeys then
 
local family = {parent, frame}
 
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 p.get(frame, arg, passArgs)
 
function p.get(frame, arg, passArgs)
local orphan, frame = disinherit(frame, passArgs and {arg or 1})
+
frame = frame:getParent() or frame
local code, noWiki, preserve = frame.args[arg or 1] or ''
+
orphan = frame:newChild{}
if code:match'nowiki' then
+
--Allows parameters to be added (see example at Module:RoundN/doc and Module:RoundN/testcases/2)
local placeholder, preserve = ('6'):char(), {}
+
if frame.args.passArgs or passArgs then
code = mw.text.unstripNoWiki(code)
+
orphan.args = mw.clone(frame.args)
noWiki = code:gsub('%%', placeholder):gsub('&lt;', '<'):gsub('&gt;', '>')
+
--Set arg to something else if you want to pass your source code to the child for some reason.
for k in noWiki:gmatch('&.-;') do
+
orphan.args[arg or 1] = nil
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
 +
orphan.getParent = function() return nil end
 
return {
 
return {
source = noWiki or code,
+
source = mw.text.nowiki(mw.text.decode(mw.text.unstripNoWiki(frame.args[arg or 1] or ''))),
output = orphan:preprocess(code):gsub(frame.args.demo_kill_categories and '%[%[Category.-%]%]' or '', ''),
+
output = orphan:preprocess(mw.text.unstripNoWiki(frame.args[arg or 1] or '')),
 
frame = frame
 
frame = frame
 
}
 
}
 
end
 
end
  
function p.main(frame, demoTable)
+
function p.main(frame)
local show = demoTable or p.get(frame)
+
local show = p.get(frame)
local args = show.frame.args
+
local fbr, br = show.frame.args.br, ''
args.br = tonumber(args.br or 1) and ('<br>'):rep(args.br or 1) or args.br or ''
+
if not fbr or tonumber(fbr) then
if show[args.result_arg] then
+
for k = 1, (fbr or 1) do
return show[args.result_arg]
+
br = br .. '<br>'
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',
 
'demo_module_func',
 
'demo_main',
 
'demo_br',
 
'demo_result_arg',
 
'demo_kill_categories'
 
})
 
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 named = {insert = function(self, ...) table.insert(self, ...) return self end}
 
local source = {insert = named.insert, '{{', frame.args.demo_template or frame.args.demo_module, '\n'}
 
if not template then
 
source:insert(2, '#invoke:'):insert(4, '|'):insert(5, demoFunc)
 
 
end
 
end
local insertNamed = #source + 1
 
for k, v in pairs(orphan.args) do
 
local nan, insert = type(k) ~= 'number', {v}
 
local target = nan and named or source
 
target:insert'|'
 
if nan then
 
target:insert(k):insert'=':insert'\n'
 
table.insert(insert, 1, #target)
 
end
 
target:insert(unpack(insert))
 
local nowiki = v:match('nowiki')
 
if nowiki or v:match('{{.-}}') then
 
orphan.args[k] = frame:preprocess(nowiki and mw.text.unstripNoWiki(v) or v)
 
end
 
end
 
source:insert'}}'
 
table.insert(source, insertNamed, table.concat(named))
 
return p.main(orphan, {
 
source = mw.text.encode(table.concat(source), "<>'|=~"),
 
output = tostring(demoModule(orphan)):gsub(frame.args.demo_kill_categories and '%[%[Category.-%]%]' or '', ''),
 
frame = frame
 
})
 
 
else
 
else
return "ERROR: Invalid module function: "..demoFunc
+
br = fbr
 +
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

Please note that all contributions to MINR.ORG WIKI may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see MINR.ORG WIKI:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)