Module:Numlinks: Difference between revisions
No edit summary |
ArrowHead294 (talk | contribs) m Wikitext debugger option |
||
| (33 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
-- | -- 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 | 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. | -- Main function | ||
function p._numlinks(args) | |||
-- Numbers. These are assumed to be the number datatype already (EG, already | |||
-- parsed from a string). | |||
local | -- - Current num is the, well, current number. EG, the 12 from 12edo. | ||
local | -- - Num links is how many links to display left and right. EG, for 12edo, | ||
local | -- 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. | ||
if | 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 | end | ||
if | -- 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 | ||
end | end | ||
-- | -- Nested helper function to make current page text. | ||
local | local function make_current(num) | ||
local display_text = string.format(current_fmt, num) | |||
return string.format("%s", display_text) | |||
local | 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, "←") | |||
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 | ||
end | end | ||
-- | -- Generate next links. Add an arrow after the last link. | ||
local | local next_links = {} | ||
for i = 1, num_links do | for i = 1, num_links do | ||
local | local n = curr_num + i | ||
-- Add link and a bullet before it | |||
table.insert(next_links, "•") | |||
table.insert(next_links, make_link(format_number(n))) | |||
end | end | ||
table.insert(next_links, "→") | |||
-- 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, " ")) | |||
table.insert(result, '| style="width: 50%;" | ' .. current) | |||
table.insert(result, '| style="font-size: 0.75em;" | ' .. table.concat(next_links, " ")) | |||
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, " ")) | |||
table.insert(result, current) | |||
table.insert(result, table.concat(next_links, " ")) | |||
return table.concat(result, " ") | |||
end | |||
end | end | ||
function p. | -- 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 | -- Create numbered navigation links | ||
local | local result = p._numlinks(args) | ||
if | |||
-- Debugger option to show Wikitext | |||
if wtext then | |||
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>" | |||
end | end | ||
return | 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 | ||