Jump to content

Module:RemoveMiddleColumn: Difference between revisions

From Untitled Monster Game
imported>Ziebster
No edit summary
imported>Ziebster
No edit summary
 
Line 4: Line 4:
     local text = frame.args[1] or ""
     local text = frame.args[1] or ""
     if text == "" then return "" end
     if text == "" then return "" end
   
     local lines = {}
     local lines = {}
     for line in text:gmatch("([^\n]*)\n?") do
     for line in text:gmatch("([^\n]*)\n?") do
         table.insert(lines, line)
         table.insert(lines, line)
     end
     end
   
     local result = {}
     local result = {}
     local in_table = false
     local i = 1
      
     while i <= #lines do
    for _, line in ipairs(lines) do
         local trimmed = lines[i]:match("^%s*(.-)%s*$") or ""
         local trimmed = line:match("^%s*(.-)%s*$") or ""
       
         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, lines[i])
         elseif trimmed:match("^%{%|") then
         elseif trimmed:match("^%{%|") then
            in_table = true
             table.insert(result, lines[i])
             table.insert(result, line)
         elseif trimmed:match("^|%}") then
         elseif trimmed:match("^|%}") then
            in_table = false
             table.insert(result, lines[i])
             table.insert(result, line)
         elseif trimmed:match("^|%-") then
         elseif in_table then
            table.insert(result, lines[i])
            if trimmed == "|-" then
        elseif trimmed:match("^!") then
                table.insert(result, "|-")
            local headers = {}
            elseif trimmed:match("^!") then
            while i <= #lines and lines[i]:match("^%s*!") do
                -- Header row
                table.insert(headers, lines[i])
                local cells = {}
                i = i + 1
                local temp = line
            end
                while temp:find("!!") do
            i = i - 1
                    local pre, post = temp:match("^(.-)!!(.*)$")
            if #headers == 3 then
                    local cell = pre:match("^%s*!%s*'''(.*)'''%s*$") or pre:match("^%s*!%s*(.-)%s*$") or ""
                table.insert(result, headers[1])
                    table.insert(cells, cell)
                 table.insert(result, headers[3])
                    temp = post
            end
                end
        elseif trimmed:match("^|") and not trimmed:match("^|%-") and not trimmed:match("^|%}") then
                local cell = temp:match("^%s*!%s*'''(.*)'''%s*$") or temp:match("^%s*!%s*(.-)%s*$") or ""
            local cells = {}
                table.insert(cells, cell)
            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])
                local new_cells = {}
                 i = i + 1
                for i = 1, #cells do
            end
                    if i ~= 2 then
            i = i - 1
                        table.insert(new_cells, "! '''" .. cells[i] .. "'''")
            if #cells == 3 then
                    end
                table.insert(result, cells[1])
                end
                 table.insert(result, cells[3])
                 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
             end
         else
         else
             table.insert(result, line)
             table.insert(result, lines[i])
         end
         end
        i = i + 1
     end
     end
   
     return table.concat(result, "\n")
     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