Jump to content

Module:SimpleDebug

From Wikipedia, the free encyclopedia

--2020-06-16 fix error when vtos(nil), then it showed two nil--2020-06-08 if a variable is a function now is displayed as function (before "function")--2020-06-06 fix error which occasionally happens when a value == nillocalp={}p.s=''p.tab={oneline=true,allidx=false,}p.dec=-1p.maxlines={num=100,doerror=true,}p.enabled=truep.nowiki=falsep.nohtml=falsep._plaintext=falsep.counter=falselocalLinCount=0localvep='  •  'localfunctionMessRaised(n)return'\n\nIt has been reached to '..n..', you can change this limit with "maxlines.num".'endlocalfunctionarrow()return' => 'endfunctionp.breakline()LinCount=LinCount+1p.s=p.s..'\n\n'ifp.counterthenp.s=p.s..LinCount..vependif(LinCount>p.maxlines.num)andp.maxlines.doerrorthenp.pa=p.s..MessRaised(p.maxlines.num)error(p.s,0)endend--breaklinelocalfunctionCheckWhereName(wn,what)ifwn==nilthenreturn'"'..what..'" == nil'elseif(type(wn)=="table")thenreturn'Table as "'..what..'"!'elsereturnwnendend--CheckWhereNamefunctionp._plain(text)--Modified from "Module:Plain text"ifnottextthenreturnendtext=mw.text.killMarkers(text):gsub('&nbsp;',' ')--replace nbsp spaces with regular spaces:gsub('<br ?/?>',', ')--replace br with commas:gsub('<span.->(.-)</span>','%1')--remove spans while keeping text inside:gsub('<b>(.-)</b>','%1')--remove bold while keeping text inside:gsub('<i>(.-)</i>','%1')--remove italic while keeping text inside:gsub('<sub>(.-)</sub>','%1')--remove bold while keeping text inside:gsub('<sup>(.-)</sup>','%1')--remove bold while keeping text inside:gsub('<.->.-<.->','')--strip out remaining tags and the text inside:gsub('<.->','')--remove any other tag markup:gsub('%[%[%s*[Ff]ile%s*:.-%]%]','')--strip out files:gsub('%[%[%s*[Ii]mage%s*:.-%]%]','')--strip out use of image::gsub('%[%[%s*[Cc]ategory%s*:.-%]%]','')--strip out categories:gsub('%[%[[^%]]-|','')--strip out piped link text:gsub('[%[%]]','')--then strip out remaining [ and ]:gsub("'''''","")--strip out bold italic markup:gsub("'''?","")--not stripping out '''' gives correct output for bolded text in quotes:gsub('----','')--remove ---- lines:gsub("^%s+","")--strip leading:gsub("%s+$","")--and trailing spaces:gsub("%s+"," ")--strip redundant spacesreturntextend--plainfunctionp._plain_len(text)returnmw.ustring.len(p._plain(text))endfunctionp.plain(frame)returnp._plain(frame.args[1])endfunctionp.plain_len(frame)returnp._plain_len(frame.args[1])endlocalfunctiontotext(text)ifp._plaintextthenreturnp._plain(text)elsereturntextendend--totextlocalfunctionNumToStr(N)if(p.dec==-1)or(N==math.floor(N))thenreturntostring(N)elsereturntostring(math.floor((N*10^p.dec)+0.5)/(10^p.dec))endend--NumToStrlocaliniTab1Line=truefunctionp.containsTab(avar)localresult=falsefork,vinpairs(avar)doiftype(v)=='table'thenresult=truebreakendendreturnresultend--containsTablocalvarlocalfunctionDumTab(tbl,indent)ifnotindentthenindent=1endlocaltoprint=" {\r\n"indent=indent+2fork,vinpairs(tbl)dotoprint=toprint..string.rep(" ",indent)localid=kif(type(k)=="string")thenk='"'..k..'"'endtoprint=toprint.."["..k.."] = "if(type(v)=="number")thentoprint=toprint..NumToStr(v)..",\r\n"elseif(type(v)=="string")thentoprint=toprint.."\""..totext(v).."\",\r\n"elseif(type(v)=="table")thenifiniTab1Lineand(notp.containsTab(v))thenlocalwds='{'forkk,vvinpairs(v)doif(p.tab.allidx==true)or(type(kk)~='number')thenwds=wds..'['..kk..']='..var(vv)..', 'elsewds=wds..var(vv)..', 'endendtoprint=toprint..wds.."},\r\n"elsetoprint=toprint..DumTab(v,indent+2)..",\r\n"endelsetoprint=toprint.."\""..tostring(v).."\",\r\n"endendtoprint=toprint..string.rep(" ",indent-2).."}"returntoprintend--DumTabfunctionvar(avar)localEndStr=''ifavar==nilthenEndStr='nil'elseiftype(avar)=='table'thenif#avar>0thenp.s=p.s..'\r\n'endifp.tab.onelinethenlocalwds='{ 'fork,vinpairs(avar)doif(p.tab.allidx==true)or(type(k)~='number')thenwds=wds..'['..k..']='..var(v)..', 'elsewds=wds..var(v)..', 'endendEndStr=wds..'} 'elseEndStr=DumTab(avar)endelseiftype(avar)=='number'thenEndStr=NumToStr(avar)elseiftype(avar)=='boolean'thenifavar==truethenEndStr='true'elseEndStr='false'endelseiftype(avar)=='function'thenEndStr='function'elseavar=totext(tostring(avar))ifp.nohtmlthenavar=string.gsub(avar,"<","⪡")avar=string.gsub(avar,">","⪢")endEndStr='"'..avar..'"'endreturnEndStrend--varfunctionp.w(where)ifp.enabledthenreturnCheckWhereName(where,'w')endend--wlocalfunctionvarx(avar)iniTab1Line=p.tab.onelineifp.tab.onelineand(type(avar)=='table')thenp.tab.oneline=notp.containsTab(avar)endlocalss=var(avar)p.tab.oneline=iniTab1Linereturnssend--varxfunctionp.v(...)ifp.enabledthenlocalstr=''if#arg==0thenstr='nil'elselocalc=0fork,iinipairs(arg)doc=kend--error (c)fori=1,#argdoifstr~=''thenstr=str..vependstr=str..varx(arg[i])endendreturnstrendend--vfunctionp.wv(where,...)ifp.enabledthenreturnCheckWhereName(where,'w')..arrow()..p.v(unpack(arg))endend--wvfunctionp.nv(...)ifp.enabledthenifmath.mod(#arg,2)~=0thenEndStr='Any parameter has not a name or variable'elselocals=''localIsName=truefunctionConcat(wds)ifs~=''thenifIsNamethens=s..vepelses=s..': 'endends=s..wdsendfori=1,#argdoifIsNamethenConcat(CheckWhereName(arg[i],'n'))IsName=falseelseConcat(varx(arg[i]))IsName=trueendendEndStr=sendreturnEndStrendend--nvfunctionp.wnv(where,...)ifp.enabledthenreturnCheckWhereName(where,'w')..arrow()..p.nv(unpack(arg))endend----------localfunctionEnabAndBl()ifp.enabledthenifLinCount<p.maxlines.numthenp.breakline()returntrueelsep.s=p.s..MessRaised(p.maxlines.num)error(p.s)returnfalseendelsereturnfalseendend--EnabAndBlfunctionp.wtos(where)ifEnabAndBl()thenp.s=p.s..p.w(where)endend--wtosfunctionp.vtos(...)ifEnabAndBl()thenlocalend_nil_count=arg["n"]-#argp.s=p.s..p.v(unpack(arg))if#arg==0thenend_nil_count=end_nil_count-1endfori=1,end_nil_countdop.s=p.s..vep..'nil'endendend--vtosfunctionp.wvtos(where,...)ifEnabAndBl()thenp.s=p.s..p.wv(where,unpack(arg))endend--wvtosfunctionp.nvtos(...)ifEnabAndBl()thenlocalend_nil_count=arg["n"]-#argifend_nil_count>0thenfori=1,arg["n"]doifmath.mod(i,2)~=0thenp.s=p.s..arg[i]..': 'elsep.s=p.s..p.v(arg[i])ifi<arg["n"]thenp.s=p.s..vependendendelsep.s=p.s..p.nv(unpack(arg))endendend--nvtosfunctionp.wnvtos(where,...)ifEnabAndBl()thenlocalend_nil_count=arg["n"]-#argifend_nil_count>0thenp.s=p.s..where..arrow()fori=1,arg["n"]doifmath.mod(i,2)~=0thenp.s=p.s..arg[i]..': 'elsep.s=p.s..p.v(arg[i])ifi<arg["n"]thenp.s=p.s..vependendendelsep.s=p.s..p.wnv(where,unpack(arg))endendend--wnvtosreturnp
close