Module:Mw.html extension
This module is a helper module to be used by other modules; it may not designed to be invoked directly. See RuneScape:Lua/Helper modules for a full list and more information. For a full list of modules using this helper click here
Module | Function | Type | Use |
---|---|---|---|
Mw.html extension | addClassIf(cond, class) | boolean, string | If cond = true it behaves the same as the normal addClass function, otherwise it's a no-op. Ex.: mw.html.create('div'):addClassIf(true, 'align-left-1') |
attrIf(cond, name, value) | boolean, string/table, string/nil | Similar to addClassIf | |
cssIf(cond, name, value) | boolean, string/table, string/nil | Similar to addClassIf | |
doneIf(cond) | boolean | Similar to addClassIf | |
tagIf(cond, tag) | boolean, string | Similar to addClassIf | |
wikitextIf(cond, text) | boolean, string | Similar to addClassIf | |
na() | N/A | Shortcut for :tag('td'):attr('data-sort-value', 0):attr('class','table-na'):wikitext('<small>N/A</small>'):done() | |
naIf(cond) | boolean | Similar to addClassIf | |
IF(cond) | boolean | Allows for if-blocks without breaking the chain. If the condition is true it is a no-op, if false everything inside the balanced IF-END block will be ignored. Can be nested. Ex.:
mw.html.create('div')
:IF(true)
:wikitext('Conditional text')
:END()
:...
| |
ELSE() | N/A | Used together with IF() . | |
END() | N/A | Used together with IF() . Make sure the IF-END tags are balanced, it wont throw an error if they are not. | |
exec(func, ...) | function, any | Call a function without breaking the chain. See module docs for more info. | |
addFunction(func, name) | function, string | Add a function to the mw.html class that can then be used on mw.html object. See module docs for more info. |
Contents
Usage
For all functions other than addFunction()
all you need to do is simply require this module:
require('Module:Mw.html extension')
local p = {}
function p.main()
...
local tbl = mw.html.create('div')
:IF(true)
:wikitext('Conditional text')
:ELSE()
:wikitext('something else')
:END()
:addClassIf(true, 'wikitable')
:tag('span)'
:wikitext('normal wikitext')
:done()
...
end
return p
You can mix the normal old functions with the newly added ones.
attrIf
This accepts either a name-value pair or a table
:attrIf(true, 'data-sort-value', '0')
:attrIf(true, {'data-sort-value' = '0', ...})
cssIf
This accepts either a name-value pair or a table similar to attrIf
exec
The first parameter of the function will have the current state of the mw.html object passed into it, usually we call this parameter self
, the rest of the parameters can be anything you want. To not break the chaining the function must also return a mw.html object. Example:
local function repNa(self, times)
for i = 1,times do
self:na()
end
return self
end
This function can then be used as follows
mw.html.create('div'):exec(repNa, 5)
addFunction
The function you want to add has to be of the same structure as in exec()
. Example:
local htmlExtension = require('Module:Mw.html extension')
local p = {}
local function repNa(self, times)
for i = 1,times do
self:na()
end
return self
end
htmlExtension.addFunction(repNa, 'repNaName')
function p.main()
...
local tbl = mw.html.create('div'):repNaName(5)
...
end
return p
-- <nowiki>
local p = {}
local libraryUtil = require( 'libraryUtil' )
local index = getmetatable(mw.html.create()).__index -- Trick to get acces to the mw.html class
local stack = {} -- Used to keep track of nested IF-END tags
local noOp = {} -- This object is returned by IF(false) tag
local function addClassIf(self, cond, class)
if cond then
return self:addClass(class)
else
return self
end
end
local function tagIf(self, cond, tagname)
if cond then
return self:tag(tagname)
else
return self
end
end
local function wikitextIf(self, cond, text)
if cond then
return self:wikitext(text)
else
return self
end
end
local function doneIf(self, cond)
if cond then
return self:done()
else
return self
end
end
local function attrIf(self, cond, name, value)
if cond then
return self:attr(name, value)
else
return self
end
end
local function cssIf(self, cond, name, value)
if cond then
return self:css(name, value)
else
return self
end
end
local function na(self)
return self:tag('td'):attr('data-sort-value', 0):attr('class','table-na'):wikitext('<small>N/A</small>'):done()
end
local function naIf(self, cond)
if cond then
return self:na()
else
return self
end
end
local function ifFunc(self, cond)
if cond then
table.insert(stack, noOp)
return self
else
table.insert(stack, self)
return noOp
end
end
local function elseFunc(self)
if self == noOp then
return self:END():IF(true)
else
return self:END():IF(false)
end
end
local function endFunc(self)
local stackLen = #stack
if stackLen > 0 then
local res = table.remove(stack, stackLen) -- Pop element from the end
if res == noOp then
return self
else
return res
end
else
return self
end
end
local function exec(self, func, ...)
if type(func) == "function" then
return func(self, ...)
end
return self
end
function p.addFunction(func, name)
libraryUtil.checkType('addFunction', 1, func, 'function', false)
libraryUtil.checkType('addFunction', 2, name, 'string', false)
if index[name] then
error('Function "' .. name .. '" already exists', 2)
end
index[name] = func
end
noOp.IF = ifFunc
noOp.ELSE = elseFunc
noOp.END = endFunc
setmetatable(noOp, {
__index = function (self)
return self
end,
__call = function (self)
return self
end,
__tostring = function (self)
error("Attempting to convert no-op object into a string. Check for unbalanced IF-END tags", 5)
end,
__concat = function (obj1, obj2)
error("Attempting to concatenate a no-op object. Check for unbalanced IF-END tags", 2)
end
})
index.addClassIf = addClassIf
index.tagIf = tagIf
index.wikitextIf = wikitextIf
index.doneIf = doneIf
index.attrIf = attrIf
index.cssIf = cssIf
index.na = na
index.naIf = naIf
index.IF = ifFunc
index.ELSE = elseFunc
index.END = endFunc
index.exec = exec
return p
-- </nowiki>