Module:Main Page redesign

From A Wiki of Ice and Fire
Jump to: navigation, search

Documentation for this module may be created at Module:Main Page redesign/doc

local p = {}
local data = mw.loadData( 'Module:Main Page redesign/data' )

local function unixDay( frame )
	local supplied = tonumber( frame.args.day )
	if supplied then
		return math.floor( supplied )
	end
	return tonumber( frame:preprocess( '{{unix day}}' ) ) or 0
end

local function entryAt( list, index )
	return list and ( list[index] or list[tostring( index )] ) or nil
end

local function creditMarkup( item )
	local imageFile = item.imageFile or item.file
	local credit = '[[:File:' .. imageFile .. '|Image details]]'
	if item.artist then
		credit = 'Art by ' .. item.artist .. ( item.creditSuffix or '' ) .. ' · ' .. credit
	end
	if item.artistCategory and item.artistCategory ~= '' and not item.artistCategory:find( '�', 1, true ) then
		local category = item.artistCategory:gsub( '^Category:', '' )
		credit = credit .. ' · [[:Category:' .. category .. '|More by this artist]]'
	end
	return credit
end

local function imageMarkup( item, width, className, linkTarget )
	local imageFile = item and ( item.imageFile or item.file )
	if not imageFile then
		return ''
	end
	local link = linkTarget or item.imageSubject or item.article or ''
	local alt = item.imageAlt or item.title or item.speakerLabel or ''
	return '<div class="' .. className .. '">[[File:' .. imageFile .. '|' .. width ..
		'px|link=' .. link .. '|alt=' .. alt .. ']]</div>'
end

function p.feature( frame )
	local index = ( unixDay( frame ) % 61 ) + 1
	local item = entryAt( data.articles, index )
	if not item then
		return ''
	end
	local fragment = item.articleFragment or ''
	if fragment ~= '' and fragment:sub( 1, 1 ) ~= '#' then
		fragment = '#' .. fragment
	end
	local target = item.article .. fragment
	local image = item.imageFile and imageMarkup( item, 400, 'mp-redesign__feature-art', target ) or ''
	local media = ''
	local withImage = ''
	if image ~= '' then
		withImage = ' mp-redesign__feature--with-image'
		media = '<div class="mp-redesign__feature-media">' .. image ..
			'<div class="mp-redesign__feature-credit">' .. creditMarkup( item ) .. '</div></div>'
	end
	return '<div class="mp-redesign__feature' .. withImage .. '" data-feature-slot="' .. index .. '">' ..
		'<div class="mp-redesign__feature-copy">' ..
		'<p class="mp-redesign__eyebrow">' .. ( item.eyebrow or 'Selected reading' ) .. '</p>' ..
		'<h2>[[' .. target .. '|' .. item.title .. ']]</h2>' .. media .. '<p>' .. item.teaser .. '</p>' ..
		'<p>[[' .. target .. '|Read the article →]]</p></div></div>'
end

function p.hero( frame )
	local day = unixDay( frame )
	local hero = data.hero or {}
	local pool = {}
	for _, item in ipairs( data.artPool or {} ) do
		if item.enabled ~= false then
			table.insert( pool, item )
		end
	end
	if #pool == 0 or hero.mode == 'pending' then
		return ''
	end
	local item
	if hero.mode == 'featured' then
		local article = entryAt( data.articles, ( day % 61 ) + 1 )
		item = article and entryAt( data.artByKey, article.heroKey ) or nil
		if not item then
			return ''
		end
	elseif hero.mode ~= 'independent' then
		return ''
	end
	if hero.mode == 'independent' then
		local period = math.max( 1, tonumber( hero.artPeriodDays ) or 1 )
		item = pool[( math.floor( day / period ) % #pool ) + 1]
	end
	return '<div class="mp-redesign__hero-art" data-hero-key="' .. ( item.key or '' ) .. '" role="group">' ..
		'<div class="mp-redesign__hero-media">[[File:' .. ( item.imageFile or item.file ) ..
		'|1280px|link=' .. item.article .. '|alt=' .. ( item.imageAlt or item.title or '' ) .. ']]</div>' ..
		'<div class="mp-redesign__hero-caption"><h2 id="mp-redesign-title" class="mp-redesign__hero-title">[[' .. item.article .. '|' ..
		item.title .. ']]</h2><div class="mp-redesign__hero-credit">' .. creditMarkup( item ) ..
		'</div></div></div>'
end

local function quoteImageIsCurrent( frame, item, page )
	if not item.imageFile or not item.sourceRevision then
		return false
	end
	local latest = tonumber( frame:preprocess( '{{REVISIONID:' .. page .. '}}' ) )
	return latest and latest == tonumber( item.sourceRevision )
end

function p.quotes( frame )
	local day = unixDay( frame )
	local indexes = { ( day % 280 ) + 1, ( ( day + 93 ) % 280 ) + 1 }
	local rendered = {}
	for _, index in ipairs( indexes ) do
		local page = 'Feature quote/' .. index
		local item = entryAt( data.quotations, index ) or {}
		local image = quoteImageIsCurrent( frame, item, page ) and
			imageMarkup( item, 400, 'mp-redesign__quote-art' ) or ''
		local noImage = image == '' and ' mp-redesign__quote--text-only' or ''
		local credit = image ~= '' and
			'<div class="mp-redesign__quote-credit">' .. creditMarkup( item ) .. '</div>' or ''
		table.insert( rendered, '<div class="mp-redesign__quote' .. noImage .. '" data-quote-slot="' ..
			index .. '">' .. image .. '<div><blockquote>' .. frame:expandTemplate{ title = ':' .. page } .. '</blockquote>' ..
			'<p>[[' .. page .. '|Quotation & source →]]</p></div>' .. credit .. '</div>' )
	end
	return table.concat( rendered )
end

return p