Module:Numlinks: Difference between revisions

Ganaram inukshuk (talk | contribs)
No edit summary
ArrowHead294 (talk | contribs)
m Wikitext debugger option
 
(18 intermediate revisions by one other user 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 getArgs = require("Module:Arguments").getArgs
local ordinal = require("Module:Ordinal"  )._ordinal
local ordinal = require("Module:Ordinal"  )._ordinal
Line 6: Line 6:
local p = {}
local p = {}


-- TODO:
-- TODO
-- - Split into two modules: Numlinks and Numlinks-2D, since behaviors are too
-- - (Low priority) Add a maximum allowed number. This isn't necessary in most
--   dissimilar to keep within one module
--   cases, so such a max would default to infinity.
-- - (Low priority) Add table style as a param?


-- 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)
-- Numbers. These are assumed to be the number datatype already (EG, already
local curr_num  = args["Num"] -- Current number
-- parsed from a string).
local min_num    = args["Min"] or 1 -- Smallest allowed number
-- - Current num is the, well, current number. EG, the 12 from 12edo.
local link_text  = args["Link Text"] or { "", "" } -- Text for a link is "<pre-text><num><post-text>"
-- - Num links is how many links to display left and right. EG, for 12edo,
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)")
--  with the default of 1, this shows links for 11edo and 13edo. Max number
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
--   of allowed links is 10.
local is_ordinal = yesno(args["Is Ordinal"], false) -- Option to convert numbers to ordinal (EG, 3rd-octave temperament)
-- - Min is the smallest allowed value. EG, with the default minimum of 1,
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.
--   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 link text, displayed text, and current-page text
-- Clamp numlinks.
if #link_text == 1 then
num_links = math.max(1, math.min(num_links, 10))
link_text = { "", link_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 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
-- Nested 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. Links are generated if the pages they link to
-- After each link produced, add a bullet as a separator
-- are greater than or equal to the minimum.
local prev_lines = {}
-- If the first link in the sequence is greater than the minimum, add an
if curr_num - num_links >= min_num then
-- arrow before the first link.
table.insert(prev_lines, "&larr;")
local prev_links = {}
if curr_num - num_links > min_num then
table.insert(prev_links, "&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
-- Add link and a bullet after it
if curr_num - i >= min_num then
table.insert(prev_links, make_link(format_number(n)))
local link = (disp_text == nil
table.insert(prev_links, "&bull;")
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. Add an arrow after the last link.
local next_lines = {}
local next_links = {}
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
local link = (disp_text == nil
-- Add link and a bullet before it
and string.format("[[%s%s%s]]", link_text[1], num, link_text[2]) -- Link text is what's displayed
table.insert(next_links, "&bull;")
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(next_links, make_link(format_number(n)))
)
-- Insert to lines
table.insert(next_lines, "&bull;")
table.insert(next_lines, link)
end
end
table.insert(next_links, "&rarr;")


-- Add right arrow
-- Current page text
table.insert(next_lines, "&rarr;")
local current = make_current(format_number(curr_num))
-- 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 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 is_table then
local lines = {}
if #prev_links ~= 0 then table.remove(prev_links) end
table.insert(lines, '{| style="width: 100%;"')
table.remove(next_links, 1)
table.insert(lines, '|-')
table.insert(lines, '| style="font-size: 0.75em;" | ' .. table.concat(prev_lines, "&nbsp;"))
table.insert(lines, '| style="width: 50%;" | ' .. curr_page_text)
table.insert(lines, '| style="font-size: 0.75em;" | ' .. table.concat(next_lines, "&nbsp;"))
table.insert(lines, '|}')
return table.concat(lines, "\n")
else
local lines = {}
table.insert(lines, table.concat(prev_lines, "&nbsp;"))
table.insert(lines, curr_page_text)
table.insert(lines, table.concat(next_lines, "&nbsp;"))
return table.concat(lines, "&nbsp;")
end
end
end


-- wip; to be placed in its own module
-- Output...
function p.numlinks_2num(args)
if is_table then
local curr_num_1  = args["Num 1"]
-- ...as a 3-cell table, for use with infoboxes.
local min_num_1    = args["Min 1"] or 1
local result = {}
local curr_num_2  = args["Num 2"]
table.insert(result, '{| style="width: 100%;"')
local min_num_2    = args["Min 2"] or 1
table.insert(result, '|-')
local disp_text    = args["Page Text"] or { "PRE-TEXT", "MID-TEXT", "POST-TEXT" }
table.insert(result, '| style="font-size: 0.75em;" | ' .. table.concat(prev_links, "&nbsp;"))
local link_text    = args["Link Text"] or nil
table.insert(result, '| style="width: 50%;" | ' .. current)
local is_ordinal_1 = args["Is Ordinal 1"] ~= nil and args["Is Ordinal 1"] or false
table.insert(result, '| style="font-size: 0.75em;" | ' .. table.concat(next_links, "&nbsp;"))
local is_ordinal_2 = args["Is Ordinal 1"] ~= nil and args["Is Ordinal 2"] or false
table.insert(result, '|}')
return table.concat(result, "\n")
local x = curr_num_1
else
local y = curr_num_2
-- ...as one long string, for use with navboxes with prev/next links.
local link_nums = {
local result = {}
{x-1, y-1}, {x  , y-1}, {x+1, y-1},
table.insert(result, table.concat(prev_links, "&nbsp;"))
{x-1, y  },            {x+1, y  },
table.insert(result, current)
{x-1, y+1}, {x  , y+1}, {x+1, y+1}
table.insert(result, table.concat(next_links, "&nbsp;"))
}
return table.concat(result, "&nbsp;")
local links = {}
for i = 1, #link_nums do
end
end
end
end


-- Main function (TODO: split functionality into two modules)
-- Main function
function p.numlinks(frame)
function p.numlinks(frame)
local args = getArgs(frame)
local args = getArgs(frame)
local wtext = yesno(frame.args["wtext"] or args["wtext"])
args["Num"] = tonumber(args["Num"]) or 123
-- Preprocess numeric input
args["Min"] = tonumber(args["Min"]) or 1
args["Current Num"] = tonumber(args["Current Num"]) -- Required arg
args["Link Count"] = tonumber(args["Link Count"]) or 1
args["Min"       ] = tonumber(args["Min"]) or 1
args["Link Count" ] = tonumber(args["Link Count"]) or 1
local link_pretext  = args["Link Pretext" ] or ""
-- Preprocess toggles
local link_posttext = args["Link Posttext"] or ""
args["Is Ordinal"] = args["Is Ordinal"] or false
args["Link Text"] = { link_pretext, link_posttext }
args["Is Table" ] = args["Is Table" ] or true
args["Link Pretext" ] = nil
args["Link Posttext"] = nil
local disp_pretext  = args["Displayed Pretext" ]
-- Create numbered navigation links
local disp_posttext = args["Displayed Posttext"]
local result = p._numlinks(args)
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" ]
-- Debugger option to show Wikitext
local curr_posttext = args["Displayed Posttext"]
if wtext then
if curr_pretext ~= nil and curr_posttext ~= nil then
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
args["Current Page Text"] = { curr_pretext, curr_posttext }
args["Current Page Pretext" ] = nil
args["Current Page Posttext"] = nil
end
end
-- Create numbered navigation links
local result = p._numlinks(args)


return result
return frame:preprocess(result)
end
end


Line 167: Line 165:
["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
}
}