Module:Numlinks: Difference between revisions

Ganaram inukshuk (talk | contribs)
refactored
Ganaram inukshuk (talk | contribs)
No edit summary
Line 17: Line 17:
local link_fmt    = args["Link Format"] or "%s" -- Format for link target (EG, %sedo)
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 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
local current_fmt = args["Current Page Format"] or link_fmt -- Format for current page text, if it differs from the link text


-- Helper function to format a number
-- Helper function to format a number
Line 27: Line 27:
-- Nested helper function to make a link
-- Nested helper function to make a link
local function make_link(num)
local function make_link(num)
local link_target = string.format(link_fmt, num)
local link_target = string.format(link_fmt   , num)
local display_text = string.format(display_fmt, num)
local display_text = string.format(display_fmt, num)


Line 43: Line 43:
end
end


-- Generate previous links
-- Generate previous links, if they are greater or equal to the minimum.
local prev_parts = {}
-- If the first link in the sequence is greater than the minimum, add an
-- arrow to the beginning.
local prev_links = {}
if curr_num - num_links >= min_num then
if curr_num - num_links >= min_num then
table.insert(prev_parts, "←")
table.insert(prev_links, "←")
end
end
for i = num_links, 1, -1 do
for i = num_links, 1, -1 do
local n = curr_num - i
local n = curr_num - i
if n >= min_num then
if n >= min_num then
table.insert(prev_parts, make_link(format_number(n)))
table.insert(prev_links, make_link(format_number(n)))
table.insert(prev_parts, "•")
-- If output is a table, don't add a bullet after the last link
if n - 1 ~= curr_num and is_table then
table.insert(prev_links, "•")
end
end
end
end
end


-- Generate next links
-- Generate next links. Add an arrow after the last link.
local next_parts = {}
local next_links = {}
for i = 1, num_links do
for i = 1, num_links do
local n = curr_num + i
local n = curr_num + i
table.insert(next_parts, "•")
table.insert(next_parts, make_link(format_number(n)))
-- If output is a table, don't add a bullet before the first link
if n + 1 ~= curr_num and is_table then
table.insert(next_links, "•")
end
table.insert(next_links, make_link(format_number(n)))
end
end
table.insert(next_parts, "→")
table.insert(next_links, "→")


-- Current page text
-- Current page text
Line 73: Line 84:
table.insert(out, '{| style="width: 100%;"')
table.insert(out, '{| style="width: 100%;"')
table.insert(out, '|-')
table.insert(out, '|-')
table.insert(out, '| style="font-size: 0.75em;" | ' .. table.concat(prev_parts, " "))
table.insert(out, '| style="font-size: 0.75em;" | ' .. table.concat(prev_links, " "))
table.insert(out, '| style="width: 50%;" | ' .. current)
table.insert(out, '| style="width: 50%;" | ' .. current)
table.insert(out, '| style="font-size: 0.75em;" | ' .. table.concat(next_parts, " "))
table.insert(out, '| style="font-size: 0.75em;" | ' .. table.concat(next_links, " "))
table.insert(out, '|}')
table.insert(out, '|}')
return table.concat(out, "\n")
return table.concat(out, "\n")
Line 82: Line 93:
table.insert(out, table.concat(prev_parts, " "))
table.insert(out, table.concat(prev_parts, " "))
table.insert(out, current)
table.insert(out, current)
table.insert(out, table.concat(next_parts, " "))
table.insert(out, table.concat(next_links, " "))
return table.concat(out, " ")
return table.concat(out, " ")
end
end