Module:Numlinks: Difference between revisions

Ganaram inukshuk (talk | contribs)
mNo edit summary
Ganaram inukshuk (talk | contribs)
No edit summary
Line 10: Line 10:
--  dissimilar to keep within one module
--  dissimilar to keep within one module


-- Main function
function p._numlinks(args)
function p._numlinks(args)
local num_links  = args["Link Count"] or 1
local num_links  = args["Link Count"] or 1 -- Number of links to produce in each direction (left/prev and right/next)
local curr_num  = args["Num"] or 10
local curr_num  = args["Num"] -- Current number
local min_num    = args["Min"] or 1
local min_num    = args["Min"] or 1 -- Smallest allowed number
local page_text = args["Page Text"] or { "PRE-TEXT", "POST-TEXT" }
local link_text = args["Link Text"] or { "", "" } -- Text for a link is "<pre-text><num><post-text>"
local link_text = args["Link Text"] or nil
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_ordinal = args["Is Ordinal"] ~= nil and args["Is Ordinal"] or false
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)
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.
-- Preprocess page and link text
-- Preprocess link text, displayed text, and current-page text
if #page_text == 1 then
if #link_text == 1 then
page_text = { "", page_text[1] }
link_text = { "", link_text[1] }
end
end
if link_text ~= nil then
if disp_text ~= nil then
if #link_text == 1 then
if #disp_text == 1 then
link_text = { "", link_text[1] }
disp_text = { "", disp_text[1] }
end
end
end
end
if curr_text ~= nil then
-- Produce next links
if #curr_text == 1 then
local next_links = ""
curr_text = { "", curr_text[1] }
for i = 1, num_links do
local link = ""
local num = is_ordinal and ordinal(curr_num + i) or string.format("%s", curr_num + i)
if link_text == nil then
-- Link text is the same as the page text
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
end
next_links = next_links .. link .. (i == num_links and "" or "&nbsp;")
end
end
-- Produce prev links
-- Prev lines; start with left arrow, if applicable
local prev_links = ""
-- After each link produced, add a bullet as a separator
for i = 1, num_links do
local prev_lines = {}
local link = ""
if curr_num - num_links >= min_num then
table.insert(prev_lines, "&larr;")
end
for i = num_links, 1, -1 do
local num = is_ordinal and ordinal(curr_num - i) or string.format("%s", curr_num - i)
local num = is_ordinal and ordinal(curr_num - i) or string.format("%s", curr_num - i)
-- Only add links if they're not less than the minimum
if curr_num - i >= min_num then
if curr_num - i >= min_num then
if link_text == nil then
local link = (disp_text == nil
-- Link text is the same as the page text
and string.format("[[%s%s%s]]", link_text[1], num, link_text[2]) -- Link text is what's displayed
link = string.format("[[%s%s%s]]", page_text[1], num, page_text[2])
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
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])
table.insert(prev_lines, link)
end
table.insert(prev_lines, "&bull;")
prev_links = (curr_num - i <= min_num and "" or "&nbsp;") .. link .. prev_links
end
end
end
end
return { prev_links, next_links }
-- Produce next links; before each link, add a bullet as a separator
local next_lines = {}
for i = 1, num_links do
local num = is_ordinal and ordinal(curr_num + i) or string.format("%s", curr_num + i)
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
)
-- Insert to lines
table.insert(next_lines, "&bull;")
table.insert(next_lines, link)
end
 
-- Add right arrow
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
local lines = {}
table.insert(lines, '{| style="width: 100%;"')
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


Line 69: Line 108:
local curr_num_2  = args["Num 2"]
local curr_num_2  = args["Num 2"]
local min_num_2    = args["Min 2"] or 1
local min_num_2    = args["Min 2"] or 1
local page_text   = args["Page Text"] or { "PRE-TEXT", "MID-TEXT", "POST-TEXT" }
local disp_text   = args["Page Text"] or { "PRE-TEXT", "MID-TEXT", "POST-TEXT" }
local link_text    = args["Link Text"] or nil
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_1 = args["Is Ordinal 1"] ~= nil and args["Is Ordinal 1"] or false
Line 89: Line 128:


-- Main function (TODO: split functionality into two modules)
-- Main function (TODO: split functionality into two modules)
function p._numlinks(frame)
function p.numlinks(frame)
local args = getArgs(frame)
local args = getArgs(frame)
Line 105: Line 144:
local args = {
local args = {
["Link Count"] = 3,
["Link Count"] = 3,
["Num"]        = 13,
["Num"]        = 3,
["Link Text"]  = {"edo"}
["Link Text"]  = {"-octave temperaments"},
["Displayed Text"] = {""},
["Current Page Text"] = {"-octave"},
["Is Ordinal"] = 1,
["Is Table"  ] = 1
}
}