跳转到内容

模組:Loop/archive

维基百科,自由的百科全书

这是本页的一个历史版本,由SunAfterRain留言 | 贡献2025年2月22日 (六) 10:34编辑。这可能和当前版本存在着巨大的差异。

local p = {}
local mYesno
local mTemplateParameters
local mIfexist

local function titleExists(title, usePF)
	if not mIfexist then
		mIfexist = require('Module:Ifexist')
	end
	return mIfexist[usePF and '_parseFunctionExists' or '_luaExists'](title)
end

local function yesno(value)
	if not mYesno then
		mYesno = require('Module:Yesno')
	end
	return mYesno(value)
end

local function buildLink(titleText, display, checkExists, checkExistsPF)
	local success, title = pcall(mw.title.new, titleText)
	if not success or not title then
		return ''
	end
	if checkExists and not titleExists(title) then
		return string.format('<!--noexist:lua--><span style="color: var(--color-disabled, #a2a9b1)">%s</span>', display)
	end
	if checkExistsPF then
		if titleExists(title, true) then
			return string.format('[[%s|%s]]', title.prefixedText, display)
		else
			return string.format('<!--noexist:pf--><span style="color: var(--color-disabled, #a2a9b1)">%s</span>', display)
		end
	end
	return string.format('[[%s|%s]]', title.prefixedText, display)
end

local currentYear = tonumber(os.date('%Y'))
local currentMonth = tonumber(os.date('%m'))
local currentSeason = math.ceil(currentMonth / 3)

function p.byYear(frame)
	local args = frame.args
	local prefix = args.prefix -- 存檔母頁面
	local template = args.template -- 存檔頁面名模板 預設是套用 prefix/%04d年
	if not template then
		if prefix then
			template = prefix .. '/%04d年'
		elseif not template then
			error('One of prefix or template must be provided.')
		end
	end
	local startYear = tonumber(args.start) -- 循環開始年分
	local endYear = tonumber(args['end']) or currentYear -- 循環終止年分
	local range = tonumber(args.range) or 1 -- 循環分組
	local offset = tonumber(args.offset) or 0 -- 正常會用 2001~2005 2006~2010 此方法分組 此參數指定後對範圍進行偏移偏移
	local checkExists = args.checkExists and yesno(args.checkExists) or false -- 檢查存檔目標是否存在
	local checkExistsPF = not checkExists and (args.checkExistsPF and yesno(args.checkExistsPF) or true) -- 檢查存檔目標是否存在(解析器函式版)
	local outputTableBox = args.outputTableBox and yesno(args.outputTableBox) or false -- 是否輸出表格的框 {| |}
	local tableProps = outputTableBox and (args.tableProps ~= nil and args.tableProps or 'style="width:100%;"') -- 表格屬性組
	local disableCurrentBold = args.disableCurrentBold and yesno(args.disableCurrentBold) or false -- 是否阻止對當前年分加粗

	local output = {}
	if outputTableBox then
		local tableBox = '{|'
		if tableProps then
			tableBox = tableBox .. ' ' .. mw.text.trim(tableProps)
		end
		output[#output + 1] = tableBox
	end
	local index = math.ceil((startYear - offset) / range) - 1
	while index * range + offset < endYear do
		output[#output + 1] = '|-'
		for i = 1, range, 1 do
			local curYear = index * range + offset + i
			local link = (curYear >= startYear and curYear <= endYear)
				and buildLink(
					string.format(template, curYear),
					string.format(
						(curYear == currentYear and not disableCurrentBold)
							and '<span style="font-weight: bold;">%4d</span>'
							or '%4d',
						curYear
					),
					checkExists,
					checkExistsPF
				)
				or string.format('<!--%4d-->', curYear)
			output[#output + 1] = '|width=' .. tostring(90 / range) .. '%|' .. link
		end
		output[#output + 1] = '|width=10%|年'
		index = index + 1
	end
	if outputTableBox then
		output[#output + 1] = '|}'
	end
	return table.concat(output, '\n')
end

function p.bySeason(frame)
	local args = frame.args
	local prefix = args.prefix -- 存檔母頁面
	local template = args.template -- 存檔頁面名模板 預設是套用 prefix/%04d年/%d-%d月 # TODO 提供命名參數替換
	if not template then
		if prefix then
			template = prefix .. '/%04d年/%d-%d月'
		elseif not template then
			error('One of prefix or template must be provided.')
		end
	end
	local startYear = tonumber(args.startYear) -- 循環開始年分
	local startSeason = tonumber(args.startSeason) -- 循環開始季度
	local endYear = tonumber(args.endYear) or currentYear -- 循環終止年分
	local endSeason = tonumber(args.endSeason) or currentSeason -- 循環終止季度

	local checkExists = args.checkExists and yesno(args.checkExists) or false -- 檢查存檔目標是否存在
	local checkExistsPF = not checkExists and (args.checkExistsPF and yesno(args.checkExistsPF) or true) -- 檢查存檔目標是否存在(解析器函式版)
	local outputTableBox = args.outputTableBox and yesno(args.outputTableBox) or false -- 是否輸出表格的框 {| |}
	local tableProps = outputTableBox and (args.tableProps ~= nil and args.tableProps or 'style="width:100%;"') -- 表格屬性組
	local disableCurrentBold = args.disableCurrentBold and yesno(args.disableCurrentBold) or false -- 是否阻止對當前季度加粗

	local output = {}
	if outputTableBox then
		local tableBox = '{|'
		if tableProps then
			tableBox = tableBox .. ' ' .. mw.text.trim(tableProps)
		end
		output[#output + 1] = tableBox
	end
	for year = startYear, endYear, 1 do
		output[#output + 1] = '|-'
		output[#output + 1] = string.format('|width=27%%|<span style="font-weight: bold;">%4d年</span>', year)
		for season = 1, 4, 1 do
			local shouldPrintLink = not ((year == startYear and season < startSeason) or (year == endYear and season > endSeason))
			local startMonth = (season - 1) * 3 + 1
			local endMonth = season * 3
			local link = shouldPrintLink
				and buildLink(
					string.format(template, year, startMonth, endMonth),
					string.format(
						(curYear == currentYear and not disableCurrentBold)
							and '<span style="font-weight: bold;">%d-%d</span>'
							or '%d-%d',
						startMonth, endMonth
					),
					checkExists,
					checkExistsPF
				)
				or string.format('<!--%d-%d-->', startMonth, endMonth)
			output[#output + 1] = string.format('|width=%d%%|%s', season == 4 and 21 or 14, link)
		end
		output[#output + 1] = '|width=10%|月'
	end
	if outputTableBox then
		output[#output + 1] = '|}'
	end
	return table.concat(output, '\n')
end

return p