置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:Sandbox/不是液氮/CLSsort

贴贴♀百科,万娘皆可贴的百科全书!转载请标注来源页面的网页链接,并声明引自贴贴百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
local p = {}
local getArgs = require('Module:Arguments').getArgs

local this -- 当前frame。

function p.main(frame)
	this = frame
	local args = getArgs(frame)
	return p._main(args)
end

local function parseList(args)
	local lists, html = {}, {}
	for i, v in pairs(args) do
		if(i ~= "t") then lists[i] = mw.text.decode(mw.text.unstripNoWiki(v)) end
	end
	for i = 1, #lists do
		html[i] = this:preprocess(lists[i])
	end
	return lists, html
end

function p.build(frame)
	this = frame
	local args = getArgs(frame)
	local lists, html = parseList(args)
	local template = [[{{切换显示按钮|@default=b1|@radio=true|b1=按投稿时间排序|b2=按达成时间排序|b3=按所用时间排序|@#1=b1|@#2=b2|@#3=b3}}
	{{切换显示按钮样式|@cancel=display:none}}
	{{切换显示|b1|START_REPLACE_b1_END_REPLACE|div}}
	{{切换显示|b2|START_REPLACE_b2_END_REPLACE|div}}
	{{切换显示|b3|START_REPLACE_b3_END_REPLACE|div}}]]
	template = this:preprocess(template)
	local tabs, ret = {}, {}
	for i = 1, 3 do
		lists = p.Ssort(i, lists)
		tabs[i] = p.Stab(i, lists, html)
	end
	ret = mw.ustring.gsub(template, 'START_REPLACE_b(%d+)_END_REPLACE', tabs)
	return ret
end

function p._main(args)
	local lists, html = parseList(args)
	lists = p.Ssort(tonumber(args.t), lists)
	return p.Stab(tonumber(args.t), lists, html)
end

local function parseDate(...)
	local year, month, day, hour, min = ...
	return {
		year = tonumber(year),
		month = tonumber(month),
		day = tonumber(day),
		hour = tonumber(hour),
		min = tonumber(min)
	}
end

local function parseTime(...)
	local day, hour, min = ...
	day, hour, min = tonumber(day), tonumber(hour), tonumber(min)
	return day * 1440 + hour * 24 + min
end

function p.Ssort(t, lists)
	local pat = {
		"投稿时间%s*=%s*(%d+)-(%d+)-(%d+) (%d+):(%d+)",
		"达成时间%s*=%s*(%d+)-(%d+)-(%d+) (%d+):(%d+)",
		"(%d+)日(%d+)时(%d+)分"
	}
	table.sort(lists, function(a, b)
		if(t ~= 3) then
			a, b = parseDate(mw.ustring.match(a, pat[t])), parseDate(mw.ustring.match(b, pat[t]))
			return os.difftime(os.time(a), os.time(b)) < 0
		else
			a, b = parseTime(mw.ustring.match(a, pat[t])), parseTime(mw.ustring.match(b, pat[t]))
			return a < b
		end
	end)
	return lists
end

function p.Stab(t, lists, html)
	local Sstr = [[{{tabs/core
	|LabelPadding=0.5em 0.75em
	|LabelBorderColor=#aaa
	|LabelColor=#aaa
	|TextPadding=1em
	|TextBorderColor=#aaa
	]]
	local pat = {
		"投稿时间%s*=%s*(%d+)",
		"达成时间%s*=%s*(%d+)",
		"所用时间%s*=%s*(%d+)"
	}
	if(t ~= 3) then
		local j, Ynow, Etext = 1, "2000", {"年投稿", "年达成"}
		for i = 1, #lists do
			local Ythis = mw.ustring.match(lists[i], pat[t])
			if(Ythis ~= Ynow) then
				Sstr = Sstr.."|label"..j.."= {{color|#007FFF|'''<u>"..Ythis..Etext[t].."</u>'''}}|text"..j.."="
				j, Ynow = j + 1, Ythis
			end
			Sstr = Sstr..'<span data-replacement="'..i..'"></span>'
		end
	else
		local j = 1
		local Etext = {
			{0, 10, 40, 100, 200, 400, 600, 800, 1000, 1500, 10000},
			{"0~9日", "10~39日", "40~99日", "100~199日", "200~399日", "400~599日", "600~799日", "800~999日", "1000~1499日", "1500日以上"}
		}
		for i = 1, #lists do
			local d = tonumber(mw.ustring.match(lists[i], pat[3]))
			if(d >= Etext[1][j]) then
				Sstr = Sstr.."|label"..j.."= {{color|#007FFF|'''<u>"..Etext[2][j].."</u>'''}}|text"..j.."="
				j = j + 1
			end
			Sstr = Sstr..'<span data-replacement="'..i..'"></span>'
		end
	end
	Sstr = this:preprocess(Sstr.."}}")
	Sstr = mw.ustring.gsub(Sstr, '<span data%-replacement="(%d+)"></span>', html)
	return Sstr
end

return p