跳转到内容

模組:Loop/archive

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

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

local p = {}
local mYesno
local mTemplateParameters

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

local function buildLink(titleText, display, template, checkExists, checkExistsPF)
	local success, title = pcall(mw.title.new, titleText)
	if not success or not title then
		return ''
	end
	if checkExists and not title.exists then
		return string.format('<!--%s|noexist:lua-->', display)
	end
	if checkExistsPF then
		return string.format('{{#ifexists:%s|[[%s|%s]]|<!--%s|noexist:pf-->}}', title.prefixedText, title.prefixedText, display, display)
	end
	return string.format('[[%s|%s]]', title.prefixedText, display)
end

local currentYear = tonumber(os.date('%Y'))

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 false) -- 檢查存檔目標是否存在(解析器函式版)
	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
		local i = 1
		while i <= range 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
					),
					template,
					checkExists,
					checkExistsPF
				)
				or string.format('<!--%4d-->', curYear)
			if i == 1 then
				output[#output + 1] = '|-'
			end
			output[#output + 1] = '|width=18%|' .. link
			if i == range then
				output[#output + 1] = '|width=10%|' .. '年'
			end
			i = i + 1
		end
		index = index + 1
	end
	if outputTableBox then
		output[#output + 1] = '|}'
	end
	if checkExistsPF then
		return mw.getCurrentFrame():preprocess(table.concat(output, '\n'))
	else
		return table.concat(output, '\n')
	end
end

return p