Jump to content

Module:RemoveMiddleColumn: Difference between revisions

From Untitled Monster Game
imported>Ziebster
Created page with "local p={} function p.slim(f) local t=f.args[1]or"" if t==""then return""end local l={} for s in t:gmatch("([^\n]*)\n?")do table.insert(l,s)end local r={} for _,s in ipairs(l)do local u=s:match("^%s*(.-)%s*$") if u:match("^%{%|")or u:match("^|%}")then table.insert(r,s) elseif u:match("^!")then local h={} local i=0 for v in u:gmatch("!%s*([^!|]+)")do i=i+1;if i~=2 then table.insert(h,"!"..v:gsub("%s+$",""))end end if#h>0 then table.insert(r," "..t..."
 
imported>Ziebster
No edit summary
Line 5: Line 5:
  if t==""then return""end
  if t==""then return""end
  local l={}
  local l={}
  for s in t:gmatch("([^\n]*)\n?")do table.insert(l,s)end
  for s in t:gmatch("(.-)\n?")do table.insert(l,s)end
  local r={}
  local r={}
  for _,s in ipairs(l)do
local intable=false
   local u=s:match("^%s*(.-)%s*$")
  for _,orig in ipairs(l)do
   if u:match("^%{%|")or u:match("^|%}")then table.insert(r,s)
   if orig:match("^<section")or orig:match("^</section")then r[#r+1]=orig
   elseif u:match("^!")then
   elseif orig:match("^%s*%{%|")then intable=true r[#r+1]=orig
  local h={}
  elseif orig:match("^%s*|}%}")then intable=false r[#r+1]=orig
  local i=0
   elseif intable then
  for v in u:gmatch("!%s*([^!|]+)")do i=i+1;if i~=2 then table.insert(h,"!"..v:gsub("%s+$",""))end end
  if orig:match("^%s*!")then
  if#h>0 then table.insert(r," "..table.concat(h," !! "))end
    local heads={}
  elseif u:match("^|%-")then table.insert(r,"|-")
    for head in orig:gmatch("!%s*(.-)%s*!!")do heads[#heads+1]=head end
  elseif u:match("^|")then
    local last=orig:match("!!%s*(.-)%s*$")or""
  local c={}
    if heads[1]then heads[#heads+1]=last end
  local i=0
    local newh={}
  for v in u:gmatch("|%s*([^|]+)")do i=i+1;if i~=2 then table.insert(c,"|"..v:gsub("%s+$",""))end end
    for i=1,#heads do if i~=2 then newh[#newh+1]="!'''"..heads[i] end end
  if#c>=2 then
    if#newh>0 then r[#r+1]=table.concat(newh," !!''' ")end
    local b=table.concat(c," || ")
  elseif orig:match("^%s*|%-")then r[#r+1]="|-"
    b=b:gsub("^%s*||","|")
  elseif orig:match("^%s*|")then
    table.insert(r,b)
    local cells={}
    local temp=orig
    while temp:match("||")do
    local pos=temp:find("||")
    local cell=temp:sub(1,pos-1):match("|%s*(.-)%s*$")or""
    cells[#cells+1]=cell
    temp=temp:sub(pos+2)
    end
    local lastcell=temp:match("|%s*(.-)%s*$")or""
    cells[#cells+1]=lastcell
    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
   end
   end
  end
  end

Revision as of 14:36, 30 December 2025

Example Usage:

{{Device Disambiguation|Example}}

local p={}

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

return p