Jump to content

Module:RemoveMiddleColumn

From Untitled Monster Game
Revision as of 14:46, 30 December 2025 by imported>Ziebster

Example Usage:

{{Device Disambiguation|Example}}

local p = {}

function p.slim(frame)
    local text = frame.args[1] or ""
    if text == "" then return "" end
    
    local lines = {}
    for line in text:gmatch("([^\n]*)\n?") do
        table.insert(lines, line)
    end
    
    local result = {}
    local in_table = false
    
    for _, line in ipairs(lines) do
        local trimmed = line:match("^%s*(.-)%s*$") or ""
        
        if trimmed:match("^<section") or trimmed:match("^</section") then
            table.insert(result, line)
        
        elseif trimmed:match("^%{%|") then
            in_table = true
            table.insert(result, line)
        
        elseif trimmed:match("^|%}") then
            in_table = false
            table.insert(result, line)
        
        elseif in_table then
            if trimmed:match("^|%-") then
                table.insert(result, line)
            
            elseif trimmed:match("^!") then
                -- Header line - skip 2nd column
                local cells = {}
                local remaining = line
                while remaining:find("!!") do
                    local pre, post = remaining:match("^(.-)!!(.*)$")
                    local cell = pre:match("^%s*!%s*(.-)$") or ""
                    table.insert(cells, cell)
                    remaining = "!!" .. post
                end
                -- last cell
                local cell = remaining:match("^%s*!%s*(.-)$") or ""
                table.insert(cells, cell)
                
                local new_cells = {}
                for i = 1, #cells do
                    if i ~= 2 then
                        table.insert(new_cells, "!'''" .. cells[i])
                    end
                end
                if #new_cells > 0 then
                    table.insert(result, table.concat(new_cells, " !!''' "))
                end
            
            elseif trimmed:match("^|") then
                -- Data row - skip 2nd column
                local cells = {}
                local remaining = line
                while remaining:find("||") do
                    local pre, post = remaining:match("^(.-)||(.*)$")
                    local cell = pre:match("^%s*|%s*(.-)$") or ""
                    table.insert(cells, cell)
                    remaining = "||" .. post
                end
                -- last cell
                local cell = remaining:match("^%s*|%s*(.-)$") or ""
                table.insert(cells, cell)
                
                local new_cells = {}
                for i = 1, #cells do
                    if i ~= 2 then
                        table.insert(new_cells, "|" .. cells[i])
                    end
                end
                if #new_cells >= 2 then
                    table.insert(result, table.concat(new_cells, " || "))
                end
            
            else
                table.insert(result, line)
            end
        else
            table.insert(result, line)
        end
    end
    
    return table.concat(result, "\n")
end

return p