跳转到内容

模块:TowerTable/sandbox:修订间差异

来自Minimalist Tower Defense Wiki
Jathonbot​(留言 | 贡献)
小 沙盒:行分隔改由模块负责,兼容新旧 twtl
Jathonbot​(留言 | 贡献)
小 沙盒:去掉预处理探测,改为统一由模块负责行分隔
第66行: 第66行:
                 firstCol = firstCol:gsub('^|%-%s*', '')
                 firstCol = firstCol:gsub('^|%-%s*', '')
                 needRowSep = false
                 needRowSep = false
            elseif firstCol:match('^{{') then
                local expanded = frame:preprocess(firstCol)
                if expanded:match('^%s*|%-') then
                    needRowSep = false
                end
             end
             end



2026年11月25日 (三) 10:39的版本

此模块的文档可以在模块:TowerTable/sandbox/doc创建

local p = {}

local cfg = {
    wrapStyle = 'overflow-x:auto;width:100%;',
    tableStyle = 'background-color:#212121;color: #EAECF0;white-space: nowrap;',
    defaultClass = 'wikitable',
    firstHeader = "'''防御塔等级与属性项(总和)'''",
    errCategory = '[[Category:防御塔表格需要修复]]',
}

local function trim(s)
    return mw.text.trim(s or '')
end

local function cell(header, value)
    return '{{c|' .. header .. '|' .. value .. '}}'
end

function p.main(frame)
    local args = frame.args
    local parent = frame:getParent()
    if parent and parent.args and (parent.args.header or parent.args.rows) then
        args = parent.args
    end

    local class = args.class or cfg.defaultClass
    local headerStr = trim(args.header)
    local rowsStr = trim(args.rows)
    local footerStr = trim(args.footer)
    local issues = {}

    if headerStr == '' then
        return cfg.errCategory
    end

    local headers = {}
    for h in mw.text.gsplit(headerStr, ',') do
        headers[#headers + 1] = trim(h)
    end
    local ncol = #headers
    if ncol == 0 then
        return cfg.errCategory
    end

    local tableText = '<div style="' .. cfg.wrapStyle .. '">\n'
        .. '{| class="' .. class .. '" style="' .. cfg.tableStyle .. '"\n'
        .. '| ' .. cfg.firstHeader

    for _, h in ipairs(headers) do
        tableText = tableText .. ' || ' .. cell(h, h)
    end
    tableText = tableText .. '\n'

    for row in mw.text.gsplit(rowsStr, ';') do
        local rowTrim = trim(row)
        if rowTrim ~= '' then
            local cells = {}
            for c in mw.text.gsplit(rowTrim, ',') do
                cells[#cells + 1] = trim(c)
            end

            local firstCol = cells[1] or ''
            local needRowSep = true

            if firstCol:match('^|%-') then
                firstCol = firstCol:gsub('^|%-%s*', '')
                needRowSep = false
            end

            if firstCol == '' then
                firstCol = '| '
                issues[#issues + 1] = 'empty-first-cell'
            elseif not firstCol:match('^[|!]') and not firstCol:match('^{{') then
                firstCol = '| ' .. firstCol
            end

            if needRowSep then
                tableText = tableText .. '|-\n'
            end
            tableText = tableText .. firstCol .. '\n'

            for i = 2, #cells do
                local header = headers[i - 1]
                if header == nil then
                    issues[#issues + 1] = 'extra-cell'
                    break
                end
                local value = cells[i]:gsub('|', '{{!}}')
                tableText = tableText .. (i == 2 and '| ' or ' || ') .. cell(header, value)
            end

            if #cells - 1 < ncol then
                issues[#issues + 1] = 'missing-cell'
                for i = #cells + 1, ncol + 1 do
                    tableText = tableText .. (i == 2 and '| ' or ' || ') .. cell(headers[i - 1], '')
                end
            end

            tableText = tableText .. '\n'
        end
    end

    if footerStr ~= '' then
        tableText = tableText .. '|-\n| ' .. footerStr .. '\n'
    end

    tableText = tableText .. '|}\n</div>'

    local out = frame:preprocess(tableText)
    if #issues > 0 then
        out = out .. cfg.errCategory
    end
    return out
end

return p