Module:Numlinks 2-num: Difference between revisions
mNo edit summary |
m update todo |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 7: | Line 7: | ||
-- TODO: add arrows | -- TODO: add arrows | ||
-- 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 | -- Offsets are the number of cells from the left and upper edges, capped | ||
local | -- between the number of links in each direction and the allowed minima for | ||
local | -- each direction. | ||
local | 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 | ||
| Line 85: | Line 91: | ||
end | end | ||
-- Produce table of links | |||
local lines = {} | local lines = {} | ||
table.insert(lines, '{| style="width: 100%;"') | table.insert(lines, '{| style="width: 100%;"') | ||
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 | if i == col_offset and j == row_offset then | ||
-- Insert text for current-page cell | |||
table.insert(lines, string.format('| %s', links[j][i])) | |||
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; | 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 | ||