Module:Numlinks: Difference between revisions

Ganaram inukshuk (talk | contribs)
m bugfix
Ganaram inukshuk (talk | contribs)
mNo edit summary
Line 5: Line 5:


local p = {}
local p = {}
-- TODO
-- - (Med priority) Rename "Num" to "Current Num", and "Displayed Text" to
--  "Display Text".
-- - (Low priority) Add a maximum allowed number. This isn't necessary in most
--  cases, so such a max would default to infinity.


-- Main function
-- Main function
function p._numlinks(args)
function p._numlinks(args)
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 num_links  = args["Link Count"] or 1 -- Number of links to display in each direction (EG, for 12edo, with default of 1, shows links for 11edo and 13edo)
local curr_num    = tonumber(args["Num"]) -- Current number (EG, 12edo)
local curr_num    = args["Num"] -- Current number (EG, 12edo)
local min_num    = tonumber(args["Min"]) or 1 -- Minimum valid number (EG, for 12edo, this corresponds to 1edo)
local min_num    = args["Min"] or 1 -- Minimum valid number (EG, for 12edo, this corresponds to 1edo)
local is_ordinal  = yesno(args["Is Ordinal"], false) -- Whether to format numbers as ordinals (EG, for nth-octave temperament pages)
local is_ordinal  = yesno(args["Is Ordinal"], false) -- Whether to format numbers as ordinals (EG, for nth-octave temperament pages)
local is_table    = yesno(args["Is Table"  ], true) -- Toggle whether to display as a 3-cell table or one line
local is_table    = yesno(args["Is Table"  ], true) -- Toggle whether to display as a 3-cell table or one line


-- Format strings
-- Format strings.
local link_fmt    = args["Link Format"] or "%s" -- Format for link target (EG, %sedo)
-- - Link format is for the link to the page, EG "%sedo".
local display_fmt = args["Displayed Format"  ] or link_fmt -- Format for displayed text in links, if it differs from the link text
-- - Display format is if the displayed text differs from that of the link
local current_fmt = args["Current Page Format"] or link_fmt -- Format for current page text, if it differs from the link text
--  text (EG, a link that says 7\12, but links to 12edo).
-- - Current page format is for the current page's text, if it's different
--  from either link or display text.
-- - By default, all three formats are the same as link_fmt.
local link_fmt    = args["Link Format"       ] or "%s"
local display_fmt = args["Displayed Format"  ] or link_fmt
local current_fmt = args["Current Page Format"] or link_fmt


-- Helper function to format a number
-- Nested helper function to format a number
local function format_number(n)
local function format_number(n)
local str = is_ordinal and ordinal(n) or tostring(n)
local str = is_ordinal and ordinal(n) or tostring(n)
Line 37: Line 49:
end
end


-- Helper function to make current page text
-- Nested helper function to make current page text
local function make_current(num)
local function make_current(num)
local display_text = string.format(current_fmt, num)
local display_text = string.format(current_fmt, num)
Line 53: Line 65:
local n = curr_num - i
local n = curr_num - i
if n >= min_num then
if n >= min_num then
-- Add link and a bullet after it
table.insert(prev_links, make_link(format_number(n)))
table.insert(prev_links, make_link(format_number(n)))
table.insert(prev_links, "•")
-- If output is a table, don't add a bullet after the last link
if n - 1 ~= curr_num and is_table then
table.insert(prev_links, "•")
end
end
end
end
end
Line 67: Line 76:
local n = curr_num + i
local n = curr_num + i
-- If output is a table, don't add a bullet before the first link
-- Add link and a bullet before it
if n + 1 ~= curr_num and is_table then
table.insert(next_links, "•")
table.insert(next_links, "•")
end
table.insert(next_links, make_link(format_number(n)))
table.insert(next_links, make_link(format_number(n)))
end
end
Line 78: Line 84:
-- Current page text
-- Current page text
local current = make_current(format_number(curr_num))
local current = make_current(format_number(curr_num))
-- If the output is a table, remove the last element from prev_links and
-- remove the first element from next_links. In both cases, it should be a
-- bullet. A quick and dirty way to do this is to call table.remove().
if is_table then
if #prev_links ~= 0 then table.remove(prev_links) end
table.remove(next_links, 1)
end


-- Output
-- Output