Bureaucrats, Moderators (CommentStreams), Interface administrators, Push subscription managers, Suppressors, Administrators
11,987
edits
(Undid revision 1240199590 by BrandonXLF (talk)) |
(copy from https://dev.fandom.com/wiki/Module:Demo) |
||
| Line 1: | Line 1: | ||
-- <pre> Description - Shows a demo of a wikitext | |||
-- Syntax : {{#invoke:demo|<nowiki>wikitext</nowiki>}} | |||
-- Note nowiki tag is necessary! | |||
-- </pre><nowiki> | |||
local p = {} | local p = {} | ||
local getArgs = require("Dev:Arguments").getArgs | |||
local frameTools = require("Dev:FrameTools") | |||
--creates a frame object that cannot access any of the parent's args | --creates a frame object that cannot access any of the parent's args | ||
| Line 5: | Line 11: | ||
function disinherit(frame, onlyTheseKeys) | function disinherit(frame, onlyTheseKeys) | ||
local parent = frame:getParent() or frame | local parent = frame:getParent() or frame | ||
local orphan = parent:newChild{} | local tmpFrame = frameTools.makePseudoFrame(parent) | ||
local orphan = tmpFrame:newChild{} | |||
-- local orphan = parent:newChild{} | |||
orphan.getParent = parent.getParent --returns nil | orphan.getParent = parent.getParent --returns nil | ||
orphan.args = {} | orphan.args = {} | ||
| Line 24: | Line 33: | ||
end | end | ||
function | -- Creates a table and shows the results | ||
function formatTable(source,output) | |||
local hTable = mw.html.create("table"):css("width","100%") | |||
hTable:addClass("wikitable") | |||
:tag("tr") | |||
:tag("th") | |||
:wikitext("Code"):done() | |||
:tag("th") | |||
:wikitext("Output"):done() | |||
hTable:tag("tr") | |||
:tag("td") | |||
:wikitext(source):done() | |||
:tag("td") | |||
:wikitext(output):done() | |||
return hTable | |||
end | end | ||
function p.get(frame, arg, passArgs) | function p.get(frame, arg, passArgs) | ||
local orphan, frame = disinherit(frame, passArgs and {arg or 1}) | local orphan, frame = disinherit(frame, passArgs and {arg or 1}) | ||
local code = frame.args[arg or 1] or '' | local code, noWiki, preserve = frame.args[arg or 1] or '' | ||
if code:match' | if code:match'nowiki' then | ||
code = mw.text. | local placeholder, preserve = ('6'):char(), {} | ||
code = mw.text.unstrip (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 | end | ||
return { | return { | ||
source = code, | source = noWiki or code, | ||
output = orphan:preprocess(code):gsub( | output = orphan:preprocess(code):gsub(frame.args.demo_kill_categories and '%[%[Category.-%]%]' or '', ''), | ||
frame = frame | frame = frame | ||
} | } | ||
| Line 56: | Line 77: | ||
local show = demoTable or p.get(frame) | local show = demoTable or p.get(frame) | ||
local args = show.frame.args | local args = show.frame.args | ||
if show[args.result_arg] then | args.br = tonumber(args.br or 1) and ('<br>'):rep(args.br or 1) or args.br or '' | ||
if getArgs(frame).table then | |||
-- local code = mw.text.unstripNoWiki(show.source) | |||
return formatTable(show.source, show.output) | |||
elseif show[args.result_arg] then | |||
return show[args.result_arg] | return show[args.result_arg] | ||
end | 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 | end | ||
| Line 82: | Line 95: | ||
return show[args.result_arg] | return show[args.result_arg] | ||
end | end | ||
return string.format('<code%s>%s</code>%s%s', args.style and string.format(" style='%s'", args.style) or '', show.source, ' → ', show.output) | |||
end | end | ||
| Line 106: | Line 105: | ||
'demo_module_func', | 'demo_module_func', | ||
'demo_main', | 'demo_main', | ||
'demo_br', | 'demo_br', | ||
'demo_result_arg', | 'demo_result_arg', | ||
'demo_kill_categories | '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.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 named = {insert = function(self, ...) table.insert(self, ...) return self end} | ||
| Line 141: | Line 137: | ||
table.insert(source, insertNamed, table.concat(named)) | table.insert(source, insertNamed, table.concat(named)) | ||
return p.main(orphan, { | return p.main(orphan, { | ||
source = table.concat(source), "<>'|=~", | source = mw.text.encode(table.concat(source), "<>'|=~"), | ||
output = tostring(demoModule(orphan)):gsub( | output = tostring(demoModule(orphan)):gsub(frame.args.demo_kill_categories and '%[%[Category.-%]%]' or '', ''), | ||
frame = frame | frame = frame | ||
}) | }) | ||