Module:Numlinks: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Ganaram inukshuk (talk | contribs)
Created page with "-- Page is following provisonal style guide: User:Ganaram_inukshuk/Provisional_style_guide_for_Lua local ordinal = require("Module:Ordinal")._ordinal -- Generate a table of..."
 
ArrowHead294 (talk | contribs)
m Wikitext debugger option
 
(35 intermediate revisions by 2 users not shown)
Line 1: Line 1:
-- Page is following provisonal style guide: User:Ganaram_inukshuk/Provisional_style_guide_for_Lua
-- This module follows [[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 ordinal = require("Module:Ordinal")._ordinal
local p = {}


-- Generate a table of previous and next links for numbered pages, with support
-- TODO
-- for up to two numbers. Args are as follows:
-- - (Low priority) Add a maximum allowed number. This isn't necessary in most
-- - Link count: how many links up and down should be displayed? For n, a count
--  cases, so such a max would default to infinity.
--  of 1 gives links for n-1 and n+1; count 2 is n-2, n-1, n+1, and n+2. This
-- - (Low priority) Add table style as a param?
--  does not apply if there are two values n and m, to limit the number of
 
--  links to be made.
-- Main function
-- - Is ordinal: should the values be cardinal numbers (eg, 12edo) or ordinal
--  numbers (eg, 12th-octave)?
-- - Page text: Text is formatted as [pre-text][n][post-text], stored in a table
--  { pre } or { pre, post }. If two values, n and m, are present, then the
--  format is [pre-text-1][n][post-text-1][pre-text-2][m][post-text-2], where
--  the second pre/post text is entered as a second table.
-- - Link text: Same as page text, except this changes the text of the link. If
--  this is not specified, then the page and link text are the same.
-- - Min: The smallest allowed value for a numbered link. Default is 1. There
--  isn't a maximum since these numbers are assumed to be either natural or
--   whole numbers.
function p._numlinks(args)
function p._numlinks(args)
local link_count = args["Link Count"]
-- Numbers. These are assumed to be the number datatype already (EG, already
local is_ordinal  = args["Is Ordinal"] ~= nil and args["Is Ordinal"] or false
-- parsed from a string).
local page_text_1 = args["Page Text 1"]
-- - Current num is the, well, current number. EG, the 12 from 12edo.
local page_text_2 = args["Page Text 2"]
-- - Num links is how many links to display left and right. EG, for 12edo,
local link_text_1 = args["Link Text 1"] ~= nil and args["Link Text 1"] or args["Page Text 1"]
--  with the default of 1, this shows links for 11edo and 13edo. Max number
local link_text_2 = args["Link Text 2"] ~= nil and args["Link Text 2"] or args["Page Text 2"]
--  of allowed links is 10.
local min_1      = args["Min 1"] ~= nil and args["Min 1"] or 1
-- - Min is the smallest allowed value. EG, with the default minimum of 1,
local min_2      = args["Min 2"] ~= nil and args["Min 2"] or 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.
-- - 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. EG, links that display "nth", link to
--  "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 display_fmt = args["Display 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.
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. Links are generated if the pages they link to
-- are greater than or equal to the minimum.
-- If the first link in the sequence is greater than the minimum, add an
-- arrow before the first link.
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(),
-- although a better way could be implemented.
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
-- ...as a 3-cell table, for use with infoboxes.
local result = {}
table.insert(result, '{| style="width: 100%;"')
table.insert(result, '|-')
table.insert(result, '| style="font-size: 0.75em;" | ' .. table.concat(prev_links, " "))
table.insert(result, '| style="width: 50%;" | ' .. current)
table.insert(result, '| style="font-size: 0.75em;" | ' .. table.concat(next_links, " "))
table.insert(result, '|}')
return table.concat(result, "\n")
else
-- ...as one long string, for use with navboxes with prev/next links.
local result = {}
table.insert(result, table.concat(prev_links, " "))
table.insert(result, current)
table.insert(result, table.concat(next_links, " "))
return table.concat(result, " ")
end
end
end
-- Main function
function p.numlinks(frame)
local args = getArgs(frame)
local wtext = yesno(frame.args["wtext"] or args["wtext"])
-- Preprocess numeric input
args["Current Num"] = tonumber(args["Current Num"]) -- Required arg
args["Min"        ] = tonumber(args["Min"]) or 1
args["Link Count" ] = tonumber(args["Link Count"]) or 1
-- Preprocess toggles
args["Is Ordinal"] = args["Is Ordinal"] or false
args["Is Table"  ] = args["Is Table"  ] or true
-- Create numbered navigation links
local result = p._numlinks(args)
-- Debugger option to show Wikitext
if wtext then
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
end
return frame:preprocess(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

Latest revision as of 21:14, 5 December 2025

Module documentation[view] [edit] [history] [purge]
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 
Functions provided (3)
Line Function Params
14 _numlinks (main) (args)
139 numlinks (invokable) (frame)
163 tester none
Lua modules required (3)
Variable Module Functions used
getArgs Module:Arguments getArgs
ordinal Module:Ordinal _ordinal
yesno Module:Yesno yesno

No function descriptions were provided. The Lua code may have further information.


-- This module follows [[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
-- - (Low priority) Add a maximum allowed number. This isn't necessary in most
--   cases, so such a max would default to infinity.
-- - (Low priority) Add table style as a param?

-- Main function
function p._numlinks(args)
	-- Numbers. These are assumed to be the number datatype already (EG, already
	-- parsed from a string).
	-- - Current num is the, well, current number. EG, the 12 from 12edo.
	-- - Num links is how many links to display left and right. EG, for 12edo,
	--   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.
	-- - 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. EG, links that display "nth", link to
	--   "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 display_fmt = args["Display 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.
	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. Links are generated if the pages they link to
	-- are greater than or equal to the minimum.
	-- If the first link in the sequence is greater than the minimum, add an
	-- arrow before the first link.
	local prev_links = {}
	if curr_num - num_links > min_num then
		table.insert(prev_links, "&larr;")
	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, "&bull;")
		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, "&bull;")
		table.insert(next_links, make_link(format_number(n)))
	end
	table.insert(next_links, "&rarr;")

	-- 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(),
	-- although a better way could be implemented.
	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
		-- ...as a 3-cell table, for use with infoboxes.
		local result = {}
		table.insert(result, '{| style="width: 100%;"')
		table.insert(result, '|-')
		table.insert(result, '| style="font-size: 0.75em;" | ' .. table.concat(prev_links, "&nbsp;"))
		table.insert(result, '| style="width: 50%;" | ' .. current)
		table.insert(result, '| style="font-size: 0.75em;" | ' .. table.concat(next_links, "&nbsp;"))
		table.insert(result, '|}')
		return table.concat(result, "\n")
	else
		-- ...as one long string, for use with navboxes with prev/next links.
		local result = {}
		table.insert(result, table.concat(prev_links, "&nbsp;"))
		table.insert(result, current)
		table.insert(result, table.concat(next_links, "&nbsp;"))
		return table.concat(result, "&nbsp;")
	end
end

-- Main function
function p.numlinks(frame)
	local args = getArgs(frame)
	local wtext = yesno(frame.args["wtext"] or args["wtext"])
	
	-- Preprocess numeric input
	args["Current Num"] = tonumber(args["Current Num"])		-- Required arg
	args["Min"        ] = tonumber(args["Min"]) or 1
	args["Link Count" ] = tonumber(args["Link Count"]) or 1
	
	-- Preprocess toggles
	args["Is Ordinal"] = args["Is Ordinal"] or false
	args["Is Table"  ] = args["Is Table"  ] or true
	
	-- Create numbered navigation links
	local result = p._numlinks(args)
	
	-- Debugger option to show Wikitext
	if wtext then
		result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
	end

	return frame:preprocess(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