Module:Numlinks 2-num

Revision as of 10:53, 13 October 2025 by Ganaram inukshuk (talk | contribs) (remove test args)
Module documentation[view] [edit] [history] [purge]
This module implements a metatemplate, and may be invoked by templates using its corresponding template Template:Numlinks 2-num, or used directly from other modules.

This module generates previous and next links for a numbered page with two varying numbers.


Introspection summary for Module:Numlinks 2-num 
Functions provided (2)
Line Function Params
11 _numlinks_2_num (main) (args)
101 numlinks_2_num (invokable) (frame)
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: add arrows

-- Main function
function p._numlinks_2_num(args)
	-- Numbers. These are assumed to be the number datatype already (EG, already
	-- parsed from a string).
	-- - Current num N is the, well, current number. EG, the 5 and 2 in 5L 2s.
	-- - Min N is the smallest allowed value. EG, with the default minimum of 1,
	--   if the page were 1L 1s, there would be no link to 0L 0s.
	-- - No option exists for the number of links in each direction.
	local curr_num_1 = args["Current Num 1"]
	local curr_num_2 = args["Current Num 2"]
	local min_num_1  = args["Min 1"] or 1
	local min_num_2  = args["Min 2"] or 1
	
	-- Format strings.
	-- - Link format is for the link to the page. EG, "%L %ss".
	-- - Display format is if the displayed text differs from that of the link
	--   text. EG, a link that says 4L 5s, but links to 4L 5s (3/1-equivalent).
	-- - Current page format is for the current page's text, if it's different
	--   from either link or display text.
	local link_fmt    = args["Link Format"        ] or "%s %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.
	-- - No toggle for table-based output exists; it must default to a table.
	local is_ordinal_1 = yesno(args["Is Ordinal 1"], false)
	local is_ordinal_2 = yesno(args["Is Ordinal 2"], false)
	
	-- Nested helper function to format a number.
	local function format_number(n, is_ordinal)
		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_1, num_2)
		local link_target  = string.format(link_fmt   , num_1, num_2)
		local display_text = string.format(display_fmt, num_1, num_2)

		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_1, num_2)
		local display_text = string.format(current_fmt, num_1, num_2)
		return string.format("%s", display_text)
	end
	
	local links = {}
	
	for j = 1, 3 do
		local row = {}
		for i = 1, 3 do
			local num_2 = format_number(curr_num_2 + i - 2, is_ordinal_2)
			local num_1 = format_number(curr_num_1 + j - 2, is_ordinal_1)
			
			if i == 2 and j == 2 then
				table.insert(row, make_current(num_1, num_2))
			else
				table.insert(row, make_link(num_1, num_2))
			end
		end
		table.insert(links, row)
	end

	local lines = {}
	
	table.insert(lines, '{| style="width: 100%;"')
	table.insert(lines, '|-')
	table.insert(lines, '| style="font-size: 0.75em;" | '             .. links[1][1])
	table.insert(lines, '| style="font-size: 0.75em; width: 50%;" | ' .. links[1][2])
	table.insert(lines, '| style="font-size: 0.75em;" | '             .. links[1][3])
	table.insert(lines, '|-')
	table.insert(lines, '| style="font-size: 0.75em;" | '             .. links[2][1])
	table.insert(lines, '| style="width: 50%;"  | '                   .. links[2][2])
	table.insert(lines, '| style="font-size: 0.75em;" | '             .. links[2][3])
	table.insert(lines, '|-')
	table.insert(lines, '| style="font-size: 0.75em;" | '             .. links[3][1])
	table.insert(lines, '| style="font-size: 0.75em; width: 50%;" | ' .. links[3][2])
	table.insert(lines, '| style="font-size: 0.75em;" | '             .. links[3][3])
	table.insert(lines, '|}')
	
	return table.concat(lines, "\n")
end

-- Main function
function p.numlinks_2_num(frame)
	local args = getArgs(frame)
	
	-- Preprocess numeric input
	args["Current Num 1"] = tonumber(args["Current Num 1"])		-- Required arg
	args["Current Num 2"] = tonumber(args["Current Num 2"])		-- Required arg
	args["Min 1"] = tonumber(args["Min 1"]) or 1
	args["Min 2"] = tonumber(args["Min 2"]) or 1
	
	-- Preprocess toggles
	args["Is Ordinal 1"] = args["Is Ordinal 1"] or false
	args["Is Ordinal 2"] = args["Is Ordinal 2"] or false
	
	-- Create numbered navigation links
	local result = p._numlinks(args)

	return result
end

return p