Minecraft Wiki

除另有声明,转载时均必须注明出处若簡繁轉換出錯,請以遊戲內為準请勇于扩充与修正内容有兴趣逛逛我们的微博沟通交流,欢迎到社区专页需要协助,请在告示板留言

了解更多

Minecraft Wiki
Advertisement
Minecraft Wiki沙盒

条目模板模块


欢迎来到中文Minecraft Wiki的公共沙盒。您可以在此页面上进行测试性编辑,但我们仍然建议在此处进行有建设性的编辑或翻译较短的条目。要编辑此页面,请点击上方的“编辑”铅笔按钮,或点击此处。在进行更改后,您可以点击编辑框下面的“显示预览”按钮来预览您的编辑,然后在相同位置点击“保存页面”按钮来保存编辑。

请注意,对此页面的更改不会被长期保留,因为此页面仅供测试编辑或翻译较短的条目之用。您也可以创建此页面的子页面(例如,Module:Sandbox/测试)来进行长期的项目,但它们仍然会在社区认定其过期后被删除。任何不具建设性的内容随时都可能会被清理。另外,如果您已注册账号,可以在自己的个人页面下创建您的个人沙盒,并且不用担心您的编辑会被清理。

您在编辑包括本页面在内的所有Wiki页面时都必须遵守相关的Wiki条例格式指导书面汉语指导等规范指导,否则您的编辑会被回退,您也可能会遭到警告甚至封禁处理。若有任何疑问,请到讨论页社区专页提出。若需要联系管理员,请到管理员告示板提出请求。


所有子页面:

若上述页面信息不准确,请点击此处刷新页面。

local p = {}

function trim(str)
	return (string.gsub(str, '^%s*(.-)%s*$', '%1'))
end

function table.unique(t, bArray)
	local check = {}
	local n = {}
	local idx = 1
	for k, v in pairs(t) do
		if not check[v] then
			if bArray then
				n[idx] = v
				idx = idx + 1
			else
				n[k] = v
			end
			check[v] = true
		end
	end
	return n
end

function getTimeStyle(time)
	local _, _, year, month, day, hour, min, sec = time:find '(%d+)年(%d+)月(%d+)日%s%(.-%)%s(%d+):(%d+)'

	-- divided by 86400 is to convert seconds to days
	local diff = os.difftime(os.time(), os.time {year = year, month = month, day = day, hour = hour, min = min, sec = sec}) / 86400

	if diff >= 30 then
		return 'background-color: #bbb;'
	end
	if diff >= 7 then
		return 'background-color: #ddd;'
	end
	if diff >= 1 then
		return ''
	end
	return 'background-color: #efe;'
end

function close(text, timeStyle)
	local yes = string.match(text, 'closed%-topic%-yes')
	local no = string.match(text, 'closed%-topic%-no')
	if yes then
		return 'background-color: #efe;'
	end
	if no then
		return 'background-color: #fee;'
	end
	return timeStyle
end

function conv(talk)
	-- 关键词替换
	-- 如有字符导致生成错误,请在这里添加转换
	talk = talk
		:gsub('===(.-)===', '%1')
		:gsub('Special:用户贡献/', 'User:ip:') -- ip user handle first
		:gsub('用户', 'User')
		:gsub('user', 'User')
	return talk
end

function trim(text)
	-- remove special markers and spaces

	text = mw.text.killMarkers(text):gsub('^[\t\r\n\f]*(.-)[\t\r\n\f]*$', '%1')
	return text
end

function makeUserLink(text)
	--输入用户名以及带有ip:前缀的ip用户。
	local ipUser = string.match(text, 'ip:(.*)')
	if ipUser then
		return '[[Special:用户贡献/' .. ipUser .. '|' .. ipUser .. ']]'
	else
		return '[[User:' .. text .. '|' .. text .. ']]'
	end
end

function getTalkList(pageName)
	--输入页面名,返回一个包含文本的讨论信息表
	--例如:talklist[1][text]
	local talk = conv(mw.getCurrentFrame():expandTemplate {title = pageName}) .. '=='
	local talkList = {}
	for topic in talk:gmatch '==\n(.-)==' do
		talkList[#talkList + 1] = topic
	end

	return talkList
end

function getTitleList(pageName)
	local talk = conv(mw.getCurrentFrame():expandTemplate {title = ':' .. pageName})
	local titleList = {}
	for title in talk:gmatch '==(.-)==' do
		titleList[#titleList + 1] = title
	end

	return titleList
end

function getTalkTime(talk)
	return tostring(talk):match '[%s%S]*(%d%d%d%d.*) %(UTC%)'
end

function getUserInfo(text)
	-- 输入讨论的文本,输出和User有关的转换部分table
	local userList = {}
	for user in tostring(text):gmatch 'User:([^\n]-)%|[^\n]-UTC' do
		userList[#userList + 1] = user
	end

	local userNum = #userList
	local uniqueUserNum = #(table.unique(userList))

	return {userNum = userNum, uniqueUserNum = uniqueUserNum, firstUser = userList[1], lastUser = userList[#userList]}
end

function generateTable(talkTitle, talkText)
	local body = [[{| class="wikitable sortable collapsible" style="text-align: center;"
|-
! # !! 话题 !! 发言条数 !! 参与人数 !! 发起者 !! 最后发言者 !! 最后发言时间(UTC)]]

	local userInfo, time
	local userStyle, uniqueUserStyle, timeStyle

	for i = 1, #talkText do
		time, userInfo = getTalkTime(talkText[i]), getUserInfo(talkText[i])
		-- 对表格添加样式
		userStyle = userInfo.userNum == 1 and 'background-color: #fcc;' or ''
		uniqueUserStyle = userInfo.uniqueUserNum == 1 and 'background-color: #fcc;' or ''
		timeStyle = close(talkText[i], getTimeStyle(time))
		-- 组装表格一个话题的部分
		body = body .. '\n' ..
			table.concat(
				{
					'|-',
					'! ' .. i,
					'| ' .. trim(talkTitle[i]),
					'| style="' .. userStyle .. '" | ' .. userInfo.userNum,
					'| style="' .. uniqueUserStyle .. '" | ' .. userInfo.uniqueUserNum,
					'| ' .. makeUserLink(userInfo.firstUser),
					'| style="' .. timeStyle .. '" | ' .. makeUserLink(userInfo.lastUser),
					'| style="' .. timeStyle .. '" | ' .. time
				},
				'\n'
			)
	end

	return body .. '\n|}'
end

function p.main(f)
	local args = f
	if f == mw.getCurrentFrame() then
		args = f:getParent().args
	end
	f = mw.getCurrentFrame()

	local talkTitle, talkText = getTitleList(args[1]), getTalkList(args[1])
	for i = 1, #talkTitle do
		talkTitle[i] = f:preprocess(talkTitle[i])
	end
	local result = tostring(generateTable(talkTitle, talkText))

	if #talkTitle == #talkText then
		return result
	else
		return error 'Topic list获取错误。'
	end
end

return p
Advertisement