Module:Topic list:修订间差异
小无编辑摘要 |
小无编辑摘要 |
||
第1行: | 第1行: | ||
local p = {} | local p = {} | ||
function table.unique(t, bArray) | 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 | end | ||
function | function getTimeStyle(time) | ||
local _, _, year, month, day, hour, min = 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}) / 86400 | |||
local | |||
if diff >= 30 then | |||
return 'background-color: #bbb;' | |||
end | |||
if diff >= 7 then | |||
return 'background-color: #ddd;' | |||
if | end | ||
if | if diff >= 1 then | ||
if | return '' | ||
return | end | ||
return 'background-color: #efe;' | |||
end | end | ||
function | function close(text) | ||
if text:match 'closed%-topic%-yes' then | |||
return 'background-color: #efe;' | |||
end | |||
if text:match 'closed%-topic%-no' then | |||
return 'background-color: #fee;' | |||
end | end | ||
return | return "" | ||
end | end | ||
function | function conv(talk) | ||
--关键词替换 | -- 关键词替换 | ||
--如有字符导致生成错误,请在这里添加转换 | -- 如有字符导致生成错误,请在这里添加转换 | ||
talk= | talk = talk | ||
:gsub('===(.-)===', '%1') | |||
:gsub('Special:用户贡献/', 'User:ip:') -- ip user handle first | |||
:gsub(' 用户', 'User') | |||
:gsub('user', 'User') | |||
:gsub('<.->', '') | |||
return talk | return talk | ||
end | end | ||
function | function trim(text) | ||
-- | -- remove special strip markers and spaces | ||
text= | text = mw.text.killMarkers(text):gsub('^[\t\r\n\f]*(.-)[\t\r\n\f]*$', '%1') | ||
return text | return text | ||
end | end | ||
function | function makeUserLink(text) | ||
--输入用户名以及带有ip:前缀的ip用户。 | -- 输入用户名以及带有ip:前缀的ip用户。 | ||
local | local ipUser = text:match 'ip:(.*)' | ||
if | if ipUser then | ||
return | return '[[Special:用户贡献/' .. ipUser .. '|' .. ipUser .. ']]' | ||
else | else | ||
return | return '[[User:' .. text .. '|' .. text .. ']]' | ||
end | end | ||
end | end | ||
function | function getTalkList(pageName) | ||
--输入页面名,返回一个包含文本的讨论信息表 | -- 输入页面名,返回一个包含文本的讨论信息表 | ||
--例如:talklist[1][text] | -- 例如:talklist[1][text] | ||
local talk= | local talk = conv(mw.getCurrentFrame():expandTemplate {title = ':' .. pageName}) .. '==' | ||
local | local talkList = {} | ||
for topic in talk:gmatch '==\n(.-)==' do | |||
talkList[#talkList + 1] = topic | |||
end | end | ||
return talkList | |||
end | end | ||
function | function getTitleList(pageName) | ||
local talk= | local talk = conv(mw.getCurrentFrame():expandTemplate {title = ':' .. pageName}) | ||
local titleList = {} | |||
local | for title in talk:gmatch '==(.-)==' do | ||
titleList[#titleList + 1] = title | |||
end | end | ||
return | return titleList | ||
end | end | ||
function | function getTalkTime(talk) | ||
return talk:match '[%s%S]*(%d%d%d%d.*) %(CST%)' | |||
end | end | ||
function | function getUserInfo(text) | ||
--输入讨论的文本,输出和User有关的转换部分table | -- 输入讨论的文本,输出和User有关的转换部分table | ||
local userList = {} | |||
text | for user in text:gmatch 'User:([^\n]-)%|[^\n]-CST' do | ||
userList[#userList + 1] = user | |||
end | end | ||
return | local userNum = #userList | ||
local uniqueUserNum = #(table.unique(userList)) | |||
return {userNum = userNum, uniqueUserNum = uniqueUserNum, firstUser = userList[1], lastUser = userList[#userList]} | |||
end | end | ||
function | function generateTable(talkTitle, talkText) | ||
local | local body = { | ||
'{| class="wikitable sortable collapsible talktable" style="text-align: center;"', | |||
'|-', | |||
'! # !! 话题 !! 发言 条数 !! 参与 人数 !! 发起者 !! 最 后 发言 者 ', | |||
'! class="talkpage-topic-list-time" | 最后发言时间 (CST)' | |||
} | |||
local | local userInfo, time | ||
local | local userStyle, uniqueUserStyle, timeStyle, serialStyle | ||
for i=1, | |||
for i = 1, #talkText do | |||
time, | 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 = getTimeStyle(time) | |||
serialStyle = close(talkText[i]) | |||
-- 组装表格一个 话 题的部分 | |||
--组装表格一个 | table.insert( | ||
body, | |||
table.concat( | |||
{ | |||
'|-', | |||
'! style="' .. serialStyle .. '" | ' .. i, | |||
'| ' .. '[[#' .. trim(talkTitle[i]) .. '|' .. trim(talkTitle[i]) .. ']]', | |||
'| style="' .. userStyle .. '" | ' .. userInfo.userNum, | |||
'| style="' .. uniqueUserStyle .. '" | ' .. userInfo.uniqueUserNum, | |||
'| ' .. makeUserLink(userInfo.firstUser), | |||
'| style="' .. timeStyle .. '" | ' .. makeUserLink(userInfo.lastUser), | |||
'| class="talkpage-topic-list-time" style="' .. timeStyle .. '" | ' .. time | |||
}, | |||
'\n' | |||
) | |||
) | |||
end | end | ||
table.insert(body, '|}') | |||
return | return table.concat(body, '\n') | ||
end | |||
end | function p.main() | ||
local args = require([[Module:ProcessArgs]]).merge(true) | |||
local 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 = generateTable(talkTitle, talkText) | |||
if #talkTitle == #talkText then | |||
return result | |||
else | |||
return error 'Topic list获取错误。' | |||
end | |||
end | end | ||
return p | return p |
2021年4月13日 (二) 18:22的版本
可在Module:Topic list/doc创建此模块的帮助文档
local p = {}
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 = 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}) / 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)
if text:match 'closed%-topic%-yes' then
return 'background-color: #efe;'
end
if text:match 'closed%-topic%-no' then
return 'background-color: #fee;'
end
return ""
end
function conv(talk)
-- 关键词替换
-- 如有字符导致生成错误,请在这里添加转换
talk = talk
:gsub('===(.-)===', '%1')
:gsub('Special:用户贡献/', 'User:ip:') -- ip user handle first
:gsub('用户', 'User')
:gsub('user', 'User')
:gsub('<.->', '')
return talk
end
function trim(text)
-- remove special strip 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 = text:match '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 talk:match '[%s%S]*(%d%d%d%d.*) %(CST%)'
end
function getUserInfo(text)
-- 输入讨论的文本,输出和User有关的转换部分table
local userList = {}
for user in text:gmatch 'User:([^\n]-)%|[^\n]-CST' 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 talktable" style="text-align: center;"',
'|-',
'! # !! 话题 !! 发言条数 !! 参与人数 !! 发起者 !! 最后发言者 ',
'! class="talkpage-topic-list-time" | 最后发言时间(CST)'
}
local userInfo, time
local userStyle, uniqueUserStyle, timeStyle, serialStyle
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 = getTimeStyle(time)
serialStyle = close(talkText[i])
-- 组装表格一个话题的部分
table.insert(
body,
table.concat(
{
'|-',
'! style="' .. serialStyle .. '" | ' .. i,
'| ' .. '[[#' .. trim(talkTitle[i]) .. '|' .. trim(talkTitle[i]) .. ']]',
'| style="' .. userStyle .. '" | ' .. userInfo.userNum,
'| style="' .. uniqueUserStyle .. '" | ' .. userInfo.uniqueUserNum,
'| ' .. makeUserLink(userInfo.firstUser),
'| style="' .. timeStyle .. '" | ' .. makeUserLink(userInfo.lastUser),
'| class="talkpage-topic-list-time" style="' .. timeStyle .. '" | ' .. time
},
'\n'
)
)
end
table.insert(body, '|}')
return table.concat(body, '\n')
end
function p.main()
local args = require([[Module:ProcessArgs]]).merge(true)
local 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 = generateTable(talkTitle, talkText)
if #talkTitle == #talkText then
return result
else
return error 'Topic list获取错误。'
end
end
return p