Jump to content

Module:RemoveMiddleColumn: Difference between revisions

From Untitled Monster Game
imported>Ziebster
No edit summary
imported>Ziebster
No edit summary
Line 18: Line 18:
         if trimmed:match("^<section") or trimmed:match("^</section") then
         if trimmed:match("^<section") or trimmed:match("^</section") then
             table.insert(result, line)
             table.insert(result, line)
       
         elseif trimmed:match("^%{%|") then
         elseif trimmed:match("^%{%|") then
             in_table = true
             in_table = true
             table.insert(result, line)
             table.insert(result, line)
       
         elseif trimmed:match("^|%}") then
         elseif trimmed:match("^|%}") then
             in_table = false
             in_table = false
             table.insert(result, line)
             table.insert(result, line)
       
         elseif in_table then
         elseif in_table then
             if trimmed:match("^|%-") then
             if trimmed == "|-" then
                 table.insert(result, line)
                 table.insert(result, "|-")
           
             elseif trimmed:match("^!") then
             elseif trimmed:match("^!") then
                 -- Header line - skip 2nd column
                 -- Header row
                 local cells = {}
                 local cells = {}
                 local remaining = line
                 local temp = line
                 while remaining:find("!!") do
                 while temp:find("!!") do
                     local pre, post = remaining:match("^(.-)!!(.*)$")
                     local pre, post = temp:match("^(.-)!!(.*)$")
                     local cell = pre:match("^%s*!%s*(.-)$") or ""
                     local cell = pre:match("^%s*!%s*'''(.*)'''%s*$") or pre:match("^%s*!%s*(.-)%s*$") or ""
                     table.insert(cells, cell)
                     table.insert(cells, cell)
                     remaining = "!!" .. post
                     temp = post
                 end
                 end
                -- last cell
                 local cell = temp:match("^%s*!%s*'''(.*)'''%s*$") or temp:match("^%s*!%s*(.-)%s*$") or ""
                 local cell = remaining:match("^%s*!%s*(.-)$") or ""
                 table.insert(cells, cell)
                 table.insert(cells, cell)
                  
                  
Line 48: Line 43:
                 for i = 1, #cells do
                 for i = 1, #cells do
                     if i ~= 2 then
                     if i ~= 2 then
                         table.insert(new_cells, "!'''" .. cells[i])
                         table.insert(new_cells, "! '''" .. cells[i] .. "'''")
                     end
                     end
                 end
                 end
                 if #new_cells > 0 then
                 if #new_cells > 0 then
                     table.insert(result, table.concat(new_cells, " !!''' "))
                     table.insert(result, table.concat(new_cells, " !! "))
                 end
                 end
           
             elseif trimmed:match("^|") then
             elseif trimmed:match("^|") then
                 -- Data row - skip 2nd column
                 -- Data row
                 local cells = {}
                 local cells = {}
                 local remaining = line
                 local temp = line
                 while remaining:find("||") do
                 while temp:find("||") do
                     local pre, post = remaining:match("^(.-)||(.*)$")
                     local pre, post = temp:match("^(.-)||(.*)$")
                     local cell = pre:match("^%s*|%s*(.-)$") or ""
                     local cell = pre:match("^%s*|%s*(.-)%s*$") or ""
                     table.insert(cells, cell)
                     table.insert(cells, cell)
                     remaining = "||" .. post
                     temp = post
                 end
                 end
                -- last cell
                 local cell = temp:match("^%s*|%s*(.-)%s*$") or ""
                 local cell = remaining:match("^%s*|%s*(.-)$") or ""
                 table.insert(cells, cell)
                 table.insert(cells, cell)
                  
                  
Line 72: Line 65:
                 for i = 1, #cells do
                 for i = 1, #cells do
                     if i ~= 2 then
                     if i ~= 2 then
                         table.insert(new_cells, "|" .. cells[i])
                         table.insert(new_cells, "| " .. cells[i])
                     end
                     end
                 end
                 end
Line 78: Line 71:
                     table.insert(result, table.concat(new_cells, " || "))
                     table.insert(result, table.concat(new_cells, " || "))
                 end
                 end
           
             else
             else
                 table.insert(result, line)
                 table.insert(result, line)

Revision as of 14:51, 30 December 2025

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 == "|-" then
                table.insert(result, "|-")
            elseif trimmed:match("^!") then
                -- Header row
                local cells = {}
                local temp = line
                while temp:find("!!") do
                    local pre, post = temp:match("^(.-)!!(.*)$")
                    local cell = pre:match("^%s*!%s*'''(.*)'''%s*$") or pre:match("^%s*!%s*(.-)%s*$") or ""
                    table.insert(cells, cell)
                    temp = post
                end
                local cell = temp:match("^%s*!%s*'''(.*)'''%s*$") or temp:match("^%s*!%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
                local cells = {}
                local temp = line
                while temp:find("||") do
                    local pre, post = temp:match("^(.-)||(.*)$")
                    local cell = pre:match("^%s*|%s*(.-)%s*$") or ""
                    table.insert(cells, cell)
                    temp = post
                end
                local cell = temp:match("^%s*|%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