Content deleted Content added
add an html_list function, and rename the randomize functions |
use a different function for each html list type |
||
Line 1:
-- This module contains a number of functions that make use of random numbers.
local p = {}
local makeList = require('Module:List').makeList
-- Set the seed for the random number to the current number of edits made to Wikipedia.
Line 13 ⟶ 10:
-- so please use it with caution.
math.randomseed(mw.site.stats.edits)
--------------------------------------------------------------------------------------
-- Random number function
--------------------------------------------------------------------------------------
function p._number(args)
-- Returns a random number.
first = tonumber(args[1])
second = tonumber(args[2])
-- This needs to use if statements as math.random won't accept explicit nil values as arguments.
if first then
if second and first <= second then -- Second number cannot be less than the first, or it causes an error.
return math.random(first, second)
else
return math.random(first)
end
else
return math.random()
end
end
--------------------------------------------------------------------------------------
-- List functions
--------------------------------------------------------------------------------------
function p.randomizeArray(t)
-- Randomizes an array. It works by iterating through the list backwards, each time swapping the entry
-- "i" with a random entry. Courtesy of Xinhuan at http://forums.wowace.com/showthread.php?p=279756
for i = #t, 2, -1 do
local r = math.random(i)
t[i], t[r] = t[r], t[i]
end
return t
end
local function removeBlanks(t)
Line 30 ⟶ 61:
local function makeSeparator(sep)
if sep ==
-- Include an easy way to use spaces as separators.
return ' '
Line 43 ⟶ 74:
return sep
end
end
local function makeRandomList(args)
local list = removeBlanks(args)
list = p.randomizeArray(list)
return list
end
Line 100 ⟶ 105:
end
--------------------------------------------------------------------------------------
-- HTML list function
--------------------------------------------------------------------------------------
function p.html_list(args, listType)
-- Randomizes a list and turns it into an HTML list. Uses [[Module:List]].
listType = listType or 'bulleted'
local listArgs = makeRandomList(args) -- Arguments for [[Module:List]].
for k, v in pairs(args) do
if type(k) == 'string' then
Line 113 ⟶ 121:
end
--------------------------------------------------------------------------------------
-- Process arguments from #invoke
--------------------------------------------------------------------------------------
local function makeWrapper(funcName, listType)
-- This function provides a wrapper for argument-processing from #invoke.
-- listType is only used with p.html_list, and is nil the rest of the time.
return function (frame)
-- If called via #invoke, use the args passed into the invoking template, or the args passed to #invoke if any exist.
Line 137 ⟶ 151:
end
end
return p[funcName](args, listType)
end
end
-- Process arguments for HTML list functions.
local htmlListFuncs = {
bulleted_list = 'bulleted',
unbulleted_list = 'unbulleted',
horizontal_list = 'horizontal',
ordered_list = 'ordered',
horizontal_ordered_list = 'horizontal_ordered'
}
for funcName, listType in pairs(htmlListFuncs) do
p[funcName] = makeWrapper('html_list', listType)
end
-- Process arguments for other functions.
local otherFuncs = {'number', 'item', 'list', 'text_list'}
for _, funcName in ipairs(otherFuncs) do
p[funcName] = makeWrapper('_' .. funcName)
end
|