Module:Selected current events
Appearance
| This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing. |
| Editing of this module by new or unregistered users is currently disabled. See the protection policy and protection log for more details. If you cannot edit this module and you wish to make a change, you can submit an edit request, discuss changes on the talk page, request unprotection, log in, or create an account. |
Usage
{{#invoke:Selected current events|main}}
Arguments come from the parent template {{Transclude selected current events}}; see documentation there for details.
See also
function setCleanArgs(argsTable)
local cleanArgs = {}
for key, val in pairs(argsTable) do
if type(val) == 'string' then
val = val:match('^%s*(.-)%s*$')
if val ~= '' then
cleanArgs[key] = val
end
else
cleanArgs[key] = val
end
end
return cleanArgs
end
-- Get current events for a "YYYY Month" date. Returns a table of list items.
function getCurrentEvents(date, pattern)
local frame = mw.getCurrentFrame()
local raw = mw.text.killMarkers( frame:expandTemplate{ title="Portal:Current events/" .. date } )
local lines = mw.text.split( raw , '\n')
local items = {}
for i, v in ipairs(lines) do
if string.sub( v, 0, 2 ) == '**' and mw.ustring.find(v, pattern) then
local item = mw.ustring.gsub(v, "%*+","*")
table.insert(items, item)
end
end
return items
end
local p = {}
p.main = function(frame)
local parent = frame.getParent(frame)
local parentArgs = parent.args
local lang = mw.language.new('en')
local keywords = {}
local ii = 1
while parentArgs[ii] do
table.insert(keywords, parentArgs[ii])
end
if not keywords[1] then
return error("Keywords not set")
end
local allItems = {}
local daysAgo = 0
while daysAgo < 4 do
local dailyItems = getCurrentEvents(lang:formatDate('Y F j', 'now - '..daysAgo..' days'), keywords[1])
for i, item in ipairs(dailyItems) do
table.insert(allItems, item)
end
daysAgo = daysAgo + 1
end
if #allItems < 1 then
return 'No recent news'
end
return table.concat(allItems, '\n')
end
return p