Module:Numlinks: Difference between revisions
todo |
refactored |
||
| Line 5: | Line 5: | ||
local p = {} | local p = {} | ||
-- Main function | -- Main function | ||
function p._numlinks(args) | function p._numlinks(args) | ||
local num_links | local num_links = tonumber(args["Link Count"]) or 1 -- Number of links to display in each direction (EG, for 12edo, with default of 1, show links for 11edo and 13edo) | ||
local curr_num | local curr_num = tonumber(args["Num"]) -- Current number (EG, 12edo) | ||
local min_num | local min_num = tonumber(args["Min"]) or 1 -- Minimum valid number (EG, for 12edo, this corresponds to 1edo) | ||
local | local is_ordinal = yesno(args["Is Ordinal"], false) -- Whether to format numbers as ordinals (EG, for nth-octave temperament pages) | ||
local | local is_table = yesno(args["Is Table" ], true) -- Toggle whether to display as a 3-cell table or one line | ||
local | |||
local | -- Format strings | ||
local | local link_fmt = args["Link Format"] or "%s" -- Format for link target (EG, %sedo) | ||
local display_fmt = args["Displayed Format" ] or link_fmt -- Format for displayed text in links, if it differs from the link text | |||
-- | local current_fmt = args["Current Page Format"] or display_fmt -- Format for current page text, if it differs from the link text | ||
-- Helper function to format a number | |||
local function format_number(n) | |||
local str = is_ordinal and ordinal(n) or tostring(n) | |||
return str | |||
end | end | ||
if | -- Nested helper function to make a link | ||
local function make_link(num) | |||
local link_target = string.format(link_fmt, num) | |||
local display_text = string.format(display_fmt, num) | |||
if display_fmt == link_fmt then | |||
return string.format("[[%s]]", link_target) | |||
else | |||
return string.format("[[%s|%s]]", link_target, display_text) | |||
end | end | ||
end | end | ||
-- Helper function to make current page text | |||
local function make_current(num) | |||
local display_text = string.format(current_fmt, num) | |||
return string.format("'''%s'''", display_text) | |||
end | end | ||
-- Generate previous links | |||
-- | local prev_parts = {} | ||
local | |||
if curr_num - num_links >= min_num then | if curr_num - num_links >= min_num then | ||
table.insert( | table.insert(prev_parts, "←") | ||
end | end | ||
for i = num_links, 1, -1 do | for i = num_links, 1, -1 do | ||
local | local n = curr_num - i | ||
if n >= min_num then | |||
table.insert(prev_parts, make_link(format_number(n))) | |||
table.insert(prev_parts, "•") | |||
table.insert( | |||
end | end | ||
end | end | ||
-- | -- Generate next links | ||
local | local next_parts = {} | ||
for i = 1, num_links do | for i = 1, num_links do | ||
local | local n = curr_num + i | ||
table.insert(next_parts, "•") | |||
table.insert(next_parts, make_link(format_number(n))) | |||
table.insert( | |||
table.insert( | |||
end | end | ||
table.insert(next_parts, "→") | |||
-- Current page text | |||
local current = make_current(format_number(curr_num)) | |||
-- | -- Output | ||
if is_table then | if is_table then | ||
local | local out = {} | ||
table.insert( | table.insert(out, '{| style="width: 100%;"') | ||
table.insert( | table.insert(out, '|-') | ||
table.insert( | table.insert(out, '| style="font-size: 0.75em;" | ' .. table.concat(prev_parts, " ")) | ||
table.insert( | table.insert(out, '| style="width: 50%;" | ' .. current) | ||
table.insert( | table.insert(out, '| style="font-size: 0.75em;" | ' .. table.concat(next_parts, " ")) | ||
table.insert( | table.insert(out, '|}') | ||
return table.concat(out, "\n") | |||
return table.concat( | |||
else | else | ||
local | local out = {} | ||
table.insert( | table.insert(out, table.concat(prev_parts, " ")) | ||
table.insert( | table.insert(out, current) | ||
table.insert( | table.insert(out, table.concat(next_parts, " ")) | ||
return table.concat(out, " ") | |||
return table.concat( | |||
end | end | ||
end | end | ||
| Line 133: | Line 119: | ||
args["Min"] = tonumber(args["Min"]) or 1 | args["Min"] = tonumber(args["Min"]) or 1 | ||
args["Link Count"] = tonumber(args["Link Count"]) or 1 | args["Link Count"] = tonumber(args["Link Count"]) or 1 | ||
-- Create numbered navigation links | -- Create numbered navigation links | ||
| Line 166: | Line 130: | ||
["Link Count"] = 3, | ["Link Count"] = 3, | ||
["Num"] = 3, | ["Num"] = 3, | ||
["Link | ["Link Format"] = "%s-octave temperaments", | ||
["Current Page Format"] = "%s-octave", | |||
["Current Page | |||
["Is Ordinal"] = 1, | ["Is Ordinal"] = 1, | ||
["Is Table" ] = | ["Is Table" ] = 0 | ||
} | } | ||