Module:Numlinks: Difference between revisions
Jump to navigation
Jump to search
m bugfix |
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 = | 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 = | local curr_num = args["Num"] -- Current number (EG, 12edo) | ||
local min_num = | 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" | -- - Link format is for the link to the page, EG "%sedo". | ||
local display_fmt = args["Displayed Format" ] or link_fmt | -- - Display format is if the displayed text differs from that of the link | ||
local current_fmt = args["Current Page Format"] or link_fmt | -- 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 | |||
-- | -- 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 | ||
-- | -- 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, "•") | |||
end | end | ||
end | end | ||
| Line 67: | Line 76: | ||
local n = curr_num + i | local n = curr_num + i | ||
-- | -- Add link and a bullet before it | ||
table.insert(next_links, "•") | |||
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 | ||
Revision as of 22:57, 9 October 2025
- This module implements a metatemplate, and may be invoked by templates using its corresponding template Template:Numlinks, or used directly from other modules.
This module generates previous and next links for a numbered page.
| Introspection summary for Module:Numlinks | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||||||||||||||||||||
No function descriptions were provided. The Lua code may have further information.
-- Page is following provisonal style guide: User:Ganaram_inukshuk/Provisional_style_guide_for_Lua
local getArgs = require("Module:Arguments").getArgs
local ordinal = require("Module:Ordinal" )._ordinal
local yesno = require("Module:Yesno" )
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
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)
local curr_num = args["Num"] -- Current number (EG, 12edo)
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_table = yesno(args["Is Table" ], true) -- Toggle whether to display as a 3-cell table or one line
-- Format strings.
-- - Link format is for the link to the page, EG "%sedo".
-- - 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).
-- - 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
-- Nested helper function to format a number
local function format_number(n)
local str = is_ordinal and ordinal(n) or tostring(n)
return str
end
-- 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
-- Nested 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
-- 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
-- arrow to the beginning.
local prev_links = {}
if curr_num - num_links >= min_num then
table.insert(prev_links, "←")
end
for i = num_links, 1, -1 do
local n = curr_num - i
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, "•")
end
end
-- Generate next links. Add an arrow after the last link.
local next_links = {}
for i = 1, num_links do
local n = curr_num + i
-- Add link and a bullet before it
table.insert(next_links, "•")
table.insert(next_links, make_link(format_number(n)))
end
table.insert(next_links, "→")
-- Current page text
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
if is_table then
local out = {}
table.insert(out, '{| style="width: 100%;"')
table.insert(out, '|-')
table.insert(out, '| style="font-size: 0.75em;" | ' .. table.concat(prev_links, " "))
table.insert(out, '| style="width: 50%;" | ' .. current)
table.insert(out, '| style="font-size: 0.75em;" | ' .. table.concat(next_links, " "))
table.insert(out, '|}')
return table.concat(out, "\n")
else
local out = {}
table.insert(out, table.concat(prev_links, " "))
table.insert(out, current)
table.insert(out, table.concat(next_links, " "))
return table.concat(out, " ")
end
end
-- wip; to be placed in its own module
function p.numlinks_2num(args)
local curr_num_1 = args["Num 1"]
local min_num_1 = args["Min 1"] or 1
local curr_num_2 = args["Num 2"]
local min_num_2 = args["Min 2"] or 1
local disp_text = args["Page Text"] or { "PRE-TEXT", "MID-TEXT", "POST-TEXT" }
local link_text = args["Link Text"] or nil
local is_ordinal_1 = args["Is Ordinal 1"] ~= nil and args["Is Ordinal 1"] or false
local is_ordinal_2 = args["Is Ordinal 1"] ~= nil and args["Is Ordinal 2"] or false
local x = curr_num_1
local y = curr_num_2
local link_nums = {
{x-1, y-1}, {x , y-1}, {x+1, y-1},
{x-1, y }, {x+1, y },
{x-1, y+1}, {x , y+1}, {x+1, y+1}
}
local links = {}
for i = 1, #link_nums do
end
end
-- Main function (TODO: split functionality into two modules)
function p.numlinks(frame)
local args = getArgs(frame)
args["Num"] = tonumber(args["Num"]) or 123
args["Min"] = tonumber(args["Min"]) or 1
args["Link Count"] = tonumber(args["Link Count"]) or 1
-- Create numbered navigation links
local result = p._numlinks(args)
return result
end
function p.tester()
local args = {
["Link Count"] = 3,
["Num"] = 3,
["Link Format"] = "%s-octave temperaments",
["Current Page Format"] = "%s-octave",
["Is Ordinal"] = 1,
["Is Table" ] = 0
}
return p._numlinks(args)
end
return p