http://nswiki.org/index.php?title=Module:HtmlBuilder&feed=atom&action=history
Module:HtmlBuilder - Revision history
2024-03-29T07:04:49Z
Revision history for this page on the wiki
MediaWiki 1.23.0
http://nswiki.org/index.php?title=Module:HtmlBuilder&diff=223941&oldid=prev
Jenlom: Created page with "-- Module for building complex HTML (e.g. infoboxes, navboxes) using a fluent interface. local HtmlBuilder = {} local metatable = {} metatable.__index = function(t, key) ..."
2014-03-11T13:44:36Z
<p>Created page with "-- Module for building complex HTML (e.g. infoboxes, navboxes) using a fluent interface. local HtmlBuilder = {} local metatable = {} metatable.__index = function(t, key) ..."</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>
Jenlom