Documentation for this module may be created at Module:DailyRandom/doc

-- Module:DailyRandom
local p = {}

local function mergeArgs(frame)
	local args = {}
	local parent = frame:getParent()
	if parent then
		for k,v in pairs(parent.args) do args[k] = v end
	end
	for k,v in pairs(frame.args) do args[k] = v end
	return args
end

local function daySeed(frame, tz, salt)
	tz = tz or 'Asia/Singapore'
	local ymd = frame:preprocess(string.format('{{#time:Ymd|now|%s}}', tz))
	return (salt or '') .. ymd
end

function p.list(frame)
	local args   = mergeArgs(frame)
	local count  = tonumber(args.count) or 6
	local tz     = args.tz or 'Asia/Singapore'
	local seed   = args.seed or daySeed(frame, tz, args.salt)

	-- Gather items from numbered params 1..N
	local items = {}
	local i = 1
	while true do
		local v = args[i]
		if not v then break end
		if v ~= '' then
			table.insert(items, { text = v, hash = mw.hash.hashValue('md5', seed .. '|' .. v .. '|' .. i) })
		end
		i = i + 1
	end

	-- Also allow a newline list via |list=
	if #items == 0 and args.list then
		for line in tostring(args.list):gmatch('[^\r\n]+') do
			local v = mw.text.trim(line)
			if v ~= '' then
				table.insert(items, { text = v, hash = mw.hash.hashValue('md5', seed .. '|' .. v) })
			end
		end
	end

	if #items == 0 then return '' end
	if count > #items then count = #items end

	table.sort(items, function(a, b) return a.hash < b.hash end)

	local lis = {}
	for idx = 1, count do
		lis[#lis+1] = '<li>' .. items[idx].text .. '</li>'
	end

	local attrs = {}
	if args.class then attrs.class = args.class end
	return mw.text.tag('ul', attrs, table.concat(lis))
end

return p