Jump to content

Module:Selected current events

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Evad37 (talk | contribs) at 15:41, 22 May 2018 (Start. Based on WT:WPPORT#Alternative to Wikinews). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

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)
	local frame = mw.getCurrentFrame()
	local raw = mw.text.killMarkers( frame:expandTemplate{ title="Portal:Current events/Month Inclusion", args={date} } )
	local items = string.gsub( raw, pattern, repl, n )
	local lines = mw.text.split( raw , '\n')
	local items = {}
	for i, v in ipairs(lines) do
		if string.sub( v, 0, 2 ) == '**' then
			local item = mw.ustring.gsub(v, "%*+","*")
			table.insert(items, item)
		end
	end
	return items
end

-- TODO: Filter items to those that match keywords/matches
function filterEvents(items, keywords)
	local filteredItems = {}
	for i, item in ipairs(items) do
		local done = false
		for k, patt in pairs(keywords) do
			if not done and mw.ustring.find(v, patt) then
				table.insert(filteredItems, item)
			end
		end
	end
	return filteredItems
end


local p = {}

p.main = function(frame)
	local parent = frame.getParent(frame)
	local parentArgs = parent.args
	local lang = mw.language.new('en')
	local thisMonth = lang:formatDate('Y F')
	local lastMonth = lang:formatDate('Y F', 'now - 1 month')

	-- TODO: get keywords from args
	local keywords = {"Japan"}

	local currentEventsThisMonth = filterEvents(getCurrentEvents(thisMonth), keywords)
	local currentEventsLastMonth = filterEvents(getCurrentEvents(lastMonth), keowords)

	if not currentEventsThisMonth and not currentEventsLastMonth then
		return 'No recent news'
	end
	return currentEventsThisMonth.reverse().concat('/n') .. currentEventsLastMonth.reverse().concat('/n')

end

return p