Module:Numlinks: Difference between revisions

Ganaram inukshuk (talk | contribs)
No edit summary
ArrowHead294 (talk | contribs)
m Wikitext debugger option
 
(11 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 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["Displayed Format"   ] or link_fmt
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("'''%s'''", display_text)
return string.format("%s", display_text)
end
end


-- Generate previous links, if they are greater or equal to the minimum.
-- 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 >= min_num then
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 out = {}
-- ...as a 3-cell table, for use with infoboxes.
table.insert(out, '{| style="width: 100%;"')
local result = {}
table.insert(out, '|-')
table.insert(result, '{| style="width: 100%;"')
table.insert(out, '| style="font-size: 0.75em;" | ' .. table.concat(prev_links, " "))
table.insert(result, '|-')
table.insert(out, '| style="width: 50%;" | ' .. current)
table.insert(result, '| style="font-size: 0.75em;" | ' .. table.concat(prev_links, " "))
table.insert(out, '| style="font-size: 0.75em;" | ' .. table.concat(next_links, " "))
table.insert(result, '| style="width: 50%;" | ' .. current)
table.insert(out, '|}')
table.insert(result, '| style="font-size: 0.75em;" | ' .. table.concat(next_links, " "))
return table.concat(out, "\n")
table.insert(result, '|}')
return table.concat(result, "\n")
else
else
local out = {}
-- ...as one long string, for use with navboxes with prev/next links.
table.insert(out, table.concat(prev_links, " "))
local result = {}
table.insert(out, current)
table.insert(result, table.concat(prev_links, " "))
table.insert(out, table.concat(next_links, " "))
table.insert(result, current)
return table.concat(out, " ")
table.insert(result, table.concat(next_links, " "))
return table.concat(result, " ")
end
end
end
end


-- wip; to be placed in its own module
-- Main function
function p.numlinks_2num(args)
local curr_num_1  = args["Num 1"]
local min_num_1    = args["Min 1"] or 1
local curr_num_2  = args["Num 2"]
local min_num_2    = args["Min 2"] or 1
local disp_text    = args["Page Text"] or { "PRE-TEXT", "MID-TEXT", "POST-TEXT" }
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_2 = args["Is Ordinal 1"] ~= nil and args["Is Ordinal 2"] or false
local x = curr_num_1
local y = curr_num_2
local link_nums = {
{x-1, y-1}, {x  , y-1}, {x+1, y-1},
{x-1, y  },            {x+1, y  },
{x-1, y+1}, {x  , y+1}, {x+1, y+1}
}
local links = {}
for i = 1, #link_nums do
end
end
 
-- Main function (TODO: split functionality into two modules)
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"]) or 12
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"] = yesno(args["Is Ordinal"], false)
args["Is Ordinal"] = args["Is Ordinal"] or false
args["Is Table"  ] = yesno(args["Is Table"  ], true )
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