Module:Featured picture

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

-- Picks the Featured Picture for the Main Page.
-- Reads the list at Adaris:Main Page/Pictures (one picture per line:
-- "File:Name.jpg | Caption") and shows a different line each week.
local p = {}

local LIST = 'Adaris:Main Page/Pictures'

function p.main(frame)
	local title = mw.title.new(LIST)
	local text = title and title:getContent() or ''
	text = text:gsub('<!%-%-.-%-%->', '')
	local items = {}
	for line in mw.text.gsplit(text, '\n', true) do
		line = mw.text.trim(line):gsub('^%*%s*', '')
		local file, caption = line:match('^[Ff]ile:([^|]+)|%s*(.+)$')
		if not file then
			file = line:match('^[Ff]ile:([^|]+)$')
			caption = ''
		end
		if file then
			table.insert(items, { file = mw.text.trim(file), caption = mw.text.trim(caption or '') })
		end
	end
	if #items == 0 then
		return ''
	end
	local week = tonumber(frame:preprocess('{{CURRENTWEEK}}')) or 0
	local item = items[(week % #items) + 1]
	local out = '[[File:' .. item.file .. '|center|frameless|633x633px]]'
	if item.caption ~= '' then
		out = out .. '<div class="featured-caption">' .. item.caption .. '</div>'
	end
	return frame:preprocess(out)
end

return p