模組:MParchives/sandbox

维基百科,自由的百科全书
跳到导航 跳到搜索
require 'Module:No globals'

local p = {}

local function monthsyear(year, wplinks)
	local year_str = tostring(year)..'年'
	local ret = {}
	if year == tonumber(os.date('%Y')) then
		for month = 1, tonumber(os.date('%m'))+1 do
			-- TODO: 当前月份是12月时无法列出第二年的月份
			if month < 13 then
				local rootpage = wplinks..'/'..year_str..tostring(month)..'月'
				table.insert(ret, '[[' .. rootpage .. '|' .. tostring(month) .. '月]]')
			end
		end
	else
		for month = 1, 12 do
			local rootpage = wplinks..'/'..year_str..tostring(month)..'月'
			table.insert(ret, '[[' .. rootpage .. '|' .. tostring(month) .. '月]]')
		end	
	end
	return '* ' .. year_str .. ':'..table.concat(ret, '-')
end
function p.archives(frame)
	local args = require('Module:Arguments').getArgs(frame)
	local currentyear = tonumber(os.date('%Y'))
	local ret = {}
	for year = currentyear - 2, currentyear + 1 do
		table.insert(ret, monthsyear(year, args.page))
	end
	return table.concat(ret, '\n')
end
return p