Documentation for this module may be created at Module:Demo/doc

  1. local p = {}
  2.  
  3. --creates a frame object that cannot access any of the parent's args
  4. --unless a table containing a list keys of not to inherit is provided
  5. function disinherit(frame, onlyTheseKeys)
  6. local parent = frame:getParent() or frame
  7. local orphan = parent:newChild{}
  8. orphan.getParent = parent.getParent --returns nil
  9. orphan.args = {}
  10. if onlyTheseKeys then
  11. local family = {parent, frame}
  12. for f = 1, 2 do
  13. for k, v in pairs(family[f] and family[f].args or {}) do
  14. orphan.args[k] = orphan.args[k] or v
  15. end
  16. end
  17. parent.args = mw.clone(orphan.args)
  18. setmetatable(orphan.args, nil)
  19. for _, k in ipairs(onlyTheseKeys) do
  20. rawset(orphan.args, k, nil)
  21. end
  22. end
  23. return orphan, parent
  24. end
  25.  
  26. function p.get(frame, arg, passArgs)
  27. local orphan, frame = disinherit(frame, passArgs and {arg or 1})
  28. local code, noWiki, preserve = frame.args[arg or 1] or ''
  29. if code:match'nowiki' then
  30. local placeholder, preserve = ('6'):char(), {}
  31. code = mw.text.unstripNoWiki(code)
  32. noWiki = code:gsub('%%', placeholder):gsub('&lt;', '<'):gsub('&gt;', '>')
  33. for k in noWiki:gmatch('&.-;') do
  34. if not preserve[k] then
  35. preserve[k] = true
  36. table.insert(preserve, (k:gsub('&', '&amp;')))
  37. noWiki = noWiki:gsub('(&.-;)', '%%%s')
  38. end
  39. end
  40. noWiki = mw.text.nowiki(noWiki):format(unpack(preserve)):gsub(placeholder, '%%')
  41. end
  42. return {
  43. source = noWiki or code,
  44. output = orphan:preprocess(code):gsub(frame.args.demo_kill_categories and '%[%[Category.-%]%]' or '', ''),
  45. frame = frame
  46. }
  47. end
  48.  
  49. function p.main(frame, demoTable)
  50. local show = demoTable or p.get(frame)
  51. local args = show.frame.args
  52. args.br = tonumber(args.br or 1) and ('<br>'):rep(args.br or 1) or args.br or ''
  53. if show[args.result_arg] then
  54. return show[args.result_arg]
  55. end
  56. 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)
  57. end
  58.  
  59. --passing of args into other module without preprocessing
  60. function p.module(frame)
  61. local orphan, frame = disinherit(frame, {
  62. 'demo_template',
  63. 'demo_module',
  64. 'demo_module_func',
  65. 'demo_main',
  66. 'demo_br',
  67. 'demo_result_arg',
  68. 'demo_kill_categories'
  69. })
  70. local template = frame.args.demo_template and 'Template:'..frame.args.demo_template
  71. local demoFunc = frame.args.demo_module_func or 'main\n'
  72. local demoModule = require('Module:' .. frame.args.demo_module)[demoFunc:match('^%s*(.-)%s*$')]
  73. frame.args.br, frame.args.result_arg = frame.args.demo_br, frame.args.demo_result_arg
  74. if demoModule then
  75. local named = {insert = function(self, ...) table.insert(self, ...) return self end}
  76. local source = {insert = named.insert, '{{', frame.args.demo_template or frame.args.demo_module, '\n'}
  77. if not template then
  78. source:insert(2, '#invoke:'):insert(4, '|'):insert(5, demoFunc)
  79. end
  80. local insertNamed = #source + 1
  81. for k, v in pairs(orphan.args) do
  82. local nan, insert = type(k) ~= 'number', {v}
  83. local target = nan and named or source
  84. target:insert'|'
  85. if nan then
  86. target:insert(k):insert'=':insert'\n'
  87. table.insert(insert, 1, #target)
  88. end
  89. target:insert(unpack(insert))
  90. local nowiki = v:match('nowiki')
  91. if nowiki or v:match('{{.-}}') then
  92. orphan.args[k] = frame:preprocess(nowiki and mw.text.unstripNoWiki(v) or v)
  93. end
  94. end
  95. source:insert'}}'
  96. table.insert(source, insertNamed, table.concat(named))
  97. return p.main(orphan, {
  98. source = mw.text.encode(table.concat(source), "<>'|=~"),
  99. output = tostring(demoModule(orphan)):gsub(frame.args.demo_kill_categories and '%[%[Category.-%]%]' or '', ''),
  100. frame = frame
  101. })
  102. else
  103. return "ERROR: Invalid module function: "..demoFunc
  104. end
  105. end
  106.  
  107. return p