Module:Mw.html extension/doc
This is the documentation page for Module:Mw.html extension
This module is a helper module to be used by other modules; it may not be designed to be invoked directly. See Near-Reality:Lua/Helper modules for a full list and more information.
| 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 | |
tr([settings]) | table/nil | Shortcut for :tag('tr') but also auto closes the previous 'tr', 'th' or 'td' tag (so you don't need to add :done() before it). settings is a table with keys:
| |
th([settings]) | string/table/nil | Shortcut for :tag('th'):wikitext(settings) if settings is a string. Also auto closes the previous 'th' or 'td' tag. settings can also be a table with keys:
| |
td([settings]) | string/table/nil | Same as :th(). Example:
local tbl = mw.html.create('table')
tbl:tr{ class='sortable' }
:th{'foo', attr={'data-sort-type', 'number'}}
:th('bar')
:tr()
:td('buz')
:attr('data-sort-value', 10)
:td{'N/A', class='table-na'} | |
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()
:...Note: This only prevents elements from being added to your html object, it does not protect against statements that throw errors. I.e mw.html.create('div')
:IF(false)
:wikitext(5 * nil) -- This will still throw an error
:END() | |
ELSEIF(cond) | boolean | Used together with IF(). | |
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. |
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 pYou 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
endThis 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 ptr, th and td
The following three tables are the same:
local tbl = mw.html.create('table')
tbl:tr{ class='sortable' }
:th{'foo', attr={['data-sort-type']='number'}} -- or attr={'data-sort-type', 'number'}
:th('bar')
:IF(expression)
:addClass('table-na')
:END()
:tr()
:td('buz')
:td{'N/A', class='table-na'}
local tbl2 = mw.html.create('table')
tbl2:tag('tr')
:addClass('sortable')
:tag('th')
:attr('data-sort-type', 'number')
:wikitext('foo')
:done()
:tag('th')
:wikitext('bar')
:IF(expression)
:addClass('table-na')
:END()
:done() -- This is needed because "tag('tr')" is used after this instead of "tr()"
:done()
:tag('tr')
:tag('td')
:wikitext('buz')
:done()
:tag('td')
:wikitext('N/A')
:addClass('table-na')
local tbl3 = mw.html.create('table')
tbl3:tag('tr')
:addClass('sortable')
:tag('th')
:attr('data-sort-type', 'number')
:wikitext('foo')
:th('bar')
:IF(expression)
:addClass('table-na')
:END()
:done()
:done()
:tag('tr')
:td('buz')
:td('N/A')
:addClass('table-na')