-- Source: https://runescape.wiki/w/Module:Mw.html_extension
-- <nowiki>
localp={}
localcheckType=require('libraryUtil').checkType
localmwHtml=getmetatable(mw.html.create()).__index-- Trick to get acces to the mw.html class
localstack={}-- Used to keep track of nested IF-END tags
localnoOp={}-- This object is returned by IF(false) tag
functionmwHtml:addClassIf(cond,class)
ifcondthen
returnself:addClass(class)
else
returnself
end
end
functionmwHtml:tagIf(cond,tagname)
ifcondthen
returnself:tag(tagname)
else
returnself
end
end
functionmwHtml:wikitextIf(cond,text)
ifcondthen
returnself:wikitext(text)
else
returnself
end
end
functionmwHtml:doneIf(cond)
ifcondthen
returnself:done()
else
returnself
end
end
functionmwHtml:attrIf(cond,name,value)
ifcondthen
returnself:attr(name,value)
else
returnself
end
end
functionmwHtml:cssIf(cond,name,value)
ifcondthen
returnself:css(name,value)
else
returnself
end
end
functionmwHtml:na()
returnself:tag('td')
:attr('data-sort-value',0)
:attr('class','table-na')
:wikitext('<small>N/A</small>')
:done()
end
functionmwHtml:naIf(cond)
ifcondthen
returnself:na()
else
returnself
end
end
localfunctionaddValues(self,settings)
-- wikitext and addClass are no-ops when their argument is nil
self:wikitext(settings[1]orsettings.wikitext)
self:addClass(settings.classorsettings.addClass)
ifsettings.attrthen
ifsettings.attr[1]andsettings.attr[2]then
self:attr(settings.attr[1],settings.attr[2])
else
self:attr(settings.attr)
end
end
ifsettings.cssthen
ifsettings.css[1]andsettings.css[2]then
self:css(settings.css[1],settings.css[2])
else
self:css(settings.css)
end
end
ifsettings.cssTextthen
self:cssText(settings.cssText)
end
end
functionmwHtml:tr(settings)
ifself.tagName=='tr'then
self=self:done():tag('tr')
elseifself.tagName=='th'orself.tagName=='td'then
self=self:done():done():tag('tr')
else
self=self:tag('tr')
end
iftype(settings)=='table'then
addValues(self,settings)
end
returnself
end
functionmwHtml:th(settings)
ifself.tagName=='th'orself.tagName=='td'then
self=self:done():tag('th')
else
self=self:tag('th')
end
iftype(settings)=='table'then
addValues(self,settings)
else
self=self:wikitext(settings)
end
returnself
end
functionmwHtml:td(settings)
ifself.tagName=='th'orself.tagName=='td'then
self=self:done():tag('td')
else
self=self:tag('td')
end
iftype(settings)=='table'then
addValues(self,settings)
else
self=self:wikitext(settings)
end
returnself
end
functionmwHtml:IF(cond)
ifcondthen
table.insert(stack,{obj=noOp,trueCaseCompleted=true})
returnself
else
table.insert(stack,{obj=self,trueCaseCompleted=false})
returnnoOp
end
end
functionmwHtml:ELSEIF(cond)
if#stack==0thenerror('Missing IF tag',2)end
locallast=stack[#stack]
ifcondandnotlast.trueCaseCompletedthen
last.trueCaseCompleted=true
localres=last.obj
last.obj=noOp
returnres
else
ifself~=noOpthen
last.obj=self
end
returnnoOp
end
end
functionmwHtml:ELSE()
returnself:ELSEIF(true)
end
functionmwHtml:END()
if#stack==0thenerror('Missing IF tag',2)end
localres=table.remove(stack)-- Pop element from the end
ifres.obj==noOpthen
returnself
else
returnres.obj
end
end
functionmwHtml:exec(func,...)
checkType('exec',1,func,'function')
returnfunc(self,...)
end
functionp.addFunction(func,name)
checkType('addFunction',1,func,'function')
checkType('addFunction',2,name,'string')
mwHtml[name]=func
end
noOp.IF=mwHtml.IF
noOp.ELSEIF=mwHtml.ELSEIF
noOp.ELSE=mwHtml.ELSE
noOp.END=mwHtml.END
setmetatable(noOp,{
__index=function(self)
returnself
end,
__call=function(self)
returnself
end,
__tostring=function()
error('Attempting to convert no-op object into a string. Check for unbalanced IF-END tags',2)
end,
__concat=function()
error('Attempting to concatenate a no-op object. Check for unbalanced IF-END tags',2)
end
})
returnp
-- </nowiki>