Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Usage  



1.1  Usage via templates  





1.2  Usage in a module  







2 See also  














Module:Demo






Аԥсшәа
العربية
Azərbaycanca
تۆرکجه

 / Bân-lâm-gú
Беларуская
Беларуская (тарашкевіца)

Dansk
الدارجة
Ελληνικά
Эрзянь
فارسی
Føroyskt
ГӀалгӀай
/Hak-kâ-ngî

Ilokano
Bahasa Indonesia
Kurdî
Ladin

Bahasa Melayu
 / Mìng-dĕ̤ng-nḡ
Мокшень


Нохчийн
ି
Oʻzbekcha / ўзбекча

پښتو
Português
Русский

Simple English
Slovenščina
Српски / srpski
Suomi

Türkçe
Українська
Tiếng Vit


 

Edit links
 









Module
Talk
 

















Read
View source
View history
 








Tools
   


Actions  



Read
View source
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 




In other projects  



MediaWiki
Wikispecies
Wikisource
Wikiversity
 
















Appearance
   

 





Permanently protected module

From Wikipedia, the free encyclopedia
 


Usage

Usage via templates

This module supports {{Demo}}

{{#invoke:Demo|main}}

and {{Demo inline}}

{{#invoke:Demo|inline}}

The input must be wrapped in <nowiki>...</nowiki> tags or else it may be processed before the module can read it.

Usage in a module

If you want to use this in another module (such as to make the output prettier), you can get values like so:

require('Module:demo').get(frame)

Function get() returns a table containing:

By default, get() takes the first parameter of frame. If the frame uses a different parameter name for the nowiki-wrapped source, then place that name (as a string) as the second parameter, like so require('Module:demo').get(frame, 'alternate_name')

Example:

local p = {}

function p.main(frame)
 local parts = require('Module:demo').get(frame)
 return '…Pretty HTML… <pre>' .. parts.source .. '</pre> …More pretty HTML… ' .. parts.output .. ' …Even more pretty HTML…'
end

return p

See also

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 parent = frame:getParent() or frame
 local orphan = parent:newChild{}
 orphan.getParent = parent.getParent --returns nil
 orphan.args = {}
 if onlyTheseKeys then
  local family = {parent, frame}
  for f = 1, 2 do
   for k, v in pairs(family[f] and family[f].args or {}) do
    orphan.args[k] = orphan.args[k] or v
   end
  end
  parent.args = mw.clone(orphan.args)
  setmetatable(orphan.args, nil)
  for _, k in ipairs(onlyTheseKeys) do
   rawset(orphan.args, k, nil)
  end
 end
 return orphan, parent
end

function getSeparator(args, default)
 local br = tonumber(args.br) and ('<br>'):rep(args.br) or args.br
 local sep = args.sep or br or default
 return #sep > 0 and ' ' .. sep .. ' ' or sep
end

function p.get(frame, arg, passArgs)
 local orphan, frame = disinherit(frame, passArgs and {arg or 1})
 local code = frame.args[arg or 1] or ''
 if code:match'UNIQ%-%-nowiki' then
  code = mw.text.unstripNoWiki(code)
   :gsub('&lt;', '<')
   :gsub('&gt;', '>')
   :gsub('&quot;', '"')
   -- Replace `&#125;%-` with `}-` because of some server quirk leading to
   -- =mw.text.unstripNoWiki(mw.getCurrentFrame():preprocess('<nowiki>}-</nowiki>'))
   -- outputting `&#125;-` instead of `}-`, while it's ok with `<nowiki>} -</nowiki>`
   :gsub('&#125;%-', '}-')
   -- The same with `-&#123;`
   :gsub('%-&#123;', '-{')
 end
 local kill_categories = frame.args.demo_kill_categories or frame.args.nocat
 return {
  source = code,
  output = orphan:preprocess(code):gsub(kill_categories and '%[%[Category.-%]%]' or '', ''),
  frame = frame
 }
end

function p.main(frame, demoTable)
 local show = demoTable or p.get(frame)
 local args = show.frame.args
 if show[args.result_arg] then
  return show[args.result_arg]
 end
 local yesno = require('Module:Yesno')
 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

-- Alternate function to return an inline result
function p.inline(frame, demoTable)
 local show = demoTable or p.get(frame)
 local args = show.frame.args
 if show[args.result_arg] then
  return show[args.result_arg]
 end
 local yesno = require('Module:Yesno')
 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

--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_sep',
  'demo_br',
  'demo_result_arg',
  'demo_kill_categories',
  'nocat'
 })
 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_sep or frame.args.demo_br, frame.args.demo_result_arg
 local kill_categories = frame.args.demo_kill_categories or frame.args.nocat
 if demoModule then
  local 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 then
   source:insert(2, '#invoke:'):insert(4, '|'):insert(5, demoFunc)
  end
  local insertNamed = #source + 1
  for k, v in pairs(orphan.args) do
   local nan, insert = type(k) ~= 'number', {v}
   local target = nan and named or source
   target:insert'|'
   if nan then
    target:insert(k):insert'=':insert'\n'
    table.insert(insert, 1, #target)
   end
   target:insert(unpack(insert))
   local nowiki = v:match('nowiki')
   if nowiki or v:match('{{.-}}') then
    orphan.args[k] = frame:preprocess(nowiki and mw.text.unstripNoWiki(v) or v)
   end
  end
  source:insert'}}'
  table.insert(source, insertNamed, table.concat(named))
  return p.main(orphan, {
   source = table.concat(source), "<>'|=~",
   output = tostring(demoModule(orphan)):gsub(kill_categories and '%[%[Category.-%]%]' or '', ''),
   frame = frame
  })
 else
  return "ERROR: Invalid module function: "..demoFunc
 end
end

return p

Retrieved from "https://en.wikipedia.org/w/index.php?title=Module:Demo&oldid=1216527407"

Category: 
Modules for general use
Hidden category: 
Wikipedia extended-confirmed-protected modules
 



This page was last edited on 31 March 2024, at 14:46 (UTC).

Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



Privacy policy

About Wikipedia

Disclaimers

Contact Wikipedia

Code of Conduct

Developers

Statistics

Cookie statement

Mobile view



Wikimedia Foundation
Powered by MediaWiki