Module:Numlinks 2-num: Difference between revisions

Ganaram inukshuk (talk | contribs)
add tester; add edge-case detection
Ganaram inukshuk (talk | contribs)
comments; remove width code for current-page cell
Line 68: Line 68:
end
end
-- Create a grid of links
-- Create a grid of links.
-- Offsets are the number of cells from the left and upper edges, capped
-- between the number of links in each direction and the allowed minima for
-- each direction.
local row_offset = clamp(num_links_2, num_links_2 + 1, curr_num_2)
local row_offset = clamp(num_links_2, num_links_2 + 1, curr_num_2)
local col_offset = clamp(num_links_1, num_links_1 + 1, curr_num_1)
local col_offset = clamp(num_links_1, num_links_1 + 1, curr_num_1)
local num_rows = num_links_2 + row_offset
local num_rows = num_links_2 + row_offset
local num_cols = num_links_1 + col_offset
local num_cols = num_links_1 + col_offset
local links = {}
local links = {}
for j = 1, num_rows do
for j = 1, num_rows do
Line 90: Line 92:
end
end


-- Produce table of links
local lines = {}
local lines = {}
-- Produce table of links
table.insert(lines, '{| style="width: 100%;"')
table.insert(lines, '{| style="width: 100%;"')
local middle_cell_width = 100 / (num_cols + 1)
for j = 1, num_rows do
for j = 1, num_rows do
-- New row
-- New row
table.insert(lines, '|-')
table.insert(lines, '|-')
for i = 1, num_cols do
for i = 1, num_cols do
if i == col_offset then
if i == col_offset and j == row_offset then
if j == row_offset then
-- Insert text for current-page cell
-- Insert text for current-page cell
table.insert(lines, string.format('| %s', links[j][i]))
table.insert(lines, string.format('| style="width: %d%%;" | %s', middle_cell_width, links[j][i]))
else
-- Insert text for non-current middle cell
table.insert(lines, string.format('| style="font-size: 0.75em; width: %d%%;" | %s', middle_cell_width, links[j][i]))
end
else
else
-- Insert text for non-middle cell
-- Insert text for non-current middle cell
table.insert(lines, string.format('| style="font-size: 0.75em;" | %s', links[j][i]))
table.insert(lines, string.format('| style="font-size: 0.75em; | %s', links[j][i]))
end
end
end
end