Module:RecentUnNews/ongoing
Jump to navigation
Jump to search
-------------------------------------------------------------------------------
-- Sets max number of entries in "Ongoing" section
-------------------------------------------------------------------------------
local getArgs = require('Module:Arguments').getArgs
local p = {}
-- Set limit via section
local ONGOING_MAX = 5
local RECENT_MAX = 6
local UPCOMING_MAX = 6
-- Default limit, overriden by above
local DEFAULT_MAX = 4
-- Assigns section to section max limits via title link text
-- Takes section title and will lowercase it just for this section
-- For example, if section label is "'''[[Link|New Section]]'''", then you add like:
-- ['new section'] = NEW_MAX,
-- Otherwise, will default to DEFAULT_MAX
local SECTION_MAX = {
['ongoing'] = ONGOING_MAX,
['recent deaths'] = RECENT_MAX,
['upcoming deaths'] = UPCOMING_MAX,
}
-- What separator symbol the module checks to separate entries (DON'T TOUCH!)
local SEP = ' • '
local function split(str, delim)
return mw.text.split(str, delim, true)
end
-- Extract the display text from a bold piped wikilink header.
-- e.g. "'''[[You are Dead|Recent deaths]]''': ..." → "recent deaths"
-- Returns nil if no match.
local function getSectionTag(text)
-- Try piped link first: '''[[Target|Display]]'''
local display = text:match("'''%[%[.-|(.-)%]%]'''")
-- Fall back to non-piped link: '''[[Display]]'''
if not display then
display = text:match("'''%[%[(.-)%]%]'''")
end
if display then
return mw.ustring.lower(mw.text.trim(display))
end
return nil
end
-- Trim a single line, keeping the last N bullet-separated entries.
-- N is determined by section tag, falling back to DEFAULT_MAX.
-- Preserves everything before the first colon-space as the label prefix.
local function trimLine(text)
if not text or mw.text.trim(text) == '' then
return text or ''
end
-- Find the label prefix: everything up to and including the first ": "
-- that follows a closing bold marker ('''). This handles lines like:
-- '''[[UnNews|Ongoing]]''': stuff • stuff
local prefix, items_str = text:match("^(.-'''.-:%s*)(.*)")
if not prefix then
-- No recognisable prefix; return the line untouched
return text
end
-- Determine the max for this section
local tag = getSectionTag(text)
local max = (tag and SECTION_MAX[tag]) or DEFAULT_MAX
local items = split(items_str, SEP)
-- Trim whitespace and remove empty entries
local cleaned = {}
for i = 1, #items do
local trimmed = mw.text.trim(items[i])
if trimmed ~= '' then
cleaned[#cleaned + 1] = trimmed
end
end
-- Keep only the last `max` items; warn if any were omitted
if #cleaned > max then
local omitted = {}
for i = 1, #cleaned - max do
omitted[#omitted + 1] = cleaned[i]
end
local sectionLabel = tag or 'unknown section'
mw.addWarning(string.format(
'<span class="error"><b>Too many entries!</b></span> The "%s" section exceeded %d entries (had %d). Omitted: <code><nowiki>%s</nowiki></code>',
sectionLabel, max, #cleaned, table.concat(omitted, SEP)
))
local kept = {}
for i = #cleaned - max + 1, #cleaned do
kept[#kept + 1] = cleaned[i]
end
cleaned = kept
end
return prefix .. table.concat(cleaned, SEP)
end
-- Main entry point. Processes each non-blank line independently,
-- preserving blank lines between them (which the parser renders as <p>).
function p.main(frame)
local args = getArgs(frame, { parentFirst = true })
local text = args.text or args[1]
if not text or text == '' then
return ''
end
local output = {}
for line in (text .. '\n'):gmatch('(.-)\n') do
if mw.text.trim(line) == '' then
output[#output + 1] = ''
else
output[#output + 1] = trimLine(line)
end
end
return '<div id="mf-ongoing">\n' .. table.concat(output, '\n') .. '\n</div>'
end
return p