Module:RandomTableRow
More actions
Documentation for this module may be created at Module:RandomTableRow/doc
local p = {}
--Makes a seed based on the date so that it always returns the same first random number in sequence which changes every day
local function getDailyRandomNumber(maxNum)
local seed = os.date("%Y%m%d")
math.randomseed(tonumber(seed))
return math.random(1, maxNum)
end
--Gets page content (kinda self-explanatory imo)
local function getPageContent(page)
local title = mw.title.new(page)
if not title then return nil end
return title:getContent()
end
--Parses the page which contains the table so it only grabs the tip text
local function parseTableContent(content)
local rows = {}
local inRow = false
for line in content:gmatch("[^\r\n]+") do
line = mw.text.trim(line)
if line == "|-" then
inRow = true
elseif inRow then
local tip = line:match("^|%s*(.+)%s*$")
if tip then
table.insert(rows, tip)
inRow = false
end
end
end
return rows
end
--Main function to call the local functions and return errors if needs be
function p.main(frame)
local rows = {}
local page = frame.args[1]
local content = getPageContent(page)
if not content then return 'Page not found' end
local rows = parseTableContent(content)
if(#rows == 0) then return 'No rows found' end
local index = getDailyRandomNumber(#rows)
return rows[index]
end
return p