Module:TableBuilder

From Minetest

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