Module:NewsLanding/LatestNews: Difference between revisions

From TwogPedia
mNo edit summary
mNo edit summary
 
Line 24: Line 24:
             local img = result.image or 'News placeholder.png'
             local img = result.image or 'News placeholder.png'
local image = mw.html.create('div'):wikitext('[[File:' .. img .. '|600px]]')
local image = mw.html.create('div'):wikitext('[[File:' .. img .. '|600px|link=]]')
local title = mw.html.create('div'):addClass('news-latest-item-title'):wikitext(mw.ext.displaytitle.get(result._pageName))
local title = mw.html.create('div'):addClass('news-latest-item-title'):wikitext(mw.ext.displaytitle.get(result._pageName))
local description = getExcerpt(removeLinks(result.content))
local description = getExcerpt(removeLinks(result.content))

Latest revision as of 08:35, 11 March 2024

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

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

local p = {}

function p.main(frame)
	local args = getArgs(frame)
	
	local tables = 'News'
	local fields = '_pageName, date, image, category, game, content'
	local cargoArgs = {
		where = 'category HOLDS WITHIN "' .. args.category .. '"',
		orderBy = 'date DESC',
		limit = args.limit,
		offset = args.offset
	}
	local results = cargo.query(tables, fields, cargoArgs)
		
	if #results > 0 then
		local listStr = ''
		
		for i = 1, #results do
			local result = results[i]
			
            local img = result.image or 'News placeholder.png'
			local image = mw.html.create('div'):wikitext('[[File:' .. img .. '|600px|link=]]')
			local title = mw.html.create('div'):addClass('news-latest-item-title'):wikitext(mw.ext.displaytitle.get(result._pageName))
			local description = getExcerpt(removeLinks(result.content))
			
			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'):wikitext(formatedDate)
			local text = mw.html.create('div'):addClass('news-latest-item-text'):node(title):node(description):node(date)
			local div = mw.html.create('div'):addClass('news-latest-item'):node(image):node(text):node(mw.html.create('div'):addClass('link-overlay'):wikitext('[[File:Team_placeholder_light.png|1x1px|link=' .. result._pageName .. ']]'))
			
			listStr = listStr .. tostring(div)
		end
		
		return listStr
	end
end

function getExcerpt(text)
    local maxLength = 100
    if #text <= maxLength then
        return text
    else
        local nextSpace = text:find(" ", maxLength) -- find the next space after maxLength
        if nextSpace then
            return text:sub(1, nextSpace) .. '...'
        else
            return text:sub(1, maxLength) .. '...' -- return the first maxLength characters if no space is found
        end
    end
end

function removeLinks(text)
    local result = text:gsub("%[%[(.-)|.-%]%]", "%1"):gsub("%[%[(.-)%]%]", "%1"):gsub('People/', ''):gsub('Companies/', ''):gsub('Locations/', ''):gsub('News/', '')
    return result
end

return p