Module:Sandbox/DVRTed: Difference between revisions
Appearance
Content deleted Content added
copying from testwiki:Module:Sandbox/DVRTed/test |
reflect changes |
||
Line 1: | Line 1: | ||
local functs = {} |
local functs = {} |
||
local function |
local function parse_timestamp(ts) |
||
local hour, min, day, monthname, year = ts:match( |
local hour, min, day, monthname, year = ts:match( |
||
"(%d%d):(%d%d), (%d+) ([^ ]+) (%d+) %(UTC%)") |
"(%d%d):(%d%d), (%d+) ([^ ]+) (%d+) %(UTC%)") |
||
Line 33: | Line 33: | ||
function trim(s) return s:match("^%s*(.-)%s*$") end |
function trim(s) return s:match("^%s*(.-)%s*$") end |
||
local function populate_sections(raw_content) |
|||
local section_body_pattern = "==%s*(.-)%s*==%s*([^=]+)" |
|||
⚫ | |||
local subsection_pattern = "===(=*)%s*(.-)%s*===(=*)%s*" |
|||
local current_title = nil |
|||
⚫ | |||
local current_body = "" |
|||
for line in raw_content:gmatch("[^\r\n]+") do |
|||
⚫ | |||
local section_title = line:match("^==%s*(.-)%s*==$") |
|||
⚫ | |||
if section_title then |
|||
⚫ | |||
if current_title then |
|||
⚫ | |||
table.insert(sections, { |
|||
if not rawContent then return "Page not found or empty" end |
|||
title = current_title, |
|||
body = current_body:gsub("^%s*", ""):gsub("%s*$", "") |
|||
}) |
|||
end |
|||
current_title = section_title |
|||
current_body = "" |
|||
else |
|||
if current_title then |
|||
current_body = current_body .. line .. "\n" |
|||
end |
|||
end |
|||
end |
|||
if current_title then |
|||
-- replace ===(...=) subsection ===(...=) with [subsec]subsection[subsec] |
|||
table.insert(sections, { |
|||
title = current_title, |
|||
local content = rawContent:gsub(subsection_pattern, "[subsec]%2[subsec]") |
|||
body = current_body:gsub("^%s*", ""):gsub("%s*$", "") |
|||
content = content .. "\n==END==" |
|||
}) |
|||
end |
|||
return sections |
|||
end |
|||
⚫ | |||
for header, body in content:gmatch(section_body_pattern) do |
|||
function getlatest_section(sections) |
|||
⚫ | |||
local timed_sections = {} |
|||
for _, section in ipairs(sections) do |
|||
local body = section.body |
|||
local title = section.title |
|||
local latest_time = 0 |
local latest_time = 0 |
||
local latest_raw = nil |
local latest_raw = nil |
||
for ts in body:gmatch(timestamp) do |
for ts in body:gmatch(timestamp) do |
||
local parsed = |
local parsed = parse_timestamp(ts) |
||
if parsed and parsed > latest_time then |
if parsed and parsed > latest_time then |
||
latest_time = parsed |
latest_time = parsed |
||
Line 64: | Line 84: | ||
if latest_time > 0 then |
if latest_time > 0 then |
||
table.insert( |
table.insert(timed_sections, { |
||
title = |
title = title, |
||
body = body, |
body = body, |
||
latest_time = latest_time, |
latest_time = latest_time, |
||
Line 71: | Line 91: | ||
}) |
}) |
||
end |
end |
||
end |
end |
||
local latest_section = {raw = nil, time = 0, title = nil} |
local latest_section = {raw = nil, time = 0, title = nil} |
||
for _, section in ipairs( |
for _, section in ipairs(timed_sections) do |
||
if (section.latest_time > latest_section.time) then |
if (section.latest_time > latest_section.time) then |
||
latest_section.raw = section.latest_raw |
latest_section.raw = section.latest_raw |
||
Line 83: | Line 102: | ||
end |
end |
||
end |
end |
||
return latest_section |
|||
end |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
local sections = populate_sections(raw_content) |
|||
local latest_section = getlatest_section(sections) |
|||
if latest_section.title then |
if latest_section.title then |
||
⚫ | |||
local time_ago = frame:preprocess( |
|||
⚫ | |||
return string.format( |
return string.format( |
||
"Number of threads: '''%d'''<br>Thread with the most recent comment: [[%s#%s|%s]] (%s)", |
"Number of threads: '''%d'''<br>Thread with the most recent comment: [[%s#%s|%s]] (%s)", |
||
#sections, prefixed_title, latest_section.title, latest_section.title, time_ago) |
#sections, prefixed_title, latest_section.title, |
||
latest_section.title, time_ago) |
|||
end |
end |
||
end |
end |
Latest revision as of 09:15, 27 June 2025
local functs = {}
local function parse_timestamp(ts)
local hour, min, day, monthname, year = ts:match(
"(%d%d):(%d%d), (%d+) ([^ ]+) (%d+) %(UTC%)")
if not (hour and min and day and monthname and year) then return nil end
local months = {
January = 1,
February = 2,
March = 3,
April = 4,
May = 5,
June = 6,
July = 7,
August = 8,
September = 9,
October = 10,
November = 11,
December = 12
}
return os.time {
year = tonumber(year),
month = months[monthname],
day = tonumber(day),
hour = tonumber(hour),
min = tonumber(min),
sec = 0
}
end
function trim(s) return s:match("^%s*(.-)%s*$") end
local function populate_sections(raw_content)
local sections = {}
local current_title = nil
local current_body = ""
for line in raw_content:gmatch("[^\r\n]+") do
local section_title = line:match("^==%s*(.-)%s*==$")
if section_title then
if current_title then
table.insert(sections, {
title = current_title,
body = current_body:gsub("^%s*", ""):gsub("%s*$", "")
})
end
current_title = section_title
current_body = ""
else
if current_title then
current_body = current_body .. line .. "\n"
end
end
end
if current_title then
table.insert(sections, {
title = current_title,
body = current_body:gsub("^%s*", ""):gsub("%s*$", "")
})
end
return sections
end
function getlatest_section(sections)
local timestamp = "%d%d:%d%d, %d+ [^ ]+ %d%d%d%d %(UTC%)"
local timed_sections = {}
for _, section in ipairs(sections) do
local body = section.body
local title = section.title
local latest_time = 0
local latest_raw = nil
for ts in body:gmatch(timestamp) do
local parsed = parse_timestamp(ts)
if parsed and parsed > latest_time then
latest_time = parsed
latest_raw = ts
end
end
if latest_time > 0 then
table.insert(timed_sections, {
title = title,
body = body,
latest_time = latest_time,
latest_raw = latest_raw
})
end
end
local latest_section = {raw = nil, time = 0, title = nil}
for _, section in ipairs(timed_sections) do
if (section.latest_time > latest_section.time) then
latest_section.raw = section.latest_raw
latest_section.time = section.latest_time
latest_section.title = section.title
end
end
return latest_section
end
functs.talkstats = function(frame)
local prefixed_title = frame.args[1]
local title = mw.title.new(prefixed_title)
local raw_content = title and title.getContent and title:getContent()
local sections = populate_sections(raw_content)
local latest_section = getlatest_section(sections)
if latest_section.title then
local time_ago = frame:preprocess(
"{{time ago|" .. latest_section.raw .. "}}")
return string.format(
"Number of threads: '''%d'''<br>Thread with the most recent comment: [[%s#%s|%s]] (%s)",
#sections, prefixed_title, latest_section.title,
latest_section.title, time_ago)
end
end
return functs