模組:Demo

文档图示 模块文档

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 providedfunction disinherit(frame, onlyTheseKeys)local parent = frame:getParent() or framelocal orphan = parent:newChild{}orphan.getParent = parent.getParent --returns nilorphan.args = {}if onlyTheseKeys thenlocal family = {parent, frame}for f = 1, 2 dofor k, v in pairs(family[f] and family[f].args or {}) doorphan.args[k] = orphan.args[k] or vendendparent.args = mw.clone(orphan.args)setmetatable(orphan.args, nil)for _, k in ipairs(onlyTheseKeys) dorawset(orphan.args, k, nil)endendreturn orphan, parentendfunction 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' thenlocal placeholder, preserve = ('6'):char(), {}code = mw.text.unstripNoWiki(code)noWiki = code:gsub('%%', placeholder):gsub('&lt;', '<'):gsub('&gt;', '>')for k in noWiki:gmatch('&.-;') doif not preserve[k] thenpreserve[k] = truetable.insert(preserve, (k:gsub('&', '&amp;')))noWiki = noWiki:gsub('(&.-;)', '%%%s')endendnoWiki = mw.text.nowiki(noWiki):format(unpack(preserve)):gsub(placeholder, '%%')endlocal kill_categories = frame.args.demo_kill_categories or frame.args.nocatreturn {source = noWiki or code,output = orphan:preprocess(code):gsub(kill_categories and '%[%[Category.-%]%]' or '', ''),frame = frame}endfunction p.main(frame, demoTable)local show = demoTable or p.get(frame)local args = show.frame.argsargs.br = tonumber(args.br or 1) and ('<br>'):rep(args.br or 1) or args.br or ''if show[args.result_arg] thenreturn show[args.result_arg]endreturn 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-- Alternate function to return an inline resultfunction p.inline(frame, demoTable)local show = demoTable or p.get(frame)local args = show.frame.argsif show[args.result_arg] thenreturn show[args.result_arg]endreturn string.format('<code%s>%s</code>%s%s', args.style and string.format(" style='%s'", args.style) or '', show.source, ' → ', show.output)end--passing of args into other module without preprocessingfunction 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','nocat'})local template = frame.args.demo_template and 'Template:'..frame.args.demo_templatelocal 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_arglocal kill_categories = frame.args.demo_kill_categories or frame.args.nocatif demoModule thenlocal 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 thensource:insert(2, '#invoke:'):insert(4, '|'):insert(5, demoFunc)endlocal insertNamed = #source + 1for k, v in pairs(orphan.args) dolocal nan, insert = type(k) ~= 'number', {v}local target = nan and named or sourcetarget:insert'|'if nan thentarget:insert(k):insert'=':insert'\n'table.insert(insert, 1, #target)endtarget:insert(unpack(insert))local nowiki = v:match('nowiki')if nowiki or v:match('{{.-}}') thenorphan.args[k] = frame:preprocess(nowiki and mw.text.unstripNoWiki(v) or v)endendsource:insert'}}'table.insert(source, insertNamed, table.concat(named))return p.main(orphan, {source = mw.text.encode(table.concat(source), "<>'|=~"),output = tostring(demoModule(orphan)):gsub(kill_categories and '%[%[Category.-%]%]' or '', ''),frame = frame})elsereturn "ERROR: Invalid module function: "..demoFuncendendreturn p