Module:Numlinks: Difference between revisions
No edit summary |
ArrowHead294 (talk | contribs) m Wikitext debugger option |
||
| (11 intermediate revisions by one other user not shown) | |||
| Line 1: | Line 1: | ||
-- | -- 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 9: | Line 9: | ||
-- - (Low priority) Add a maximum allowed number. This isn't necessary in most | -- - (Low priority) Add a maximum allowed number. This isn't necessary in most | ||
-- cases, so such a max would default to infinity. | -- cases, so such a max would default to infinity. | ||
-- - (Low priority) Add table style as a param? | |||
-- Main function | -- Main function | ||
| Line 37: | Line 38: | ||
-- to displaying only the number. | -- to displaying only the number. | ||
local link_fmt = args["Link Format" ] or "%s" | local link_fmt = args["Link Format" ] or "%s" | ||
local display_fmt = args[" | local display_fmt = args["Display Format" ] or link_fmt | ||
local current_fmt = args["Current Page Format"] or display_fmt | local current_fmt = args["Current Page Format"] or display_fmt | ||
| Line 71: | Line 72: | ||
local function make_current(num) | local function make_current(num) | ||
local display_text = string.format(current_fmt, num) | local display_text = string.format(current_fmt, num) | ||
return string.format(" | return string.format("%s", display_text) | ||
end | end | ||
-- Generate previous links | -- 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 | -- If the first link in the sequence is greater than the minimum, add an | ||
-- arrow before the first link. | -- arrow before the first link. | ||
local prev_links = {} | local prev_links = {} | ||
if curr_num - num_links > | if curr_num - num_links > min_num then | ||
table.insert(prev_links, "←") | table.insert(prev_links, "←") | ||
end | end | ||
| Line 113: | Line 115: | ||
end | end | ||
-- Output | -- Output... | ||
if is_table then | if is_table then | ||
local | -- ...as a 3-cell table, for use with infoboxes. | ||
table.insert( | local result = {} | ||
table.insert( | table.insert(result, '{| style="width: 100%;"') | ||
table.insert( | table.insert(result, '|-') | ||
table.insert( | table.insert(result, '| style="font-size: 0.75em;" | ' .. table.concat(prev_links, " ")) | ||
table.insert( | table.insert(result, '| style="width: 50%;" | ' .. current) | ||
table.insert( | table.insert(result, '| style="font-size: 0.75em;" | ' .. table.concat(next_links, " ")) | ||
return table.concat( | table.insert(result, '|}') | ||
return table.concat(result, "\n") | |||
else | else | ||
local | -- ...as one long string, for use with navboxes with prev/next links. | ||
table.insert( | local result = {} | ||
table.insert( | table.insert(result, table.concat(prev_links, " ")) | ||
table.insert( | table.insert(result, current) | ||
return table.concat( | table.insert(result, table.concat(next_links, " ")) | ||
return table.concat(result, " ") | |||
end | end | ||
end | end | ||
-- Main function | |||
-- 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"]) | |||
-- Preprocess numeric input | -- Preprocess numeric input | ||
args["Current Num"] = tonumber(args["Current Num"]) | args["Current Num"] = tonumber(args["Current Num"]) -- Required arg | ||
args["Min"] = tonumber(args["Min"]) or 1 | args["Min" ] = tonumber(args["Min"]) or 1 | ||
args["Link Count"] = tonumber(args["Link Count"]) or 1 | args["Link Count" ] = tonumber(args["Link Count"]) or 1 | ||
-- Preprocess toggles | -- Preprocess toggles | ||
args["Is Ordinal"] = | args["Is Ordinal"] = args["Is Ordinal"] or false | ||
args["Is Table" ] = | args["Is Table" ] = args["Is Table" ] or true | ||
-- Create numbered navigation links | -- Create numbered navigation links | ||
local result = p._numlinks(args) | local result = p._numlinks(args) | ||
-- Debugger option to show Wikitext | |||
if wtext then | |||
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>" | |||
end | |||
return result | return frame:preprocess(result) | ||
end | end | ||