Module:Numlinks: Difference between revisions

Ganaram inukshuk (talk | contribs)
mNo edit summary
Ganaram inukshuk (talk | contribs)
comments; code changes
Line 7: Line 7:


-- TODO
-- 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
-- - (Low priority) Add a maximum allowed number. This isn't necessary in most
--  cases, so such a max would default to infinity.
--  cases, so such a max would default to infinity.
Line 14: Line 12:
-- Main function
-- Main function
function p._numlinks(args)
function p._numlinks(args)
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)
-- Numbers. These are assumed to be the number datatype already (EG, already
local curr_num   = args["Num"] -- Current number (EG, 12edo)
-- parsed from a string).
local min_num    = args["Min"] or 1 -- Minimum valid number (EG, for 12edo, this corresponds to 1edo)
-- - Current num is the, well, current number. EG, the 12 from 12edo.
local is_ordinal  = yesno(args["Is Ordinal"], false) -- Whether to format numbers as ordinals (EG, for nth-octave temperament pages)
-- - Num links is how many links to display left and right. EG, for 12edo,
local is_table    = yesno(args["Is Table"  ], true) -- Toggle whether to display as a 3-cell table or one line
--  with the default of 1, this shows links for 11edo and 13edo. Max number
 
--  of allowed links is 10.
-- - Min is the smallest allowed value. EG, with the default minimum of 1,
--  if the page were 1edo, there would be no link for 0edo.
local curr_num = args["Current Num"]
local num_links = args["Link Count" ] or 1
local min_num  = args["Min"       ] or 1
-- Clamp numlinks.
num_links = math.max(1, math.min(num_links, 10))
-- Format strings.
-- Format strings.
-- - Link format is for the link to the page, EG "%sedo".
-- - Link format is for the link to the page. EG, "%sedo".
-- - Display format is if the displayed text differs from that of the link
-- - Display format is if the displayed text differs from that of the link
--  text (EG, a link that says 7\12, but links to 12edo).
--  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
-- - Current page format is for the current page's text, if it's different
--  from either link or display text.
--  from either link or display text. EG, links that display "nth", link to
-- - By default, all three formats are the same as link_fmt.
--  "nth-octave temperaments", and the current page displays "12th-octave".
-- - By default, all three formats are the same as link_fmt, which defaults
--  to displaying only the number.
local link_fmt    = args["Link Format"        ] or "%s"
local link_fmt    = args["Link Format"        ] or "%s"
local display_fmt = args["Displayed Format"  ] or link_fmt
local display_fmt = args["Displayed Format"  ] or link_fmt
local current_fmt = args["Current Page Format"] or link_fmt
local current_fmt = args["Current Page Format"] or display_fmt
-- Toggles.
-- - Is ordinal formats numbers as ordinals. EG, nth-octave temperament.
--  Default is false, since most pages tend to have cardinal numbers.
-- - Is table formats output as a table. Default is true, so it can be used
--  in the name field of an infobox, bypassing its manual link entry for
--  automated link entry. Passing in false concatenates everything into
--  one string, for use with simple navigation templates.
local is_ordinal = yesno(args["Is Ordinal"], false)
local is_table  = yesno(args["Is Table"  ], true )


-- Nested 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 56:
end
end


-- Nested helper function to make a link
-- Nested helper function to make a link.
local function make_link(num)
local function make_link(num)
local link_target  = string.format(link_fmt  , num)
local link_target  = string.format(link_fmt  , num)
Line 49: Line 68:
end
end


-- Nested 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 57: Line 76:
-- Generate previous links, if they are greater or equal to the minimum.
-- Generate previous links, if they are greater or equal to the minimum.
-- If the first link in the sequence is greater than the minimum, add an
-- If the first link in the sequence is greater than the minimum, add an
-- arrow to the beginning.
-- arrow before the first link.
local prev_links = {}
local prev_links = {}
if curr_num - num_links >= min_num then
if curr_num - num_links >= min_num then
Line 87: Line 106:
-- If the output is a table, remove the last element from prev_links and
-- 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
-- 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().
-- bullet. A quick and dirty way to do this is to call table.remove(),
-- although a better way could be implemented.
if is_table then
if is_table then
if #prev_links ~= 0 then table.remove(prev_links) end
if #prev_links ~= 0 then table.remove(prev_links) end
Line 141: Line 161:
local args = getArgs(frame)
local args = getArgs(frame)
args["Num"] = tonumber(args["Num"]) or 123
args["Current Num"] = tonumber(args["Current Num"]) or 12
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