Jump to content
Wikipedia The Free Encyclopedia

Module:Demo

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
Page extended-confirmed-protected This module is currently under extended confirmed protection.
Extended confirmed protection prevents edits from all unregistered editors and registered users with fewer than 30 days tenure and 500 edits. The policy on community use specifies that extended confirmed protection can be applied to combat disruption, if semi-protection has proven to be ineffective. Extended confirmed protection may also be applied to enforce arbitration sanctions. Please discuss any changes on the talk page; you may submit an edit request to ask for uncontroversial changes supported by consensus.

Usage

Usage via templates

This module supports {{Demo }}

{{#invoke:Demo|main}}

and {{Demo inline }}

{{#invoke:Demo|inline}}

The input must be wrapped in <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:

  • source = the source code (without <syntaxhighlight> wrappers, characters substituted with html entities)
  • output = the execution result of the source.
  • frame = the frame from which this template took the parameter.

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:

localp={}
functionp.main(frame)
localparts=require('Module:demo').get(frame)
return'...Pretty HTML... <pre>'..parts.source..'</pre> ...More pretty HTML... '..parts.output..' ...Even more pretty HTML...'
end
returnp

See also

The above documentation is transcluded from Module:Demo/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.

 localp={}

 --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
 functiondisinherit(frame,onlyTheseKeys)
 localparent=frame:getParent()orframe
 localorphan=parent:newChild{}
 orphan.getParent=parent.getParent--returns nil
 orphan.args={}
 ifonlyTheseKeysthen
 localfamily={parent,frame}
 forf=1,2do
 fork,vinpairs(family[f]andfamily[f].argsor{})do
 orphan.args[k]=orphan.args[k]orv
 end
 end
 parent.args=mw.clone(orphan.args)
 setmetatable(orphan.args,nil)
 for_,kinipairs(onlyTheseKeys)do
 rawset(orphan.args,k,nil)
 end
 end
 returnorphan,parent
 end

 functiongetSeparator(args,default)
 localbr=tonumber(args.br)and('<br>'):rep(args.br)orargs.br
 localsep=args.seporbrordefault
 return#sep>0and' '..sep..' 'orsep
 end

 functionp.get(frame,arg,passArgs)
 localorphan,frame=disinherit(frame,passArgsand{argor1})
 localcode=frame.args[argor1]or''
 ifcode: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
 localkill_categories=frame.args.demo_kill_categoriesorframe.args.nocat
 return{
 source=code,
 output=orphan:preprocess(code):gsub(kill_categoriesand'%[%[Category.-%]%]'or'',''),
 frame=frame
 }
 end

 functionp.main(frame,demoTable)
 localshow=demoTableorp.get(frame)
 localargs=show.frame.args
 ifshow[args.result_arg]then
 returnshow[args.result_arg]
 end
 localyesno=require('Module:Yesno')
 args.reverse=yesno(args.reverse,false)
 args.sep=getSeparator(args,'')
 localsource=frame:extensionTag{
 name='syntaxhighlight',
 args={
 lang='wikitext',
 style=args.style
 },
 content=show.source
 }
 returnargs.reverseand
 show.output..args.sep..sourceor
 source..args.sep..show.output
 end

 -- Alternate function to return an inline result
 functionp.inline(frame,demoTable)
 localshow=demoTableorp.get(frame)
 localargs=show.frame.args
 ifshow[args.result_arg]then
 returnshow[args.result_arg]
 end
 localyesno=require('Module:Yesno')
 args.reverse=yesno(args.reverse,false)
 args.sep=getSeparator(args,args.reverseand'←'or'→')
 localsource=frame:extensionTag{
 name='syntaxhighlight',
 args={
 lang='wikitext',
 inline=true,
 style=args.style
 },
 content=show.source
 }
 returnargs.reverseand
 show.output..args.sep..sourceor
 source..args.sep..show.output
 end

 --passing of args into other module without preprocessing
 functionp.module(frame)
 localorphan,frame=disinherit(frame,{
 'demo_template',
 'demo_module',
 'demo_module_func',
 'demo_main',
 'demo_sep',
 'demo_br',
 'demo_result_arg',
 'demo_kill_categories',
 'nocat'
 })
 localtemplate=frame.args.demo_templateand'Template:'..frame.args.demo_template
 localdemoFunc=frame.args.demo_module_funcor'main\n'
 localdemoModule=require('Module:'..frame.args.demo_module)[demoFunc:match('^%s*(.-)%s*$')]
 frame.args.br,frame.args.result_arg=frame.args.demo_seporframe.args.demo_br,frame.args.demo_result_arg
 localkill_categories=frame.args.demo_kill_categoriesorframe.args.nocat
 ifdemoModulethen
 localnamed={insert=function(self,...)table.insert(self,...)returnselfend}
 localsource={insert=named.insert,'{{',frame.args.demo_templateorframe.args.demo_module,'\n'}
 ifnottemplatethen
 source:insert(2,'#invoke:'):insert(4,'|'):insert(5,demoFunc)
 end
 localinsertNamed=#source+1
 fork,vinpairs(orphan.args)do
 localnan,insert=type(k)~='number',{v}
 localtarget=nanandnamedorsource
 target:insert'|'
 ifnanthen
 target:insert(k):insert'=':insert'\n'
 table.insert(insert,1,#target)
 end
 target:insert(unpack(insert))
 localnowiki=v:match('nowiki')
 ifnowikiorv:match('{{.-}}')then
 orphan.args[k]=frame:preprocess(nowikiandmw.text.unstripNoWiki(v)orv)
 end
 end
 source:insert'}}'
 table.insert(source,insertNamed,table.concat(named))
 returnp.main(orphan,{
 source=table.concat(source),"<>'|=~",
 output=tostring(demoModule(orphan)):gsub(kill_categoriesand'%[%[Category.-%]%]'or'',''),
 frame=frame
 })
 else
 return"ERROR: Invalid module function: "..demoFunc
 end
 end

 returnp

AltStyle によって変換されたページ (->オリジナル) /