Difference between revisions of "Module:TableBuilder"
Jump to navigation
Jump to search
m |
m (Added captions) |
||
Line 24: | Line 24: | ||
builder.styles = {} | builder.styles = {} | ||
return builder | return builder | ||
− | |||
− | |||
− | |||
− | |||
end | end | ||
Line 39: | Line 35: | ||
end | end | ||
return builder | return builder | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
Line 71: | Line 46: | ||
__tostring = tostring, | __tostring = tostring, | ||
__index = { | __index = { | ||
− | _build = | + | _build = function(t, ret) |
− | addRow = | + | table.insert(ret, '{|') |
+ | buildAttributes(t, ret) | ||
+ | buildStyles(t, ret) | ||
+ | buildNodes(t, ret) | ||
+ | table.insert(ret, '\n|}') | ||
+ | end, | ||
+ | addCaption = function(t, ...) return addCell(t, "|+", ...) end, | ||
+ | addRow = function(t) return createBuilder(rowmetatable, t) end, | ||
getAttr = methodtable.getAttr, | getAttr = methodtable.getAttr, | ||
attr = methodtable.attr, | attr = methodtable.attr, | ||
Line 84: | Line 66: | ||
__tostring = tostring, | __tostring = tostring, | ||
__index = { | __index = { | ||
− | _build = | + | _build = function(t, ret) |
+ | table.insert(ret, '\n|-') | ||
+ | buildNodes(t, ret) | ||
+ | end, | ||
addHeader = function(t, ...) return addCell(t, "!", ...) end, | addHeader = function(t, ...) return addCell(t, "!", ...) end, | ||
addData = function(t, ...) return addCell(t, "|", ...) end, | addData = function(t, ...) return addCell(t, "|", ...) end, | ||
Line 95: | Line 80: | ||
__tostring = tostring, | __tostring = tostring, | ||
__index = { | __index = { | ||
− | _build = | + | _build = function(t, ret) |
+ | table.insert(ret, '\n' .. t.mark) | ||
+ | buildAttributes(t, ret) | ||
+ | buildStyles(t, ret) | ||
+ | table.insert(ret, '|') | ||
+ | buildNodes(t, ret) | ||
+ | end, | ||
node = methodtable.node, | node = methodtable.node, | ||
wikitext = methodtable.wikitext, | wikitext = methodtable.wikitext, |
Revision as of 04:19, 11 April 2017
local TableBuilder = {}
local HtmlBuilder = require('Module:HtmlBuilder') local buildAttributes = HtmlBuilder.buildAttributes local buildStyles = HtmlBuilder.buildStyles local buildNodes = HtmlBuilder.buildNodes local methodtable = getmetatable(HtmlBuilder.create()).__index local tablemetatable, rowmetatable, cellmetatable
local function createBuilder(metatable, parent)
local builder = {} setmetatable(builder, metatable) builder.nodes = {} if parent then builder.parent = parent table.insert(parent.nodes, builder) end return builder
end
function TableBuilder.create()
local builder = createBuilder(tablemetatable) builder.attributes = {} builder.styles = {} return builder
end
local function addCell(t, mark, ...)
local builder = createBuilder(cellmetatable, t) builder.attributes = {} builder.styles = {} builder.mark = mark if ... then builder:wikitext(...) end return builder
end
local function tostring(t)
local ret = {} t:_build(ret) return table.concat(ret)
end
tablemetatable = {
__tostring = tostring, __index = { _build = function(t, ret) table.insert(ret, '{|') buildAttributes(t, ret) buildStyles(t, ret) buildNodes(t, ret) table.insert(ret, '\n|}') end, addCaption = function(t, ...) return addCell(t, "|+", ...) end, addRow = function(t) return createBuilder(rowmetatable, t) end, getAttr = methodtable.getAttr, attr = methodtable.attr, addClass = methodtable.addClass, css = methodtable.css, cssText = methodtable.cssText }
}
rowmetatable = {
__tostring = tostring, __index = { _build = function(t, ret) table.insert(ret, '\n|-') buildNodes(t, ret) end, addHeader = function(t, ...) return addCell(t, "!", ...) end, addData = function(t, ...) return addCell(t, "|", ...) end, done = methodtable.done, allDone = methodtable.allDone }
}
cellmetatable = {
__tostring = tostring, __index = { _build = function(t, ret) table.insert(ret, '\n' .. t.mark) buildAttributes(t, ret) buildStyles(t, ret) table.insert(ret, '|') buildNodes(t, ret) end, node = methodtable.node, wikitext = methodtable.wikitext, tag = methodtable.tag, getAttr = methodtable.getAttr, attr = methodtable.attr, css = methodtable.css, cssText = methodtable.cssText, done = methodtable.done, allDone = methodtable.allDone }
}
return TableBuilder