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(f)
function p.slim(frame)
  local t=f.args[1]or""
  local text=frame.args[1]or""
  if t==""then return""end
  if text==""then return""end
  local l={}
  local lines={}
  for s in t:gmatch("(.-)\n?")do table.insert(l,s)end
  for line in text:gmatch("([^\n])\n?")do
  local r={}
  table.insert(lines,line)
  local intable=false
end
  for _,orig in ipairs(l)do
  local result={}
   if orig:match("^<section")or orig:match("^</section")then r[#r+1]=orig
  local col_count=0
   elseif orig:match("^%s*%{%|")then intable=true r[#r+1]=orig
local in_table=false
   elseif orig:match("^%s*|}%}")then intable=false r[#r+1]=orig
local is_header=false
   elseif intable then
  for _,line in ipairs(lines)do
   if orig:match("^%s*!")then
  local trimmed=line:match("^%s(.-)%s*$")or""
    local heads={}
   if trimmed:match("^<section")or trimmed:match("^</section")then
    for head in orig:gmatch("!%s*(.-)%s*!!")do heads[#heads+1]=head end
  table.insert(result,line)
    local last=orig:match("!!%s*(.-)%s*$")or""
   elseif trimmed:match("^%{%|")then
    if heads[1]then heads[#heads+1]=last end
  in_table=true
     local newh={}
  is_header=true
     for i=1,#heads do if i~=2 then newh[#newh+1]="!'''"..heads[i] end end
  col_count=0
     if#newh>0 then r[#r+1]=table.concat(newh," !!''' ")end
  table.insert(result,line)
   elseif orig:match("^%s*|%-")then r[#r+1]="|-"
   elseif trimmed:match("^|%}")then
  elseif orig:match("^%s*|")then
  in_table=false
     local cells={}
  is_header=false
     local temp=orig
  col_count=0
    while temp:match("||")do
  table.insert(result,line)
     local pos=temp:find("||")
   elseif in_table then
    local cell=temp:sub(1,pos-1):match("|%s*(.-)%s*$")or""
   if trimmed:match("^|%-")then
    cells[#cells+1]=cell
     is_header=false
    temp=temp:sub(pos+2)
     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
    local lastcell=temp:match("|%s*(.-)%s*$")or""
  else
    cells[#cells+1]=lastcell
     table.insert(result,line)
    local newc={}
    for i=1,#cells do if i~=2 then newc[#newc+1]="|"..cells[i] end end
     if#newc>=2 then r[#r+1]=table.concat(newc," || ")end
  else r[#r+1]=orig
   end
   end
   else r[#r+1]=orig
   else
  table.insert(result,line)
   end
   end
  end
  end
  return table.concat(r,"\n")
  return table.concat(result,"\n")
end
end


return p
return p

Revision as of 14:43, 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 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
   else
    table.insert(result,line)
   end
  else
   table.insert(result,line)
  end
 end
 return table.concat(result,"\n")
end

return p