Module:Demo: Difference between revisions

244 bytes added ,  27 September 2024
copy from https://dev.fandom.com/wiki/Module:Demo
(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 getSeparator(args, default)
-- Creates a table and shows the results
local br = tonumber(args.br) and ('<br>'):rep(args.br) or args.br
function formatTable(source,output)
local sep = args.sep or br or default
    local hTable = mw.html.create("table"):css("width","100%")
return #sep > 0 and ' ' .. sep .. ' ' or sep
   
    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'UNIQ%-%-nowiki' then
if code:match'nowiki' then
code = mw.text.unstripNoWiki(code)
local placeholder, preserve = ('6'):char(), {}
:gsub('&lt;', '<')
code = mw.text.unstrip (code)
:gsub('&gt;', '>')
noWiki = code:gsub('%%', placeholder):gsub('&lt;', '<'):gsub('&gt;', '>')
:gsub('&quot;', '"')
for k in noWiki:gmatch('&.-;') do
-- Replace `&#125;%-` with `}-` because of some server quirk leading to
if not preserve[k] then
-- =mw.text.unstripNoWiki(mw.getCurrentFrame():preprocess('<nowiki>}-</nowiki>'))
preserve[k] = true
-- outputting `&#125;-` instead of `}-`, while it's ok with `<nowiki>} -</nowiki>`
table.insert(preserve, (k:gsub('&', '&amp;')))
:gsub('&#125;%-', '}-')
noWiki = noWiki:gsub('(&.-;)', '%%%s')
-- The same with `-&#123;`
end
:gsub('%-&#123;', '-{')
end
noWiki = mw.text.nowiki(noWiki):format(unpack(preserve)):gsub(placeholder, '%%')
end
end
local kill_categories = frame.args.demo_kill_categories or frame.args.nocat
return {
return {
source = code,
source = noWiki or code,
output = orphan:preprocess(code):gsub(kill_categories and '%[%[Category.-%]%]' or '', ''),
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
local yesno = require('Module:Yesno')
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)
args.reverse = yesno(args.reverse, false)
args.sep = getSeparator(args, '')
local source = frame:extensionTag{
name = 'syntaxhighlight',
args = {
lang = 'wikitext',
style = args.style
},
content = show.source
}
return args.reverse and
show.output .. args.sep .. source or
source .. args.sep .. show.output
end
end


Line 82: Line 95:
return show[args.result_arg]
return show[args.result_arg]
end
end
local yesno = require('Module:Yesno')
return string.format('<code%s>%s</code>%s%s', args.style and string.format(" style='%s'", args.style) or '', show.source, ' ', show.output)
args.reverse = yesno(args.reverse, false)
args.sep = getSeparator(args, args.reverse and '←' or '')
local source =  frame:extensionTag{
name = 'syntaxhighlight',
args = {
lang = 'wikitext',
inline = true,
style = args.style
},
content = show.source
}
return args.reverse and
show.output .. args.sep .. source or
source .. args.sep .. show.output
end
end


Line 106: Line 105:
'demo_module_func',
'demo_module_func',
'demo_main',
'demo_main',
'demo_sep',
'demo_br',
'demo_br',
'demo_result_arg',
'demo_result_arg',
'demo_kill_categories',
'demo_kill_categories'
'nocat'
})
})
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_sep or 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
local kill_categories = frame.args.demo_kill_categories or frame.args.nocat
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(kill_categories and '%[%[Category.-%]%]' or '', ''),
output = tostring(demoModule(orphan)):gsub(frame.args.demo_kill_categories and '%[%[Category.-%]%]' or '', ''),
frame = frame
frame = frame
})
})