Module:Numlinks: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Ganaram inukshuk (talk | contribs)
m some cleanup
Tag: Manual revert
ArrowHead294 (talk | contribs)
m Wikitext debugger option
 
Line 139: Line 139:
function p.numlinks(frame)
function p.numlinks(frame)
local args = getArgs(frame)
local args = getArgs(frame)
local wtext = yesno(frame.args["wtext"] or args["wtext"])
-- Preprocess numeric input
-- Preprocess numeric input
Line 151: Line 152:
-- Create numbered navigation links
-- Create numbered navigation links
local result = p._numlinks(args)
local result = p._numlinks(args)
-- Debugger option to show Wikitext
if wtext then
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
end


return result
return frame:preprocess(result)
end
end



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