模組:MParchives

被永久保护的模块
维基百科,自由的百科全书

require 'Module:No globals'

local p = {}
local extent_month = 6

local function monthsyear(year, wplinks, last_year)
	local year_str = tostring(year)..'年'
	local ret = {}
	if year == last_year then
		local current_year = tonumber(os.date('%Y'))
		local end_month = last_year == current_year and math.min(12, tonumber(os.date('%m')) + extent_month) or extent_month
		for month = 1, end_month do
			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 current_year = tonumber(os.date('%Y'))
	local current_month = tonumber(os.date('%m'))
	local ret = {}
	-- 當前月份 >= 7月時 列出第二年的月份
	local end_year = current_month > 12 - extent_month and current_year + 1 or current_year
	for year = current_year - 2, end_year do
		table.insert(ret, monthsyear(year, args.page, end_year))
	end
	return table.concat(ret, '\n')
end
return p