FANDOM


(Undo revision 153741 by Capefeather (talk))
(revert 1)
Line 3: Line 3:
 
local p = {}
 
local p = {}
   
local function delimiterPositions(wikitable, delimiter, ender)
+
local function rowPositions(wikitable)
-- Gets the positions and number of instances of a delimiter in a table's wikitext. By "position", we mean that, for example, the letter "e" in "hello" is in position 2, and the pattern "ll" in "hello" is in position 3.
+
-- Marks the positions of instances of the row delimiter |- in a table's wikitext. By "position", we mean that, for example, the letter "e" in "hello" is in position 2.
 
local positions = {}
 
local positions = {}
 
local index = 1
 
local index = 1
local pos = 1
 
   
 
while true do
 
while true do
if string.sub(wikitable, pos, pos + string.length(delimiter)) == delimiter then
+
if string.sub(wikitable, pos, pos + 1) == "|-" then
 
positions[index] = pos
 
positions[index] = pos
 
index = index + 1
 
index = index + 1
elseif string.sub(wikitable, pos, pos + string.length(ender)) == ender then
+
elseif string.sub(wikitable, pos, pos + 1) == "|}" then
positions[index] = pos
 
 
break
 
break
 
end
 
end
pos = pos + 1
 
 
end
 
end
  +
  +
positions[index] = string.len(wikitable) - 1
   
 
return {positions, index - 1}
 
return {positions, index - 1}
end
 
 
local function rowPositions(wikitable)
 
-- Gets the positions of instances of the row delimiter |- in a table's wikitext.
 
return delimiterPositions(wikitable, "|-", "|}")[1]
 
end
 
 
local function rows(wikitable)
 
-- Gets the number of rows in a table's wikitext.
 
return delimiterPositions(wikitable, "|-", "|}")[2]
 
 
end
 
end
   
 
local function fetchRows(wikitable, firstRow, lastRow)
 
local function fetchRows(wikitable, firstRow, lastRow)
 
-- Gets the contents of the rows between firstRow and lastRow inclusively. For example, fetchRows(wikitable, 3, 7) returns rows 3, 4, 5, 6, and 7 of the table.
 
-- Gets the contents of the rows between firstRow and lastRow inclusively. For example, fetchRows(wikitable, 3, 7) returns rows 3, 4, 5, 6, and 7 of the table.
local rowP = rowPositions(wikitable)
+
local rowP = rowPositions(wikitable)[1]
 
return string.sub(wikitable, rowP[firstRow], rowP[lastRow + 1])
 
return string.sub(wikitable, rowP[firstRow], rowP[lastRow + 1])
 
end
 
end
Line 41: Line 31:
 
-- Gets the contents of a single row.
 
-- Gets the contents of a single row.
 
return fetchRows(wikitable, row, row)
 
return fetchRows(wikitable, row, row)
end
 
 
local function columns(wikitable)
 
-- Gets the names of the columns.
 
names = {}
 
posTable = delimiterPositions(wikitable, "!", "|-")
 
for n = 1, posTable[2] do
 
names[n] = string.sub(wikitable, posTable[1][n] + 1, posTable[1][n + 1] - 1)
 
end
 
return names
 
 
end
 
end
   
Line 56: Line 36:
 
-- Constructs a subtable of the given table containing the specified rows. For example, {{#invoke:table|partialTable|(table)|3|7}} produces a table using rows 3-7 of the original table.
 
-- Constructs a subtable of the given table containing the specified rows. For example, {{#invoke:table|partialTable|(table)|3|7}} produces a table using rows 3-7 of the original table.
 
local wikitable = frame.args[1]
 
local wikitable = frame.args[1]
local rowP = rowPositions(wikitable)
+
local rowP = rowPositions(wikitable)[1]
local length = rows(wikitable)
+
local length = rowPositions(wikitable)[2]
 
local firstRow = math.max(frame.args[2], 1) or 1
 
local firstRow = math.max(frame.args[2], 1) or 1
 
local lastRow = math.min(frame.args[3], length) or length
 
local lastRow = math.min(frame.args[3], length) or length

Revision as of 04:26, February 13, 2020

-- Module for searching and manipulating wikitables.
 
local p = {}
 
local function rowPositions(wikitable)
    -- Marks the positions of instances of the row delimiter |- in a table's wikitext. By "position", we mean that, for example, the letter "e" in "hello" is in position 2.
    local positions = {}
    local index = 1
 
    while true do
        if string.sub(wikitable, pos, pos + 1) == "|-" then
            positions[index] = pos
            index = index + 1
        elseif string.sub(wikitable, pos, pos + 1) == "|}" then
            break
        end
    end
 
    positions[index] = string.len(wikitable) - 1
 
    return {positions, index - 1}
end
 
local function fetchRows(wikitable, firstRow, lastRow)
    -- Gets the contents of the rows between firstRow and lastRow inclusively. For example, fetchRows(wikitable, 3, 7) returns rows 3, 4, 5, 6, and 7 of the table.
    local rowP = rowPositions(wikitable)[1]
    return string.sub(wikitable, rowP[firstRow], rowP[lastRow + 1])
end
 
local function fetchOneRow(wikitable, row)
    -- Gets the contents of a single row.
    return fetchRows(wikitable, row, row)
end
 
function p.partialTable(frame)
    -- Constructs a subtable of the given table containing the specified rows. For example, {{#invoke:table|partialTable|(table)|3|7}} produces a table using rows 3-7 of the original table.
    local wikitable = frame.args[1]
    local rowP = rowPositions(wikitable)[1]
    local length = rowPositions(wikitable)[2]
    local firstRow = math.max(frame.args[2], 1) or 1
    local lastRow = math.min(frame.args[3], length) or length
    local header = string.sub(wikitable, 1, rowP[1] - 1)
    local body = fetchRows(wikitable, firstRow, lastRow)
    return header .. body .. "}"
end
 
return p
 
--[[Category:Modules]]
Community content is available under CC-BY-SA unless otherwise noted.