Module:Numlinks: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Ganaram inukshuk (talk | contribs)
todo
Ganaram inukshuk (talk | contribs)
refactored
Line 5: Line 5:


local p = {}
local p = {}
-- TODO:
-- - Refactor to use format strings instead of pretext-posttext


-- Main function
-- Main function
function p._numlinks(args)
function p._numlinks(args)
local num_links = args["Link Count"] or 1 -- Number of links to produce in each direction (left/prev and right/next)
local num_links   = tonumber(args["Link Count"]) or 1 -- Number of links to display in each direction (EG, for 12edo, with default of 1, show links for 11edo and 13edo)
local curr_num   = args["Num"] -- Current number
local curr_num   = tonumber(args["Num"]) -- Current number (EG, 12edo)
local min_num   = args["Min"] or 1 -- Smallest valid number
local min_num     = tonumber(args["Min"]) or 1 -- Minimum valid number (EG, for 12edo, this corresponds to 1edo)
local link_text = args["Link Text"] or { "", "" } -- Text for a link is "<pre-text><num><post-text>"
local is_ordinal = yesno(args["Is Ordinal"], false) -- Whether to format numbers as ordinals (EG, for nth-octave temperament pages)
local disp_text  = args["Displayed Text"] or nil -- Option for if the text to display is different from the link (EG, "4L 5s <3/1>"" vs "4L 5s (3/1-equivalent)")
local is_table    = yesno(args["Is Table" ], true) -- Toggle whether to display as a 3-cell table or one line
local curr_text  = args["Current Page Text"] or nil -- Option for if the text to display for the current page is different from the link text; by default, this is the same as the link text
 
local is_ordinal = yesno(args["Is Ordinal"], false) -- Option to convert numbers to ordinal (EG, 3rd-octave temperament)
-- Format strings
local is_table  = yesno(args["Is Table" ], true ) -- Output is displayed in a 1-row 3-col table by default; setting this to false formats it as one line.
local link_fmt    = args["Link Format"] or "%s" -- Format for link target (EG, %sedo)
local display_fmt = args["Displayed Format"   ] or link_fmt -- Format for displayed text in links, if it differs from the link text
-- Preprocess link text, displayed text, and current-page text
local current_fmt = args["Current Page Format"] or display_fmt -- Format for current page text, if it differs from the link text
if #link_text == 1 then
 
link_text = { "", link_text[1] }
-- 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 disp_text ~= nil then
 
if #disp_text == 1 then
-- Nested helper function to make a link
disp_text = { "", disp_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
if curr_text ~= nil then
 
if #curr_text == 1 then
-- Helper function to make current page text
curr_text = { "", curr_text[1] }
local function make_current(num)
end
local display_text = string.format(current_fmt, num)
return string.format("'''%s'''", display_text)
end
end
 
-- Prev lines; start with left arrow, if applicable
-- Generate previous links
-- After each link produced, add a bullet as a separator
local prev_parts = {}
local prev_lines = {}
if curr_num - num_links >= min_num then
if curr_num - num_links >= min_num then
table.insert(prev_lines, "&larr;")
table.insert(prev_parts, "&larr;")
end
end
for i = num_links, 1, -1 do
for i = num_links, 1, -1 do
local num = is_ordinal and ordinal(curr_num - i) or string.format("%s", curr_num - i)
local n = curr_num - i
if n >= min_num then
-- Only add links if they're not less than the minimum
table.insert(prev_parts, make_link(format_number(n)))
if curr_num - i >= min_num then
table.insert(prev_parts, "&bull;")
local link = (disp_text == nil
and string.format("[[%s%s%s]]", link_text[1], num, link_text[2]) -- Link text is what's displayed
or  string.format("[[%s%s%s|%s%s%s]]", link_text[1], num, link_text[2], disp_text[1], num, disp_text[2]) -- Displayed text is different from that of the link
)
table.insert(prev_lines, link)
table.insert(prev_lines, "&bull;")
end
end
end
end
 
-- Produce next links; before each link, add a bullet as a separator
-- Generate next links
local next_lines = {}
local next_parts = {}
for i = 1, num_links do
for i = 1, num_links do
local num = is_ordinal and ordinal(curr_num + i) or string.format("%s", curr_num + i)
local n = curr_num + i
table.insert(next_parts, "&bull;")
local link = (disp_text == nil
table.insert(next_parts, make_link(format_number(n)))
and string.format("[[%s%s%s]]", link_text[1], num, link_text[2]) -- Link text is what's displayed
or  string.format("[[%s%s%s|%s%s%s]]", link_text[1], num, link_text[2], disp_text[1], num, disp_text[2]) -- Displayed text is different from that of the link
)
-- Insert to lines
table.insert(next_lines, "&bull;")
table.insert(next_lines, link)
end
end
table.insert(next_parts, "&rarr;")
-- Current page text
local current = make_current(format_number(curr_num))


-- Add right arrow
-- Output
table.insert(next_lines, "&rarr;")
-- Produce current page text
local curr_num_text = is_ordinal and ordinal(curr_num) or string.format("%s", curr_num)
local curr_page_text = (disp_text == nil
and string.format("'''%s%s%s'''", link_text[1], curr_num_text, link_text[2]) -- Current-page text is the same as that of its links
or  string.format("'''%s%s%s'''", curr_text[1], curr_num_text, curr_text[2]) -- Displayed text is different from that of the link
)
if is_table then
if is_table then
local lines = {}
local out = {}
table.insert(lines, '{| style="width: 100%;"')
table.insert(out, '{| style="width: 100%;"')
table.insert(lines, '|-')
table.insert(out, '|-')
table.insert(lines, '| style="font-size: 0.75em;" | ' .. table.concat(prev_lines, "&nbsp;"))
table.insert(out, '| style="font-size: 0.75em;" | ' .. table.concat(prev_parts, "&nbsp;"))
table.insert(lines, '| style="width: 50%;" | ' .. curr_page_text)
table.insert(out, '| style="width: 50%;" | ' .. current)
table.insert(lines, '| style="font-size: 0.75em;" | ' .. table.concat(next_lines, "&nbsp;"))
table.insert(out, '| style="font-size: 0.75em;" | ' .. table.concat(next_parts, "&nbsp;"))
table.insert(lines, '|}')
table.insert(out, '|}')
return table.concat(out, "\n")
return table.concat(lines, "\n")
else
else
local lines = {}
local out = {}
table.insert(lines, table.concat(prev_lines, "&nbsp;"))
table.insert(out, table.concat(prev_parts, "&nbsp;"))
table.insert(lines, curr_page_text)
table.insert(out, current)
table.insert(lines, table.concat(next_lines, "&nbsp;"))
table.insert(out, table.concat(next_parts, "&nbsp;"))
return table.concat(out, "&nbsp;")
return table.concat(lines, "&nbsp;")
end
end
end
end
Line 133: Line 119:
args["Min"] = tonumber(args["Min"]) or 1
args["Min"] = tonumber(args["Min"]) or 1
args["Link Count"] = tonumber(args["Link Count"]) or 1
args["Link Count"] = tonumber(args["Link Count"]) or 1
local link_pretext  = args["Link Pretext" ] or ""
local link_posttext = args["Link Posttext"] or ""
args["Link Text"] = { link_pretext, link_posttext }
args["Link Pretext" ] = nil
args["Link Posttext"] = nil
local disp_pretext  = args["Displayed Pretext" ]
local disp_posttext = args["Displayed Posttext"]
if disp_pretext ~= nil and disp_posttext ~= nil then
args["Displayed Text"] = { disp_pretext, disp_posttext }
args["Displayed Pretext" ] = nil
args["Displayed Posttext"] = nil
end
local curr_pretext  = args["Displayed Pretext" ]
local curr_posttext = args["Displayed Posttext"]
if curr_pretext ~= nil and curr_posttext ~= nil then
args["Current Page Text"] = { curr_pretext, curr_posttext }
args["Current Page Pretext" ] = nil
args["Current Page Posttext"] = nil
end
-- Create numbered navigation links
-- Create numbered navigation links
Line 166: Line 130:
["Link Count"] = 3,
["Link Count"] = 3,
["Num"]        = 3,
["Num"]        = 3,
["Link Text"]  = {"-octave temperaments"},
["Link Format"]  = "%s-octave temperaments",
["Displayed Text"] = {""},
["Current Page Format"] = "%s-octave",
["Current Page Text"] = {"-octave"},
["Is Ordinal"] = 1,
["Is Ordinal"] = 1,
["Is Table"  ] = 1
["Is Table"  ] = 0
}
}

