2023年政策修订增补工作正在进行中,欢迎参与!
  • Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

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