Module:Numlinks: Difference between revisions

Ganaram inukshuk (talk | contribs)
No edit summary
ArrowHead294 (talk | contribs)
m Wikitext debugger option
 
(33 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 = {}


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?


function p.side_numlinks(num_links, curr_num, min_num, page_text, link_text, is_ordinal)
-- Main function
local num_links = num_links or 1
function p._numlinks(args)
local curr_num = curr_num
-- Numbers. These are assumed to be the number datatype already (EG, already
local min_1 = min_1 or 1
-- parsed from a string).
local page_text = page_text or { "PRE-TEXT", "POST-TEXT" }
-- - Current num is the, well, current number. EG, the 12 from 12edo.
local link_text = link_text or nil
-- - Num links is how many links to display left and right. EG, for 12edo,
local is_ordinal = is_ordinal ~= nil and is_ordinal or false
--  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
-- Preprocess page and link text
-- Clamp numlinks.
if #page_text == 1 then
num_links = math.max(1, math.min(num_links, 10))
page_text = { "", page_text[1] }
-- 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
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
-- 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
end


function p.eight_numlinks(curr_num, min_num, page_text, link_text)
-- Main function
function p.numlinks(frame)
local args = getArgs(frame)
local wtext = yesno(frame.args["wtext"] or args["wtext"])
end
-- Preprocess numeric input
 
args["Current Num"] = tonumber(args["Current Num"]) -- Required arg
-- Generate a table of previous and next links for numbered pages, with support
args["Min"       ] = tonumber(args["Min"]) or 1
-- for up to two numbers. Args are as follows:
args["Link Count" ] = tonumber(args["Link Count"]) or 1
-- - Link config: what is the arrangement of links? Options are:
-- -- Side-1: For n, links are for n-1 and n+1
-- -- Side-2: For n, links are for n-2 to n+1
-- -- Side-3: For n, links are for n-3 to n+3
-- -- Side-4: For n, links are for n-4 to n+4
-- -- Side-5: For n, links are for n-5 to n+5
-- -- 8-Link: For n and m, links are n +/- 1 and m +/- 1. There are no options
--    for additional links.
-- - 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][n][mid-text][m][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(args)
local link_config = args["Link Config"]
local is_ordinal  = args["Is Ordinal"] ~= nil and args["Is Ordinal"] or false
local page_text  = args["Page Text"]
local link_text  = args["Link Text"]
local min_num    = args["Min Num"] ~= nil and args["Min Num"] or 1
local curr_num_1  = args["Current Num 1"]
local curr_num_2  = args["Current Num 2"]
-- Parse link config
-- Preprocess toggles
local num_links = 0
args["Is Ordinal"] = args["Is Ordinal"] or false
local is_side = false
args["Is Table" ] = args["Is Table" ] or true
local is_eight = false
if link_config == "Side-1" then
-- One set of prev/next links
local num_links = 1
local is_side = true
elseif link_config == "Side-2" then
-- Two sets of prev/next links
local num_links = 2
local is_side = true
elseif link_config == "Side-3" then
-- Three sets of prev/next links
local num_links = 3
local is_side = true
elseif link_config == "Side-4" then
-- Four sets of prev/next links
local num_links = 4
local is_side = true
elseif link_config == "Side-5" then
-- Five sets of prev/next links
local num_links = 5
local is_side = true
elseif link_config == "8-Link" then
-- Prev/next links for two changing values
local is_eight = true
else
-- One set of prev/next links as the default
local num_links = 1
local is_side = true
end
-- Create numbered navigation links
-- Create numbered navigation links
local navigation_links = {}
local result = p._numlinks(args)
if is_side then
navigation_links = p.side_numlinks(num_links, curr_num, min_num, page_text, link_text, is_ordinal)
-- Debugger option to show Wikitext
if wtext then
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
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