Module:NewsLanding/LatestNews/Top

From TwogPedia

Documentation for this module may be created at Module:NewsLanding/LatestNews/Top/doc

local getArgs = require('Module:Arguments').getArgs
local cargo = mw.ext.cargo

local p = {}

function p.main(frame)
	local args = getArgs(frame)
	local category = string.gsub(args.category, '_', ' ')
	local tables = 'News'
	local fields = '_pageName, date, image'
	local cargoArgs = {
		where = 'category HOLDS WITHIN "' .. args.category .. '"',
		orderBy = 'date DESC',
		limit = 2,
	}

	local results = cargo.query(tables, fields, cargoArgs)
	
	local container = mw.html.create('div'):attr('id', 'news-cat-top')
	
	if #results > 0 then
		for i = 1, #results do
			local result = results[i]
			
			local title = mw.html.create('div'):addClass('news-cat-title'):wikitext(mw.ext.displaytitle.get(result._pageName))
			local dateString = mw.ustring.gsub(result.date, "%s(AM|PM)$", "")

			-- Use os.date to convert the string to a table of date and time components
			local dateTimeTable = os.date("*t", os.time({year=string.sub(dateString, 1, 4), month=string.sub(dateString, 6, 7), day=string.sub(dateString, 9, 10), hour=tonumber(string.sub(dateString, 12, 13)) + ((string.sub(dateString, 22, 22) == "PM") and 12 or 0), min=string.sub(dateString, 15, 16), sec=string.sub(dateString, 18, 19)}))
			
			-- Use os.date again to format the date and time components into a desired format
			local formatedDate = os.date("%d %b %Y %H:%M", os.time(dateTimeTable))
			local date = mw.html.create('div'):addClass('news-cat-date'):wikitext(formatedDate)
			local info = mw.html.create('div'):addClass('news-cat-info'):node(title):node(date)
			local newsItem = mw.html.create('div'):addClass('news-cat-item'):wikitext('[[File:'.. result.image .. '|link=' .. result._pageName .. ']]'):node(info)
			container:node(newsItem)
		end
		return container
	end
end

return p