Module:Numlinks: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Ganaram inukshuk (talk | contribs)
No edit summary
ArrowHead294 (talk | contribs)
m Wikitext debugger option
 
(30 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 ordinal = require("Module:Ordinal")._ordinal
local getArgs = require("Module:Arguments").getArgs
local getArgs = require("Module:Arguments").getArgs
local yesno = require("Module:yesno")
local ordinal = require("Module:Ordinal"  )._ordinal
local yesno   = require("Module:Yesno"   )


local p = {}
local p = {}


function p.side_numlinks(args)
-- TODO
local num_links = args["Link Count"] or 3
-- - (Low priority) Add a maximum allowed number. This isn't necessary in most
local curr_num  = args["Num 1"] or 2
--  cases, so such a max would default to infinity.
local min_num   = args["Min 1"] or 1
-- - (Low priority) Add table style as a param?
local page_text  = args["Page Text"] or { "PRE-TEXT", "POST-TEXT" }
 
local link_text  = args["Link Text"] or nil
-- Main function
local is_ordinal = args["Is Ordinal 1"] ~= nil and args["Is Ordinal 1"] or false
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
-- Preprocess page and link text
-- Toggles.
if #page_text == 1 then
-- - Is ordinal formats numbers as ordinals. EG, nth-octave temperament.
page_text = { "", page_text[1] }
--  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
end
if link_text ~= nil then
 
if #link_text == 1 then
-- Nested helper function to make a link.
link_text = { "", link_text[1] }
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
end
end
 
-- Produce next links
-- Nested helper function to make current page text.
local next_links = ""
local function make_current(num)
for i = 1, num_links do
local display_text = string.format(current_fmt, num)
local link = ""
return string.format("%s", display_text)
local num = is_ordinal and ordinal(curr_num + i) or string.format("%s", curr_num + i)
end
if link_text == nil then
 
-- Link text is the same as the page text
-- Generate previous links. Links are generated if the pages they link to
link = string.format("[[%s%s%s]]", page_text[1], num, page_text[2])
-- are greater than or equal to the minimum.
else
-- If the first link in the sequence is greater than the minimum, add an
-- Link and page text are different
-- arrow before the first link.
link = string.format("[[%s%s%s|%s%s%s]]", page_text[1], num, page_text[2], link_text[1], num, link_text[2])
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
next_links = next_links .. link .. (i == num_links and "" or " ")
end
end
 
-- Produce prev links
-- Generate next links. Add an arrow after the last link.
local prev_links = ""
local next_links = {}
for i = 1, num_links do
for i = 1, num_links do
local link = ""
local n = curr_num + i
local num = is_ordinal and ordinal(curr_num - i) or string.format("%s", curr_num - i)
if curr_num - i >= min_num then
-- Add link and a bullet before it
if link_text == nil then
table.insert(next_links, "•")
-- Link text is the same as the page text
table.insert(next_links, make_link(format_number(n)))
link = string.format("[[%s%s%s]]", page_text[1], num, page_text[2])
else
-- Link and page text are different
link = string.format("[[%s%s%s|%s%s%s]]", page_text[1], num, page_text[2], link_text[1], num, link_text[2])
end
prev_links = (curr_num - i <= min_num and "" or "&nbsp;") .. link .. prev_links
end
end
end
table.insert(next_links, "&rarr;")
-- Current page text
local current = make_current(format_number(curr_num))
return { prev_links, next_links }
-- If the output is a table, remove the last element from prev_links and
end
-- 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


function p.eight_numlinks(args)
-- Output...
local curr_num_1  = args["Num 1"] or 2
if is_table then
local min_num_1    = args["Min 1"] or 1
-- ...as a 3-cell table, for use with infoboxes.
local curr_num_2  = args["Num 2"] or 2
local result = {}
local min_num_2    = args["Min 2"] or 1
table.insert(result, '{| style="width: 100%;"')
local page_text    = args["Page Text"] or { "PRE-TEXT", "MID-TEXT", "POST-TEXT" }
table.insert(result, '|-')
local link_text    = args["Link Text"] or nil
table.insert(result, '| style="font-size: 0.75em;" | ' .. table.concat(prev_links, "&nbsp;"))
local is_ordinal_1 = args["Is Ordinal 1"] ~= nil and args["Is Ordinal 1"] or false
table.insert(result, '| style="width: 50%;" | ' .. current)
local is_ordinal_2 = args["Is Ordinal 1"] ~= nil and args["Is Ordinal 2"] or false
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
end


-- Generate a table of previous and next links for numbered pages, with support
-- Main function
-- for up to two numbers. Args are as follows:
function p.numlinks(frame)
-- - 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 the link config is for 8 links, then text is
--  formatted as [pre-text][n1][mid-text][n2][post-text], stored in a table
--  { mid, post } or { pre, mid, post }. Pre-text can be omitted since most
--  pages don't have pre-text (12edo, 5L 2s).
-- - 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(frame)
local args = getArgs(frame)
local args = getArgs(frame)
local wtext = yesno(frame.args["wtext"] or args["wtext"])
args["Num 1"] = tonumber(args["Num 1"])
-- Preprocess numeric input
args["Min 1"] = tonumber(args["Min 1"]) or 1
args["Current Num"] = tonumber(args["Current Num"]) -- Required arg
args["Num 2"] = tonumber(args["Num 2"]) or nil
args["Min"       ] = tonumber(args["Min"]) or 1
args["Min 2"] = tonumber(args["Min 2"]) or nil
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)
-- Create numbered navigation links
-- Debugger option to show Wikitext
local navigation_links = {}
if wtext then
if args["Num 2"] == nil then
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
navigation_links = p.side_numlinks(args)
else
navigation_links = p.eight_numlinks(args)
end
end


return navigation_links
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
end


return p
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