Revision as of 10:12, 9 October 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 (4)
Line Function Params
9 _numlinks (main) (args)
90 numlinks_2num (args)
115 numlinks (invokable) (frame)
128 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.


-- 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 = {}

-- Main function
function p._numlinks(args)
	local num_links   = tonumber(args["Link Count"]) or 1		-- Number of links to display in each direction (EG, for 12edo, with default of 1, show links for 11edo and 13edo)
	local curr_num    = tonumber(args["Num"])					-- Current number (EG, 12edo)
	local min_num     = tonumber(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
	local link_fmt    = args["Link Format"] or "%s"					-- Format for link target (EG, %sedo)
	local display_fmt = args["Displayed Format"   ] or link_fmt		-- Format for displayed text in links, if it differs from the link text
	local current_fmt = args["Current Page Format"] or display_fmt	-- Format for current page text, if it differs from the link text

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

	-- 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
	local prev_parts = {}
	if curr_num - num_links >= min_num then
		table.insert(prev_parts, "&larr;")
	end
	for i = num_links, 1, -1 do
		local n = curr_num - i
		if n >= min_num then
			table.insert(prev_parts, make_link(format_number(n)))
			table.insert(prev_parts, "&bull;")
		end
	end

	-- Generate next links
	local next_parts = {}
	for i = 1, num_links do
		local n = curr_num + i
		table.insert(next_parts, "&bull;")
		table.insert(next_parts, make_link(format_number(n)))
	end
	table.insert(next_parts, "&rarr;")

	-- Current page text
	local current = make_current(format_number(curr_num))

	-- 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_parts, "&nbsp;"))
		table.insert(out, '| style="width: 50%;" | ' .. current)
		table.insert(out, '| style="font-size: 0.75em;" | ' .. table.concat(next_parts, "&nbsp;"))
		table.insert(out, '|}')
		return table.concat(out, "\n")
	else
		local out = {}
		table.insert(out, table.concat(prev_parts, "&nbsp;"))
		table.insert(out, current)
		table.insert(out, table.concat(next_parts, "&nbsp;"))
		return table.concat(out, "&nbsp;")
	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