Module:NewsList/doc
This is the documentation page for Module:NewsList
NewsList Module Documentation
The `NewsList` module is designed to generate a table listing recent events from the `GlobalEvents` page. This module processes event data stored in a specific format and outputs it as a structured table.
Function: generateTable(frame)
This function reads the events from the `GlobalEvents` page and generates a table with the event data.
Parameters
- `frame` - A frame object that provides access to the arguments passed to the module invocation.
Usage
To use this module, invoke it in a wiki page with the following syntax:
{{#invoke:NewsList|generateTable}}
Optionally, you can specify the number of rows to display and customize the table's class and style:
{{#invoke:NewsList|generateTable|rows=10|tableClass=myCustomClass|tableStyle=width: 50%;}}
Event Data Format
The events must be stored in the `GlobalEvents` page in the following format:
{{EventSection
| title = Section Title
| dateHeader = Date
| titleHeader = Event Name
| descriptionHeader = Description
| newsHeader = News
| tableStyle = Table Style
| tableClass = Table Class
| events =
{{EventRow
| date = YYYY-MM-DD
| title = Event Title
| description = Event Description
}}
}}
Each `EventSection` can contain multiple `EventRow` entries. The `date`, `title`, and `description` fields are mandatory for each `EventRow`.
Features
- **Date Sorting:** Events are sorted by date in descending order, with year-only events placed before specific date events within the same year.
- **Link Preservation:** Links within titles and descriptions are preserved while standalone `|` characters are removed.
- **Customizable Display:** The number of rows, table class, and table style can be customized through module arguments.
Example
If the `GlobalEvents` page contains the following content:
{{EventSection
| title = June 2024
| dateHeader = Date
| titleHeader = Event Name
| descriptionHeader = Description
| newsHeader = Description
| tableStyle = width: 100%;
| tableClass = wikitable
| events =
{{EventRow
| date = 2024-06-29
| title = Future [[Cyraxx|goblin]] Rage
| description = vbn
}}
}}
Invoking the module will display a table with the event data formatted appropriately.
Code
Below is the Lua code for the `NewsList` module:
local p = {}
function p.generateTable(frame)
-- Define the page where the events are stored
local eventsPage = mw.title.new('GlobalEvents')
-- Get the content of the events page
local eventsContent = eventsPage and eventsPage:getContent() or ''
local events = {}
if eventsContent and eventsContent ~= '' then
-- Split the content into individual event blocks
for eventBlock in mw.text.gsplit(eventsContent, '{{EventRow', true) do
if eventBlock and eventBlock:find('|%s*date%s*=') then
local event = {}
-- Extract the date from the event block
event.date = mw.text.trim(eventBlock:match('|%s*date%s*=%s*(%d%d%d%d%-%d%d%-%d%d)') or eventBlock:match('|%s*date%s*=%s*(%d%d%d%d)') or '')
-- Extract the title from the event block, handling newlines correctly
event.title = eventBlock:match('|%s*title%s*=%s*(.-)%s*\n') or ''
-- Extract the description from the event block, handling newlines correctly
event.description = eventBlock:match('|%s*description%s*=%s*(.-)%s*}}') or ''
-- Function to clean standalone | characters but keep them inside links
local function cleanText(text)
local cleanedText = ""
local inLink = false
for i = 1, #text do
local char = text:sub(i, i)
if char == "[" and text:sub(i, i+1) == "[[" then
inLink = true
elseif char == "]" and text:sub(i-1, i) == "]]" then
inLink = false
end
if char == "|" and not inLink then
-- skip standalone |
else
cleanedText = cleanedText .. char
end
end
return cleanedText
end
event.title = cleanText(event.title)
event.description = cleanText(event.description)
if event.date ~= '' then
table.insert(events, event)
end
end
end
-- Sort the events first by year, then by month and day if available
table.sort(events, function(a, b)
local a_year, a_month, a_day = a.date:match("^(%d%d%d%d)%-(%d%d)%-(%d%d)$")
local b_year, b_month, b_day = b.date:match("^(%d%d%d%d)%-(%d%d)%-(%d%d)$")
a_year = a_year or a.date
b_year = b_year or b.date
if a_year ~= b_year then
return a_year > b_year
elseif a_month and b_month then
if a_month ~= b_month then
return a_month > b_month
else
return a_day > b_day
end
elseif a_month and not b_month then
return false -- Full date comes after year-only date within the same year
else
return true -- Year-only date comes before full date within the same year
end
end)
-- Determine the number of rows to display
local rows = tonumber(frame.args.rows) or #events
rows = math.min(rows, #events) -- Ensure rows does not exceed the number of available events
-- Set the default table class and style
local tableClass = frame.args.tableClass ~= 'wikitable' and (frame.args.tableClass or 'newstable') or 'newstable'
local tableStyle = frame.args.tableStyle or 'width: 100%; overflow-x: auto;'
-- Start building the result table
local result = '{| class="' .. tableClass .. '" style="' .. tableStyle .. '"\n'
result = result .. '! Date !! News\n'
-- Function to preprocess and replace the placeholder back to '|'
local function preprocessContent(text)
if not text then return '' end
return frame:preprocess(text)
end
-- Populate the table with event data
for i = 1, rows do
local event = events[i]
local date = mw.text.nowiki(event.date)
local title = preprocessContent(event.title) or ''
local description = preprocessContent(event.description) or ''
-- Fix link encoding
title = title:gsub("%%7C", "|")
description = description:gsub("%%7C", "|")
result = result .. '|-\n'
result = result .. '| ' .. date .. ' || ' .. title .. '<br>' .. description .. '\n'
end
-- Close the table
result = result .. '|}'
return result
else
return "No events found in GlobalEvents page."
end
end
return p