Module:Numlinks 2-num: Difference between revisions
m remove test args |
m update todo |
||
| (24 intermediate revisions by the same user not shown) | |||
| Line 16: | Line 16: | ||
-- if the page were 1L 1s, there would be no link to 0L 0s. | -- if the page were 1L 1s, there would be no link to 0L 0s. | ||
-- - No option exists for the number of links in each direction. | -- - No option exists for the number of links in each direction. | ||
local curr_num_1 = args["Current Num 1"] | local curr_num_1 = args["Current Num 1"] | ||
local curr_num_2 = args["Current Num 2"] | local curr_num_2 = args["Current Num 2"] | ||
local min_num_1 | local num_links_1 = args["Link Count 1"] or 1 | ||
local min_num_2 | local num_links_2 = args["Link Count 2"] or 1 | ||
local min_num_1 = args["Min 1"] or 1 | |||
local min_num_2 = args["Min 2"] or 1 | |||
-- Format strings. | -- Format strings. | ||
| Line 59: | 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. | |||
-- 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 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, | |||
local row = {} | local row = {} | ||
for i = 1, | for i = 1, num_cols do | ||
local num_2 = format_number(curr_num_2 | local num_2 = format_number(j - row_offset + curr_num_2, is_ordinal_2) | ||
local num_1 = format_number(curr_num_1 | local num_1 = format_number(i - col_offset + curr_num_1, is_ordinal_1) | ||
if i == | if i == col_offset and j == row_offset then | ||
table.insert(row, make_current(num_1, num_2)) | table.insert(row, make_current(num_1, num_2)) -- Text is for the current page | ||
else | else | ||
table.insert(row, make_link(num_1, num_2)) | table.insert(row, make_link(num_1, num_2)) -- Text is for an adjacent link | ||
end | end | ||
end | end | ||
| Line 78: | 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 | |||
-- New row | |||
table.insert(lines, '|-') | |||
for i = 1, num_cols do | |||
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 | |||
-- Insert text for non-current middle cell | |||
table.insert(lines, string.format('| style="font-size: 0.75em; | %s', links[j][i])) | |||
end | |||
end | |||
end | |||
table.insert(lines, '|}') | table.insert(lines, '|}') | ||
| Line 107: | Line 121: | ||
args["Min 1"] = tonumber(args["Min 1"]) or 1 | args["Min 1"] = tonumber(args["Min 1"]) or 1 | ||
args["Min 2"] = tonumber(args["Min 2"]) or 1 | args["Min 2"] = tonumber(args["Min 2"]) or 1 | ||
args["Link Count 1"] = tonumber(args["Link Count 1"]) or 1 | |||
args["Link Count 2"] = tonumber(args["Link Count 2"]) or 1 | |||
-- Preprocess toggles | -- Preprocess toggles | ||
| Line 113: | Line 129: | ||
-- Create numbered navigation links | -- Create numbered navigation links | ||
local result = p. | local result = p._numlinks_2_num(args) | ||
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 | ||