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 4: Line 4:
 
--unless a table containing a list keys of not to inherit is provided
 
--unless a table containing a list keys of not to inherit is provided
 
function disinherit(frame, onlyTheseKeys)
 
function disinherit(frame, onlyTheseKeys)
local parent = frame:getParent() or frame
+
frame = frame:getParent() or frame
local orphan = parent:newChild{}
+
local orphan = frame:newChild{}
orphan.getParent = parent.getParent --returns nil
+
orphan.getParent = frame.getParent --returns nil
orphan.args = {}
 
 
if onlyTheseKeys then
 
if onlyTheseKeys then
local family = {parent, frame}
+
orphan.args = mw.clone(frame.args)
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
 
for _, k in ipairs(onlyTheseKeys) do
rawset(orphan.args, k, nil)
+
orphan.args[k] = nil
 
end
 
end
 
end
 
end
return orphan, parent
+
return orphan, frame
 
end
 
end
  
Line 42: Line 34:
 
return {
 
return {
 
source = noWiki or code,
 
source = noWiki or code,
output = orphan:preprocess(code):gsub(frame.args.demo_kill_categories and '%[%[Category.-%]%]' or '', ''),
+
output = orphan:preprocess(code),
 
frame = frame
 
frame = frame
 
}
 
}
Line 61: Line 53:
 
local orphan, frame = disinherit(frame, {
 
local orphan, frame = disinherit(frame, {
 
'demo_template',
 
'demo_template',
'demo_module',
 
 
'demo_module_func',
 
'demo_module_func',
 
'demo_main',
 
'demo_main',
 
'demo_br',
 
'demo_br',
'demo_result_arg',
+
'demo_result_arg'
'demo_kill_categories'
 
 
})
 
})
 
local template = frame.args.demo_template and 'Template:'..frame.args.demo_template
 
local template = frame.args.demo_template and 'Template:'..frame.args.demo_template
 
local demoFunc = frame.args.demo_module_func or 'main\n'
 
local demoFunc = frame.args.demo_module_func or 'main\n'
local demoModule = require('Module:' .. frame.args.demo_module)[demoFunc:match('^%s*(.-)%s*$')]
+
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
 
frame.args.br, frame.args.result_arg = frame.args.demo_br, frame.args.demo_result_arg
 
if demoModule then
 
if demoModule then
local named = {insert = function(self, ...) table.insert(self, ...) return self end}
+
local source = template and {'{{', template and '' or '#invoke:', frame.args.demo_template or frame.args.demo_module, template and '' or '|', demoFunc}
local source = {insert = named.insert, '{{', frame.args.demo_template or frame.args.demo_module, '\n'}
+
for k, v in pairs(frame.args) do
if not template then
+
table.insert(
source:insert(2, '#invoke:'):insert(4, '|'):insert(5, demoFunc)
+
source,
end
+
tonumber(k) and
local insertNamed = #source + 1
+
'|'..v
for k, v in pairs(orphan.args) do
+
or ('|%s = %s'):format(k, v)
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
 
end
source:insert'}}'
+
return p.main(frame, {
table.insert(source, insertNamed, table.concat(named))
+
source = mw.text.nowiki(table.concat(source)),
return p.main(orphan, {
+
output = demoModule(orphan),
source = mw.text.encode(table.concat(source), "<>'|=~"),
 
output = tostring(demoModule(orphan)):gsub(frame.args.demo_kill_categories and '%[%[Category.-%]%]' or '', ''),
 
 
frame = frame
 
frame = frame
 
})
 
})

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)