模組:沙盒/StevenK234/test

维基百科,自由的百科全书
文档图示 模块文档[查看] [编辑] [历史] [清除缓存]

此為{{Chronology category template}}的Lua版本,當前尚未完成。

測試集[编辑]

年份[编辑]

{{#invoke:沙盒/StevenK234/test|main|year=2021|type=建立}}

{{#invoke:沙盒/StevenK234/test|main|year=-2021|type=建立}}

{{#invoke:沙盒/StevenK234/test|main|year=1|type=建立}}

{{#invoke:沙盒/StevenK234/test|main|year=-1|type=建立}}

年代[编辑]

{{#invoke:沙盒/StevenK234/test|main|decade=2020|type=建立}}

{{#invoke:沙盒/StevenK234/test|main|decade=0|type=建立}}

{{#invoke:沙盒/StevenK234/test|main|decade=-0|type=建立}}

{{#invoke:沙盒/StevenK234/test|main|decade=-2020|type=建立}}

世紀[编辑]

{{#invoke:沙盒/StevenK234/test|main|century=21|type=建立}}

{{#invoke:沙盒/StevenK234/test|main|century=1|type=建立}}

{{#invoke:沙盒/StevenK234/test|main|century=-1|type=建立}}

{{#invoke:沙盒/StevenK234/test|main|century=-21|type=建立}}

千紀=[编辑]

{{#invoke:沙盒/StevenK234/test|main|millennium=3|type=建立}}

{{#invoke:沙盒/StevenK234/test|main|millennium=-3|type=建立}}

local p = {}
local getArgs = require('Module:Arguments').getArgs
local Navbox = require('Module:Navbox').needsHorizontalLists
local format = mw.ustring.format

local period = require('Module:沙盒/StevenK234/period')
local MILLENNIUM = period.MILLENNIUM
local CENTURY = period.CENTURY
local DECADE = period.DECADE
local YEAR = period.YEAR

local function Flatlist()
	--平行列表格式
	local css = mw.html.create('div')
				:addClass("hlist")
				:css({['margin-left'] = '0em'})
	return css
end

local function HlistLoop(i,p)
	--以迴圈產生多個時間的平行列表
	local val = tonumber(i)
	if (p == 'year') then
		if (val > 0) then
			if (val < 10) then return 1,9,1
			else return 0,9,1
			end
		elseif (val < 0) then
			if (val > -10) then return -9,-1,1
			else return -9,0,1
			end
		end
	end
	if (p == 'decade') then
		if (i == "0" or val > 0) then return 0,9,1
		elseif  (i == "-0" or val < 0) then return -9,0,1
		end
	end
	if (p == 'century') then
		if (val > 0) then return 1,10,1
		elseif  (val < 0) then return -10,-1,1
		end
	end
end

local function YearList(num,type)
	local css = Flatlist()
	min,max,inc = HlistLoop(num,'year')
	for j = min,max,inc do
		if (tonumber(num) > 0) then
			func = DECADE(tonumber(num),false)+j
			css:tag('li'):wikitext(format("[[:Category:%s年%s|%s]]",func,type,func)):done()
		elseif (tonumber(num) < 0) then
			func = math.abs(DECADE(tonumber(num),false)+j)
			css:tag('li'):wikitext(format("[[:Category:前%s年%s|–%s]]",func,type,func)):done()
		end
	end
	css:done()
	return css
end

local function DecadeList(num,type)
	local css = Flatlist()
	min,max,inc = HlistLoop(num,'decade')
	for j = min,max,inc do
		if (num == "0" or tonumber(num) > 0) then
			func = (CENTURY(tonumber(num)+1,false)-1)*100+j*10
			css:tag('li'):wikitext(format("[[:Category:%s年代%s|%s年代]]",func,type,func)):done()
		elseif (num == "-0" or tonumber(num) < 0) then
			func = math.abs((CENTURY(tonumber(num)-1,false)+1)*100+j*10)
			css:tag('li'):wikitext(format("[[:Category:前%s年代%s|前%s年代]]",func,type,func)):done()
		end
	end
	css:done()
	return css
end

local function CenturyList(num,type)
	local css = Flatlist()
	min,max,inc = HlistLoop(num,'century')
	for j = min,max,inc do
		if (tonumber(num) > 0) then
			func = (MILLENNIUM(tonumber(num)*100,false)-1)*10+j
			css:tag('li'):wikitext(format("[[:Category:%s世紀%s|%s世紀]]",func,type,func)):done()
		elseif (tonumber(num) < 0) then
			func = math.abs((MILLENNIUM(tonumber(num)*100,false)+1)*10+j)
			css:tag('li'):wikitext(format("[[:Category:前%s世紀%s|前%s世紀]]",func,type,func)):done()
		end
	end
	css:done()
	return css
end

local function MillenniumList(num,type)
	local css = Flatlist()
	for _,j in ipairs({-5,-4,-3,-2,-1,1,2,3}) do
		func = MILLENNIUM(tonumber(j)*1000,true)
		css:tag('li'):wikitext(format("[[:Category:%s%s|%s]]",func,type,func)):done()		
	end
	css:done()
	return css
end

local function Direction(num,p,type)
	--UpperL,UpperM,UpperR,LowerL,LowerR格式
	local val = tonumber(num)
	if (p == 'year') then
		UpperM = format("%s%s",DECADE(num,true),type)
		if (val > 0) then
			UpperLFunc = DECADE(num,false)-10
			UpperRFunc = DECADE(num,false)+10
			UpperL = function(x,num) if (x < 10) then return format("前0年代%s|&larr; 前0年代",type) else return format("%s年代%s|&larr; %s年代",UpperLFunc,type,UpperLFunc) end end
			UpperR = format("%s年代%s|%s年代 &rarr;",UpperRFunc,type,UpperRFunc)
			LowerL = function(x,num) if (x < 10) then return format("前1年%s",type) else return format("%s年%s",DECADE(num,false)-1,type) end end
			LowerR = format("%s年%s",DECADE(num,false)+10,type)
			return UpperL(val,num), UpperM, UpperR, LowerL(val,num), LowerR
		elseif (val < 0) then
			UpperLFunc = math.abs(DECADE(num,false)-10)
			UpperRFunc = math.abs(DECADE(num,false)+10)
			UpperL = format("前%s年代%s|&larr; 前%s年代",UpperLFunc,type,UpperLFunc)
			UpperR = function(x,num) if (x > -10) then return format("0年代%s|0年代 &rarr;",type) else return format("前%s年代%s|前%s年代 &rarr;",UpperRFunc,type,UpperRFunc) end end
			LowerL = format("前%s年%s",math.abs(DECADE(num,false)-10),type)
			LowerR = function(x,num) if (x > -10) then return format("1年%s",type) else return format("前%s年%s",math.abs(DECADE(num,false)+1),type) end end
			return UpperL, UpperM, UpperR(val,num), LowerL, LowerR(val,num)
		end
	end
	if (p == 'decade') then
		if (num == "0" or val > 0) then
			UpperLFunc = CENTURY(val+1,false)-1
			UpperRFunc = CENTURY(val+1,false)+1
			UpperL = function(x,num) if (x < 100) then return format("前1世紀%s|&larr; 前1世紀",type) else return format("%s世紀%s|&larr; %s世紀",UpperLFunc,type,UpperLFunc) end end
			UpperM = format("%s%s",CENTURY(val+1,true),type)
			UpperR = format("%s世紀%s|%s世紀 &rarr;",UpperRFunc,type,UpperRFunc)
			LowerL = function(x,num) if (x < 100) then return format("前0年代%s",type) else return format("%s年代%s",(CENTURY(val+1,false)-1)*100-10,type) end end
			LowerR = format("%s年代%s",(CENTURY(val+1,false)-1)*100+100,type)
			return UpperL(val,num), UpperM, UpperR, LowerL(val,num), LowerR
		elseif (num == "-0" or val < 0) then
			UpperLFunc = math.abs(CENTURY(val-1,false)-1)
			UpperRFunc = math.abs(CENTURY(val-1,false)+1)
			UpperL = format("前%s世紀%s|&larr; 前%s世紀",UpperLFunc,type,UpperLFunc)
			UpperM = format("%s%s",CENTURY(val-1,true),type)
			UpperR = function(x,num) if (x > -100) then return format("1世紀%s|1世紀 &rarr;",type) else return format("前%s世紀%s|前%s世紀 &rarr;",UpperRFunc,type,UpperRFunc) end end
			LowerL = format("前%s年代%s",math.abs((CENTURY(val-1,false)+1)*100-100),type)
			LowerR = function(x,num) if (x > -100) then return format("0年代%s",type) else return format("前%s年代%s",math.abs((CENTURY(val-1,false)+1)*100+10),type) end end
			return UpperL, UpperM, UpperR(val,num), LowerL, LowerR(val,num)
		end
	end
	if (p == 'century') then
		UpperM = format("%s%s",MILLENNIUM(val*100,true),type)
		if (val > 0) then
			UpperLFunc = MILLENNIUM(val*100,false)-1
			UpperRFunc = MILLENNIUM(val*100,false)+1
			UpperL = function(x,num) if (x <= 10) then return format("前1千紀%s|&larr; 前1千紀",type) else return format("%s千紀%s|&larr; %s千紀",UpperLFunc,type,UpperLFunc) end end
			UpperR = format("%s千紀%s|%s千紀 &rarr;",UpperRFunc,type,UpperRFunc)
			LowerL = function(x,num) if (x <= 10) then return format("前1世紀%s",type) else return format("%s世紀%s",(MILLENNIUM(val*100,false)-1)*10,type) end end
			LowerR = format("%s世紀%s",(MILLENNIUM(val*100,false)-1)*10+11,type)
			return UpperL(val,num), UpperM, UpperR, LowerL(val,num), LowerR
		elseif (val < 0) then
			UpperLFunc = math.abs(MILLENNIUM(val*100,false)-1)
			UpperRFunc = math.abs(MILLENNIUM(val*100,false)+1)
			UpperL = format("前%s千紀%s|&larr; 前%s千紀",UpperLFunc,type,UpperLFunc)
			UpperR = function(x,num) if (x >= -10) then return format("1千紀%s|1千紀 &rarr;",type) else return format("前%s千紀%s|前%s千紀 &rarr;",UpperRFunc,type,UpperRFunc) end end
			LowerL = format("前%s世紀%s",math.abs((MILLENNIUM(val*100,false)+1)*10-11),type)
			LowerR = function(x,num) if (x >= -10) then return format("1世紀%s",type) else return format("前%s世紀%s",math.abs((MILLENNIUM(val*100,false)+1)*10),type) end end
			return UpperL, UpperM, UpperR(val,num), LowerL, LowerR(val,num)
		end
	end
end
	
local function CategoryBox(list,UpperL,UpperM,UpperR,LowerL,LowerR)
	--分類導航模板
	local css = mw.html.create('table')
				:addClass("toccolours")
				:attr({align="center"})
	if UpperL and UpperM and UpperR then
		row1 = css:tag('tr')
		row1:tag('td'):attr('colspan',3)
			:tag('div')
				:css({float = 'left'})
				:wikitext(format("[[:Category:%s]]",UpperL))
				:done()
			:tag('div')
				:css({float = 'right'})
				:wikitext(format("[[:Category:%s]]",UpperR))
				:done()
			:tag('div')
				:addClass("center")
				:wikitext(format("<b>[[:Category:%s|%s]]</b>",UpperM,UpperM))
				:done()
			:done()
	end
	row2 = css:tag('tr')
	if LowerL and LowerR then
		row2:tag('td')
				:css({ ['text-align'] = 'center' })
				:wikitext(format("[[:Category:%s|&larr;]]",LowerL))
				:done()
			:tag('td')
				:node(list)
				:done()
			:tag('td')
				:css({ ['text-align'] = 'center' })
				:wikitext(format("[[:Category:%s|&rarr;]]",LowerR))
				:done()
			:done()
	else
		row2:tag('td')
			:node(list)
			:done()
		:done()
	end
	return css	
end

function p.main(frame)
	local args = getArgs(frame,{trim = false,removeBlanks = false})
	return p._main(args)
end

function p._main(args)
	local type = args.type
	if args.year then
		UpperL,UpperM,UpperR,LowerL,LowerR = Direction(args.year,"year",type)
		return CategoryBox(YearList(args.year,type),UpperL,UpperM,UpperR,LowerL,LowerR)
	end
	if args.decade then
		UpperL,UpperM,UpperR,LowerL,LowerR = Direction(args.decade,"decade",type)
		return CategoryBox(DecadeList(args.decade,type),UpperL,UpperM,UpperR,LowerL,LowerR)
	end
	if args.century then
		UpperL,UpperM,UpperR,LowerL,LowerR = Direction(args.century,"century",type)
		return CategoryBox(CenturyList(args.century,type),UpperL,UpperM,UpperR,LowerL,LowerR)
	end
	if args.millennium then
		return CategoryBox(MillenniumList(args.millennium,type))
	end
end

return p