Introducing: Representatives. swedishwin.com, chud.win, and soyzellig.party positions are vacant.


The new Community missions system is now live! Where anyone can propose a task, discuss nominations, or join the current mission. Community missions give editors a place to turn requests from the Janny noticeboard into focused projects. No mission can be too small

Module:News/previews

From Soyjak Wiki, the free ensoyclopedia
Jump to navigationJump to search

This module draws the issue cards on the front page. It is used by Template:News/previews. It takes no parameters, since everything is handled automatically from Module:News/data and Module:News/config.

What it does

  • It will list the newest listed issues, newest first, up to the previewLimit.
  • With previewLatestLarge, the most newest one will get a large card with the LATEST ISSUE label, while the rest are put as small cards in a grid.
  • Each card contains: the issue's image (if none is provided, then previewPlaceholder is used), its title, number and date, its summary, and a "Read the issue" link. The image and title also link to the issue.
  • With previewFiller, empty places up to previewLimit are filled with placeholder cards to keep the page styling consistent.
  • With no issues at all, it will put up a simple invite to the newsroom.
  • Below cards, an "all issues" link is placed, linking to the archive.

Notes

Titles and summaries are shown as plain text and are not formatted. ALL card texts are labels in the config.


local p = {}

local News = require('Module:News')
local cfg = News.cfg

local function card(state, issue, large)
	local rec = issue.rec
	local node = mw.html.create('div'):addClass('sn-news-card')
	if large then
		node:addClass('sn-news-card-latest') -- large = most recent issue
	end
	local image = rec.image or cfg.previewPlaceholder
	if image then
		node:tag('div')
			:addClass('sn-news-card-image')
			:addClass(not rec.image and 'sn-news-card-placeholder' or nil)
			:wikitext(
				'[[File:'
					.. image
					.. '|frameless|link='
					.. issue.page
					.. '|alt='
					.. (rec.image and rec.alt or '')
					.. ']]'
			)
	end

	local body = node:tag('div'):addClass('sn-news-card-body')
	if large then body:tag('div'):addClass('sn-news-card-label'):wikitext(News.text(state, 'latestCard')) end

	local number = News.numberText(state, issue)
	local date = News.formatDate(News.need('shortDateFormat'), issue.date)
	local title
	if rec.title then
		title = mw.text.nowiki(rec.title)
	else
		title = number .. ', ' .. date
		body:wikitext(News.errorText(state, 'missingField', { id = issue.id, field = 'title' }))
	end
	body:tag('div'):addClass('sn-news-card-title'):wikitext('[[' .. issue.page .. '|' .. title .. ']]')
	body:tag('div')
		:addClass('sn-news-card-meta')
		:wikitext(News.text(state, 'cardMeta', { number = number, date = date }))

	if rec.summary then
		body:tag('div'):addClass('sn-news-card-summary'):wikitext(mw.text.nowiki(rec.summary))
	else
		body:wikitext(News.errorText(state, 'missingField', { id = issue.id, field = 'summary' }))
	end

	body:tag('div')
		:addClass('sn-news-card-read')
		:wikitext('[[' .. issue.page .. '|' .. News.text(state, 'readIssue') .. ']]')
	return node
end

local function slot(state)
	local newsroom = News.pageTitle('newsroom')
	local node = mw.html.create('div'):addClass('sn-news-card'):addClass('sn-news-card-slot')
	if cfg.previewPlaceholder then
		node:tag('div')
			:addClass('sn-news-card-image')
			:addClass('sn-news-card-placeholder')
			:wikitext('[[File:' .. cfg.previewPlaceholder .. '|frameless|link=' .. newsroom .. '|alt=]]')
	end
	local body = node:tag('div'):addClass('sn-news-card-body')
	body:tag('div')
		:addClass('sn-news-card-title')
		:wikitext('[[' .. newsroom .. '|' .. News.text(state, 'slotTitle') .. ']]')
	body:tag('div'):addClass('sn-news-card-summary'):wikitext(News.text(state, 'slotText'))
	return node
end

function p.main(frame)
	local state = News.newState()
	local list = News.listed(News.today(frame))
	local root = mw.html.create('div'):addClass('sn-news-previews')

	if #list == 0 then
		root:tag('div'):addClass('sn-news-card'):addClass('sn-news-card-empty'):wikitext(
			News.text(state, 'noIssuesCard')
				.. ' [['
				.. News.pageTitle('newsroom')
				.. '|'
				.. News.text(state, 'noIssuesLink')
				.. ']]'
		)
	else
		local limit = News.need('previewLimit')
		local shown = math.min(limit, #list)
		local grid
		for i = 1, shown do
			if i == 1 and cfg.previewLatestLarge then
				root:node(card(state, list[i], true))
			else -- mode switch for a minimal ver
				grid = grid or root:tag('div'):addClass('sn-news-cards')
				grid:node(card(state, list[i], false))
			end
		end
		if cfg.previewFiller then
			for _ = shown + 1, limit do
				grid = grid or root:tag('div'):addClass('sn-news-cards')
				grid:node(slot(state))
			end
		end
	end

	root:tag('div')
		:addClass('sn-news-all')
		:wikitext('[[' .. News.pageTitle('archive') .. '|' .. News.text(state, 'allIssues') .. ']]')

	return News.finish(state, tostring(root))
end

return p