Jump to content

Module:RemoveMiddleColumn: Difference between revisions

From Untitled Monster Game
imported>Ziebster
No edit summary
imported>Ziebster
No edit summary
 
(3 intermediate revisions by the same user not shown)
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
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
     end
     local lastcell=temp:match("|%s*(.-)%s*$")or""
     local result = {}
    cells[#cells+1]=lastcell
    local i = 1
    local newc={}
    while i <= #lines do
    for i=1,#cells do if i~=2 then newc[#newc+1]="|"..cells[i] end end
        local trimmed = lines[i]:match("^%s*(.-)%s*$") or ""
    if#newc>=2 then r[#r+1]=table.concat(newc," || ")end
        if trimmed:match("^<section") or trimmed:match("^</section") then
  else r[#r+1]=orig
            table.insert(result, lines[i])
  end
        elseif trimmed:match("^%{%|") then
  else r[#r+1]=orig
            table.insert(result, lines[i])
  end
        elseif trimmed:match("^|%}") then
end
            table.insert(result, lines[i])
return table.concat(r,"\n")
        elseif trimmed:match("^|%-") then
            table.insert(result, lines[i])
        elseif trimmed:match("^!") then
            local headers = {}
            while i <= #lines and lines[i]:match("^%s*!") do
                table.insert(headers, lines[i])
                i = i + 1
            end
            i = i - 1
            if #headers == 3 then
                table.insert(result, headers[1])
                table.insert(result, headers[3])
            end
        elseif trimmed:match("^|") and not trimmed:match("^|%-") and not trimmed:match("^|%}") then
            local cells = {}
            while i <= #lines and lines[i]:match("^%s*|") and not lines[i]:match("^%s*|%-") and not lines[i]:match("^%s*|%}") do
                table.insert(cells, lines[i])
                i = i + 1
            end
            i = i - 1
            if #cells == 3 then
                table.insert(result, cells[1])
                table.insert(result, cells[3])
            end
        else
            table.insert(result, lines[i])
        end
        i = i + 1
    end
    return table.concat(result, "\n")
end
end


return p
return p

Latest revision as of 14:53, 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 i = 1
    while i <= #lines do
        local trimmed = lines[i]:match("^%s*(.-)%s*$") or ""
        if trimmed:match("^<section") or trimmed:match("^</section") then
            table.insert(result, lines[i])
        elseif trimmed:match("^%{%|") then
            table.insert(result, lines[i])
        elseif trimmed:match("^|%}") then
            table.insert(result, lines[i])
        elseif trimmed:match("^|%-") then
            table.insert(result, lines[i])
        elseif trimmed:match("^!") then
            local headers = {}
            while i <= #lines and lines[i]:match("^%s*!") do
                table.insert(headers, lines[i])
                i = i + 1
            end
            i = i - 1
            if #headers == 3 then
                table.insert(result, headers[1])
                table.insert(result, headers[3])
            end
        elseif trimmed:match("^|") and not trimmed:match("^|%-") and not trimmed:match("^|%}") then
            local cells = {}
            while i <= #lines and lines[i]:match("^%s*|") and not lines[i]:match("^%s*|%-") and not lines[i]:match("^%s*|%}") do
                table.insert(cells, lines[i])
                i = i + 1
            end
            i = i - 1
            if #cells == 3 then
                table.insert(result, cells[1])
                table.insert(result, cells[3])
            end
        else
            table.insert(result, lines[i])
        end
        i = i + 1
    end
    return table.concat(result, "\n")
end

return p