Module:Demo/sandbox
Appearance
![]() | This is the module sandbox page for Module:Demo (diff). |
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
Usage
[edit]Usage via templates
[edit]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
[edit]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…'endreturnp
See also
[edit]- Template:Nowiki template demo which uses Module:Template test case
- Template:Automarkup which uses Module:Automarkup
localp={}localyn=require('Module:Yesno')localgetArgs=require('Module:Arguments').getArgs-- simple module that takes in an input and displays the input and output of a wikitext-- helper function which gets all the locations of all the matches of a ustringfunctionp._getAllMatchIndices(text,pattern)localoutput={}locali=0whilei~=nildoi=mw.ustring.find(text,pattern,i+1)ifi~=nilthentable.insert(output,i)endendreturnoutputend-- replaces all usages of \[, \], \<, \> \\, \{, and \} with [, ], <, >, \, {, and }-- also replaces line breaks, carriage returns, and tabs with their appropriate characterfunctionp._escapeAllCharacters(text)localindices=p._getAllMatchIndices(text,"\\")localsplitText=mw.text.split(text,'')localskip=falsefork,vinipairs(indices)doifnotskipthensplitText[v]=''localnc=splitText[v+1]splitText[v+1]=(nc=="e"and'='ornc=="p"and'|'ornc=='['andmw.getCurrentFrame():preprocess('<nowiki>[</nowiki>')ornc==']'andmw.getCurrentFrame():preprocess('<nowiki>]</nowiki>')ornc=='{'andmw.getCurrentFrame():preprocess('<nowiki>{</nowiki>')ornc=='}'andmw.getCurrentFrame():preprocess('<nowiki>}</nowiki>')ornc=='<'andmw.getCurrentFrame():preprocess('<nowiki><</nowiki>')ornc=='>'andmw.getCurrentFrame():preprocess('<nowiki>></nowiki>')ornc=='&'andmw.getCurrentFrame():preprocess('<nowiki>&</nowiki>')orsplitText[v+1])mw.log(splitText[v+1])ifnc=='\\'thenskip=trueendelseskip=falseendendreturntable.concat(splitText)endfunctionp._escapeHTMLCharCodes(str)localfunctionreplaceHTMLCharCodes(entity)mw.log(entity)localcharCode=mw.ustring.match(entity,"%d+")andtonumber(mw.ustring.match(entity,"%d+"))ormw.ustring.match(entity,"%w+;")mw.log(charCode)iftype(charCode)=='number'thenreturnmw.ustring.char(charCode)elselocalHTMLCharCodes={["amp;"]="&",["gt;"]=">",["lt;"]="<"}localreplacementChar=HTMLCharCodes[charCode]orentityreturnreplacementCharendendreturnmw.ustring.gsub(str,"%&%S-;",replaceHTMLCharCodes)endfunctionp._removeAllLinks(text)-- find all [ and ] characters and remove themlocalsplitText=mw.text.split(text,'')localnumberOfBrackets=0localendCharacter=falsefork,vinipairs(splitText)doifsplitText[k]=='['thennumberOfBrackets=numberOfBrackets+1endCharacter=falseifnumberOfBrackets>2thennumberOfBrackets=2elsesplitText[k]=''endelseifsplitText[k]==']'thennumberOfBrackets=numberOfBrackets-1endCharacter=falseifnumberOfBrackets<0thennumberOfBrackets=0elsesplitText[k]=''endelseifnumberOfBrackets==2thenifnotendCharacterthenendCharacter=splitText[k]=='|'splitText[k]=''endelseifnumberOfBrackets==1thenifnotendCharacterthenendCharacter=splitText[k]==' 'splitText[k]=''endendendendreturntable.concat(splitText)endfunctionp._removeXML(text)-- finds all xml tags and remove themlocalsplitText=mw.text.split(text,'')localnumberOfBrackets=0localnumberOfDoubleQuotes=0localnumberOfSingleQuotes=0fork,vinipairs(splitText)doifsplitText[k]=='<'thennumberOfBrackets=numberOfBrackets+1ifnumberOfBrackets>1thennumberOfBrackets=1elsesplitText[k]=''endelseifsplitText[k]=='>'thennumberOfBrackets=numberOfBrackets-1ifnumberOfBrackets<0thennumberOfBrackets=0elsesplitText[k]=''endelseifnumberOfBrackets==1thensplitText[k]=''endendendreturntable.concat(splitText)end-- from WikipedialocalfunctionmakeInvokeFunc(funcName)returnfunction(frame)localargs=getArgs(frame,{valueFunc=function(key,value)iftype(value)=='string'thenvalue=value:match('^%s*(.-)%s*$')-- Remove whitespace.ifkey=='heading'orvalue~=''thenreturnvalueelsereturnnilendelsereturnvalueendend})returnp[funcName](args)endendp.main=makeInvokeFunc("_main")functionp._main(args)localnowiki=yn(args['nowiki'])orfalselocalformat=args['format']or'block'localcode=p._code(args)localresult=p._result(args)ifformat=='inline'thenreturn'Using this code: '..code..' yields: '..resultelsereturn'<dl><dt>Using this code:</dt><dd>'..code..'</dd><dt>yields: </dt><dd>'..result..'</dd></dl>'--output the resultendendp.inline=makeInvokeFunc("_inline")functionp._inline(args)args['format']='inline'returnp._main(args)endp.block=makeInvokeFunc("_block")functionp._inline(args)args['format']='block'returnp._main(args)endp.code=makeInvokeFunc("_code")functionp._code(args)localnowiki=yn(args['nowiki'])orfalselocaltext=p._raw(args)localformat=args['format']or'block'localsyntaxhighlight=yn(args["syntaxhighlight"])ortrueifnotsyntaxhighlightthenlocalcode=format=='inline'andmw.getCurrentFrame():preprocess("<code>"..text.."</code>")ormw.getCurrentFrame():preprocess("<code style=\"display:inline-block;\">"..text.."</code>")returncodeelselocalcode=format=="inline"andmw.getCurrentFrame():preprocess("<syntaxhighlight inline lang=\"wikitext\">"..text.."</syntaxhighlight>")ormw.getCurrentFrame():preprocess("<syntaxhighlight lang=\"wikitext\">"..text.."</syntaxhighlight>")returncodeendendp.raw=makeInvokeFunc("_raw")functionp._raw(args)localnowiki=yn(args['nowiki'])orfalselocalsyntaxhighlight=yn(args["syntaxhighlight"])orfalselocaltext=(nowikiorsyntaxhighlight)andargs[1]orp._escapeAllCharacters(args[1])mw.log(text)returntextendp.result=makeInvokeFunc("_result")functionp._result(args)localnowiki=yn(args['nowiki'])orfalselocaltext=p._raw(args)mw.log(p._removeXML(p._removeAllLinks(text)))localresult=(yn(args['nowiki'])oryn(args['syntaxhighlight']))andmw.getCurrentFrame():preprocess(mw.text.unstripNoWiki(text))ormw.getCurrentFrame():preprocess(p._escapeHTMLCharCodes(mw.text.unstripNoWiki(p._removeXML(p._removeAllLinks(text)))))or''mw.log(result)returnresultendreturnp