Module:Numlinks 2-num: Difference between revisions

Ganaram inukshuk (talk | contribs)
mNo edit summary
Ganaram inukshuk (talk | contribs)
m update todo
 
(8 intermediate revisions by the same user not shown)
Line 7: Line 7:


-- TODO: add arrows
-- TODO: add arrows
-- TODO: add checks for edge cases


-- Main function
-- Main function
Line 62: Line 61:
local display_text = string.format(current_fmt, num_1, num_2)
local display_text = string.format(current_fmt, num_1, num_2)
return string.format("%s", display_text)
return string.format("%s", display_text)
end
local function clamp(value, min_val, max_val)
    return math.min(math.max(value, min_val), max_val)
end
end
-- Create a grid of links
-- Create a grid of links.
local num_rows = 1 + num_links_2 * 2
-- Offsets are the number of cells from the left and upper edges, capped
local num_cols = 1 + num_links_1 * 2
-- between the number of links in each direction and the allowed minima for
local row_offset = 1 + num_links_2
-- each direction.
local col_offset = 1 + num_links_1
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 num_rows = num_links_2 + row_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
local row = {}
local row = {}
for i = 1, num_cols do
for i = 1, num_cols do
local num_2 = format_number(j - row_offset, is_ordinal_2)
local num_2 = format_number(j - row_offset + curr_num_2, is_ordinal_2)
local num_1 = format_number(i - col_offset, is_ordinal_1)
local num_1 = format_number(i - col_offset + curr_num_1, is_ordinal_1)
if i == col_offset and j == row_offset then
if i == col_offset and j == row_offset then
Line 85: Line 91:
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_links_1 + 2)
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[i][j]))
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[i][j]))
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[i][j]))
table.insert(lines, string.format('| style="font-size: 0.75em; | %s', links[j][i]))
end
end
end
end
Line 133: Line 132:


return result
return result
end
function p.tester()
local args = {
["Current Num 1"] = 5,
["Current Num 2"] = 2,
["Link Count 1"] = 7,
["Link Count 2"] = 4,
["Link Format"] = "%sL %ss"
}
return p._numlinks_2_num(args)
end
end


return p
return p