Difference between revisions of "Module:TableBuilder"
Jump to navigation
Jump to search
m (Added captions) |
(FnControlOption changed the content model of the page Module:TableBuilder from "plain text" to "Scribunto") Tag: content model change |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 2: | Line 2: | ||
local HtmlBuilder = require('Module:HtmlBuilder') | local HtmlBuilder = require('Module:HtmlBuilder') | ||
− | |||
− | |||
− | |||
local methodtable = getmetatable(HtmlBuilder.create()).__index | local methodtable = getmetatable(HtmlBuilder.create()).__index | ||
local tablemetatable, rowmetatable, cellmetatable | local tablemetatable, rowmetatable, cellmetatable | ||
Line 46: | Line 43: | ||
__tostring = tostring, | __tostring = tostring, | ||
__index = { | __index = { | ||
+ | _buildAttributes = methodtable._buildAttributes, | ||
+ | _buildStyles = methodtable._buildStyles, | ||
+ | _buildNodes = methodtable._buildNodes, | ||
_build = function(t, ret) | _build = function(t, ret) | ||
table.insert(ret, '{|') | table.insert(ret, '{|') | ||
− | + | t:_buildAttributes(ret) | |
− | + | t:_buildStyles(ret) | |
− | + | t:_buildNodes(ret) | |
table.insert(ret, '\n|}') | table.insert(ret, '\n|}') | ||
end, | end, | ||
Line 66: | Line 66: | ||
__tostring = tostring, | __tostring = tostring, | ||
__index = { | __index = { | ||
+ | _buildNodes = methodtable._buildNodes, | ||
_build = function(t, ret) | _build = function(t, ret) | ||
table.insert(ret, '\n|-') | table.insert(ret, '\n|-') | ||
− | + | t:_buildNodes(ret) | |
end, | end, | ||
addHeader = function(t, ...) return addCell(t, "!", ...) end, | addHeader = function(t, ...) return addCell(t, "!", ...) end, | ||
Line 80: | Line 81: | ||
__tostring = tostring, | __tostring = tostring, | ||
__index = { | __index = { | ||
+ | _buildAttributes = methodtable._buildAttributes, | ||
+ | _buildStyles = methodtable._buildStyles, | ||
+ | _buildNodes = methodtable._buildNodes, | ||
_build = function(t, ret) | _build = function(t, ret) | ||
table.insert(ret, '\n' .. t.mark) | table.insert(ret, '\n' .. t.mark) | ||
− | + | t:_buildAttributes(ret) | |
− | + | t:_buildStyles(ret) | |
table.insert(ret, '|') | table.insert(ret, '|') | ||
− | + | t:_buildNodes(ret) | |
end, | end, | ||
node = methodtable.node, | node = methodtable.node, | ||
Line 92: | Line 96: | ||
getAttr = methodtable.getAttr, | getAttr = methodtable.getAttr, | ||
attr = methodtable.attr, | attr = methodtable.attr, | ||
+ | addClass = methodtable.addClass, | ||
css = methodtable.css, | css = methodtable.css, | ||
cssText = methodtable.cssText, | cssText = methodtable.cssText, |
Latest revision as of 17:41, 7 June 2022
Documentation for this module may be created at Module:TableBuilder/doc
local TableBuilder = {} local HtmlBuilder = require('Module:HtmlBuilder') 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 = { _buildAttributes = methodtable._buildAttributes, _buildStyles = methodtable._buildStyles, _buildNodes = methodtable._buildNodes, _build = function(t, ret) table.insert(ret, '{|') t:_buildAttributes(ret) t:_buildStyles(ret) t:_buildNodes(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 = { _buildNodes = methodtable._buildNodes, _build = function(t, ret) table.insert(ret, '\n|-') t:_buildNodes(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 = { _buildAttributes = methodtable._buildAttributes, _buildStyles = methodtable._buildStyles, _buildNodes = methodtable._buildNodes, _build = function(t, ret) table.insert(ret, '\n' .. t.mark) t:_buildAttributes(ret) t:_buildStyles(ret) table.insert(ret, '|') t:_buildNodes(ret) end, node = methodtable.node, wikitext = methodtable.wikitext, tag = methodtable.tag, getAttr = methodtable.getAttr, attr = methodtable.attr, addClass = methodtable.addClass, css = methodtable.css, cssText = methodtable.cssText, done = methodtable.done, allDone = methodtable.allDone } } return TableBuilder