Module:Potd

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

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

Code

require('strict')

local p = {}

local months = {}

local function getMonths(lang)
	local frame = mw.getCurrentFrame()
	local langcode = lang:getCode()
	for i = 1, 12 do
		months[i] = frame:expandTemplate{ title = 'i18n month', args = { tostring(i), langcode } }
	end
end

local function formatMonth(year, month)
	if month < 10 then
		return string.format('%d-0%d', year, month)
	else
		return string.format('%d-%d', year, month)
	end
end

local function monthsyear(year, lang, commonslinks)
	local year_str = mw.getCurrentFrame():expandTemplate{ title = 'Potd/Years', args = { tostring(year), lang:getCode() } }
	year_str = string.format("'''%s%s'''", year_str, tostring(mw.message.new('colon'):inLanguage(lang)))
	local ret = {'\t<li class="hlist">\n\t\t' .. year_str .. '\n\t\t<ul style="display:inline;">'}
	for month = 1, 12 do
		if year == 2004 and month < 11 then
			table.insert(ret, '\t\t\t<li style="color:#aaa;">' .. months[month] .. '</li>')
		else
			local page = string.format('Commons:Potd/%s (%s)', formatMonth(year, month), lang:getCode())
			if not commonslinks or not mw.title.new(page).exists then
				page = string.format('Template:Potd/%s', formatMonth(year, month))
			end
			table.insert(ret, '\t\t\t<li>[[' .. page .. '|' .. months[month] .. ']]</li>')
		end
	end
	return table.concat(ret, '\n') .. '\n\t\t</ul>\n\t</li>'
end

function p.months(frame)
	local args = require('Module:Arguments').getArgs(frame)
	local commonslinks = true
	if args.commonslinks then
		commonslinks = require('Module:Yesno')(args.commonslinks)
	end
	local lang
	if args.lang and mw.language.isKnownLanguageTag(args.lang) then
		lang = mw.language.new(args.lang)
	else
		lang = mw.language.getContentLanguage()
	end
	getMonths(lang)
	local ret = {'<div class="plainlist"><ul>'}
	local currentyear = tonumber(os.date('%Y'))
	for year = 2004, currentyear + 2 do
		table.insert(ret, monthsyear(year, lang, commonslinks))
	end
	return table.concat(ret, '\n') .. '\n</ul></div>'
end

return p