Jump to content

Module:RemoveMiddleColumn: Difference between revisions

From Untitled Monster Game
imported>Ziebster
No edit summary
imported>Ziebster
No edit summary
Line 1: Line 1:
local p={}
local p = {}


function p.slim(frame)
function p.slim(frame)
local text=frame.args[1]or""
    local text = frame.args[1] or ""
if text==""then return""end
    if text == "" then return "" end
local lines={}
   
for line in text:gmatch("([^\n])\n?")do
    local lines = {}
  table.insert(lines,line)
    for line in text:gmatch("([^\n]*)\n?") do
end
        table.insert(lines, line)
local result={}
local col_count=0
local in_table=false
local is_header=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
  is_header=true
  col_count=0
  table.insert(result,line)
  elseif trimmed:match("^|%}")then
  in_table=false
  is_header=false
  col_count=0
  table.insert(result,line)
  elseif in_table then
  if trimmed:match("^|%-")then
    is_header=false
    col_count=0
    table.insert(result,line)
  elseif(is_header and trimmed:match("^!"))or(not is_header and trimmed:match("^|"))then
    col_count=col_count+1
    if col_count~=2 then
    table.insert(result,line)
     end
     end
  else
   
    table.insert(result,line)
    local result = {}
  end
    local in_table = false
  else
   
  table.insert(result,line)
    for _, line in ipairs(lines) do
  end
        local trimmed = line:match("^%s*(.-)%s*$") or ""
end
       
return table.concat(result,"\n")
        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
end


return p
return p

Revision as of 14:46, 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: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