Jump to content

Module:Demo/sandbox

From Wikipedia, the free encyclopedia
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>&amp;</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
close