Difference between revisions of "Module:Demo"
From MINR.ORG WIKI
(html entities are now visible in pre; allow modules to be demonstrated directly) |
|||
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) | ||
− | frame | + | local orphan, parent, args = frame:newChild{}, frame:getParent(), {} |
− | + | orphan.getParent = (parent or frame).getParent --returns nil | |
− | orphan.getParent = frame.getParent --returns nil | + | --setmetatable(orphan.args, {index = args}) |
+ | --if 1 then return orphan, frame end | ||
if onlyTheseKeys then | if onlyTheseKeys then | ||
− | + | for _, f in ipairs(parent and {parent.args, frame.args} or {frame.args})do | |
+ | for k, v in pairs(f) do | ||
+ | args[k] = args[k] or v | ||
+ | end | ||
+ | end | ||
for _, k in ipairs(onlyTheseKeys) do | for _, k in ipairs(onlyTheseKeys) do | ||
− | + | args[k] = nil | |
end | end | ||
+ | orphan.args = args | ||
end | end | ||
return orphan, frame | return orphan, frame | ||
Line 53: | Line 59: | ||
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', | ||
Line 58: | Line 65: | ||
'demo_result_arg' | 'demo_result_arg' | ||
}) | }) | ||
+ | --if 1 then return mw.dumpObject(orphan.args) end | ||
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: | + | 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 source = | + | local source = {insert = function(self, ...) table.insert(self, ...) return self end, '{{', template and '' or '#invoke:', frame.args.demo_template or frame.args.demo_module, template and '' or '|', not template and demoFunc or '\n'} |
− | for k, v in pairs( | + | for k, v in pairs(orphan.args) do |
− | + | local nan, insert = type(k) ~= 'number', {v} | |
− | + | source:insert'|' | |
− | + | if nan then | |
− | + | source:insert(k):insert'=':insert'\n' | |
− | + | table.insert(insert, 1, #source) | |
− | + | end | |
+ | source:insert(unpack(insert)) | ||
+ | if v:match('nowiki') then | ||
+ | orphan.args = orphan:preprocess(mw.text.unstripNoWiki(v)) | ||
+ | end | ||
end | end | ||
− | return p.main( | + | source:insert'}}' |
− | source = mw.text. | + | return p.main(orphan, { |
− | output = demoModule(orphan), | + | source = mw.text.encode(table.concat(source), "<>'|=~"), |
+ | output = tostring(demoModule(orphan)), | ||
frame = frame | frame = frame | ||
}) | }) |
Revision as of 03:08, 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) local orphan, parent, args = frame:newChild{}, frame:getParent(), {} orphan.getParent = (parent or frame).getParent --returns nil --setmetatable(orphan.args, {index = args}) --if 1 then return orphan, frame end if onlyTheseKeys then for _, f in ipairs(parent and {parent.args, frame.args} or {frame.args})do for k, v in pairs(f) do args[k] = args[k] or v end end for _, k in ipairs(onlyTheseKeys) do args[k] = nil end orphan.args = args 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('<', '<'):gsub('>', '>') for k in noWiki:gmatch('&.-;') do if not preserve[k] then preserve[k] = true table.insert(preserve, (k:gsub('&', '&'))) 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', 'demo_module_func', 'demo_main', 'demo_br', 'demo_result_arg' }) --if 1 then return mw.dumpObject(orphan.args) end 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 = {insert = function(self, ...) table.insert(self, ...) return self end, '{{', template and '' or '#invoke:', frame.args.demo_template or frame.args.demo_module, template and '' or '|', not template and demoFunc or '\n'} for k, v in pairs(orphan.args) do local nan, insert = type(k) ~= 'number', {v} source:insert'|' if nan then source:insert(k):insert'=':insert'\n' table.insert(insert, 1, #source) end source:insert(unpack(insert)) if v:match('nowiki') then orphan.args = orphan:preprocess(mw.text.unstripNoWiki(v)) end end source:insert'}}' return p.main(orphan, { source = mw.text.encode(table.concat(source), "<>'|=~"), output = tostring(demoModule(orphan)), frame = frame }) else return "ERROR: Invalid module function: "..demoFunc end end return p