https://www.svrwiki.com/mediawiki-1.28.2/index.php?title=Module:HtmlBuilder&feed=atom&action=history
Module:HtmlBuilder - Revision history
2024-03-29T07:39:17Z
Revision history for this page on the wiki
MediaWiki 1.28.2
https://www.svrwiki.com/mediawiki-1.28.2/index.php?title=Module:HtmlBuilder&diff=9329&oldid=prev
Danny252: 1 revision imported: Importing Navbox
2016-06-20T21:33:55Z
<p>1 revision imported: Importing Navbox</p>
<table class="diff diff-contentalign-left" data-mw="interface">
<tr style='vertical-align: top;' lang='en-GB'>
<td colspan='1' style="background-color: white; color:black; text-align: center;">← Older revision</td>
<td colspan='1' style="background-color: white; color:black; text-align: center;">Revision as of 21:33, 20 June 2016</td>
</tr><tr><td colspan='2' style='text-align: center;' lang='en-GB'><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>
Danny252
https://www.svrwiki.com/mediawiki-1.28.2/index.php?title=Module:HtmlBuilder&diff=9328&oldid=prev
Peter Harrison: ←Created page with '-- Module for building complex HTML (e.g. infoboxes, navboxes) using a fluent interface. local HtmlBuilder = {} local metatable = ...'
2014-02-03T11:45:04Z
<p><a href="/mediawiki-1.28.2/index.php?title=Festipedia:Automatic_edit_summaries&action=edit&redlink=1" class="new" title="Festipedia:Automatic edit summaries (page does not exist)">←</a>Created page with '-- Module for building complex HTML (e.g. infoboxes, navboxes) using a fluent interface. local HtmlBuilder = {} local metatable = ...'</p>
<p><b>New page</b></p><div>-- Module for building complex HTML (e.g. infoboxes, navboxes) using a fluent interface.<br />
<br />
local HtmlBuilder = {}<br />
<br />
local metatable = {}<br />
<br />
metatable.__index = function(t, key)<br />
local ret = rawget(t, key)<br />
if ret then<br />
return ret<br />
end<br />
<br />
ret = metatable[key]<br />
if type(ret) == 'function' then<br />
return function(...) <br />
return ret(t, ...) <br />
end <br />
else<br />
return ret<br />
end<br />
end<br />
<br />
metatable.__tostring = function(t)<br />
local ret = {}<br />
t._build(ret)<br />
return table.concat(ret)<br />
end<br />
<br />
metatable._build = function(t, ret)<br />
if t.tagName then <br />
table.insert(ret, '<' .. t.tagName)<br />
for i, attr in ipairs(t.attributes) do<br />
table.insert(ret, ' ' .. attr.name .. '="' .. attr.val .. '"') <br />
end<br />
if #t.styles > 0 then<br />
table.insert(ret, ' style="')<br />
for i, prop in ipairs(t.styles) do<br />
if type(prop) == 'string' then -- added with cssText()<br />
table.insert(ret, prop .. ';')<br />
else -- added with css()<br />
table.insert(ret, prop.name .. ':' .. prop.val .. ';')<br />
end<br />
end<br />
table.insert(ret, '"')<br />
end<br />
if t.selfClosing then<br />
table.insert(ret, ' /')<br />
end<br />
table.insert(ret, '>') <br />
end<br />
for i, node in ipairs(t.nodes) do<br />
if node then<br />
if type(node) == 'table' then<br />
node._build(ret)<br />
else<br />
table.insert(ret, tostring(node))<br />
end<br />
end<br />
end<br />
if t.tagName and not t.unclosed and not t.selfClosing then<br />
table.insert(ret, '</' .. t.tagName .. '>')<br />
end<br />
end<br />
<br />
metatable.node = function(t, builder)<br />
if builder then<br />
table.insert(t.nodes, builder)<br />
end<br />
return t<br />
end<br />
<br />
metatable.wikitext = function(t, ...) <br />
local vals = {...}<br />
for i = 1, #vals do<br />
if vals[i] then<br />
table.insert(t.nodes, vals[i])<br />
end<br />
end<br />
return t<br />
end<br />
<br />
metatable.newline = function(t)<br />
table.insert(t.nodes, '\n')<br />
return t<br />
end<br />
<br />
metatable.tag = function(t, tagName, args)<br />
args = args or {}<br />
args.parent = t<br />
local builder = HtmlBuilder.create(tagName, args)<br />
table.insert(t.nodes, builder)<br />
return builder<br />
end<br />
<br />
local function getAttr(t, name)<br />
for i, attr in ipairs(t.attributes) do<br />
if attr.name == name then<br />
return attr<br />
end<br />
end<br />
end<br />
<br />
metatable.attr = function(t, name, val)<br />
if type(val) == 'string' or type(val) == 'number' then<br />
-- if caller sets the style attribute explicitly, then replace all styles previously added with css() and cssText()<br />
if name == 'style' then<br />
t.styles = {val}<br />
return t<br />
end<br />
<br />
local attr = getAttr(t, name)<br />
if attr then<br />
attr.val = val<br />
else<br />
table.insert(t.attributes, {name = name, val = val})<br />
end<br />
end<br />
<br />
return t<br />
end<br />
<br />
metatable.addClass = function(t, class)<br />
if class then<br />
local attr = getAttr(t, 'class')<br />
if attr then<br />
attr.val = attr.val .. ' ' .. class<br />
else<br />
t.attr('class', class)<br />
end<br />
end<br />
<br />
return t<br />
end<br />
<br />
metatable.css = function(t, name, val)<br />
if type(val) == 'string' or type(val) == 'number' then<br />
for i, prop in ipairs(t.styles) do<br />
if prop.name == name then<br />
prop.val = val<br />
return t<br />
end<br />
end<br />
<br />
table.insert(t.styles, {name = name, val = val})<br />
end<br />
<br />
return t<br />
end<br />
<br />
metatable.cssText = function(t, css)<br />
if css then<br />
table.insert(t.styles, css)<br />
end<br />
return t<br />
end<br />
<br />
metatable.done = function(t)<br />
return t.parent or t<br />
end<br />
<br />
metatable.allDone = function(t)<br />
while t.parent do<br />
t = t.parent<br />
end<br />
return t<br />
end<br />
<br />
function HtmlBuilder.create(tagName, args)<br />
args = args or {}<br />
local builder = {}<br />
setmetatable(builder, metatable)<br />
builder.nodes = {}<br />
builder.attributes = {}<br />
builder.styles = {}<br />
builder.tagName = tagName<br />
builder.parent = args.parent<br />
builder.unclosed = args.unclosed or false<br />
builder.selfClosing = args.selfClosing or false<br />
return builder<br />
end<br />
<br />
return HtmlBuilder</div>
Peter Harrison