Module:RemoveMiddleColumn
Appearance
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 == "|-" then
table.insert(result, "|-")
elseif trimmed:match("^!") then
-- Header row
local cells = {}
local temp = line
while temp:find("!!") do
local pre, post = temp:match("^(.-)!!(.*)$")
local cell = pre:match("^%s*!%s*'''(.*)'''%s*$") or pre:match("^%s*!%s*(.-)%s*$") or ""
table.insert(cells, cell)
temp = post
end
local cell = temp:match("^%s*!%s*'''(.*)'''%s*$") or 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 > 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
else
table.insert(result, line)
end
end
return table.concat(result, "\n")
end
return p