Jump to content
Menú alternativo
Alternar el menú de preferencias
Menú alternativo personal
No has accedido
Tu dirección IP será visible si haces alguna edición

La documentación para este módulo puede ser creada en Módulo:Portada/doc

-- Módulo:Portada — piezas dinámicas de la Página principal.
--   {{#invoke:Portada|rotar|pagina=Plantilla:Portada/Sabías que|n=3}}  -> n elementos que cambian cada día
--   {{#invoke:Portada|personaje|pagina=Plantilla:Portada/Personajes}}   -> personaje del día
--   {{#invoke:Portada|destacado}}                                       -> artículo de MediaWiki:Portada-destacado
-- La portada se refresca sola cada noche (cron que purga la Página principal).
local p = {}

local function lineas(pagina)
	local t = mw.title.new(pagina)
	local contenido = t and t:getContent()
	if not contenido then
		return {}
	end
	local out = {}
	for linea in mw.text.gsplit(contenido, '\n', true) do
		local item = linea:match('^%*%s*(.-)%s*$')
		if item and item ~= '' then
			table.insert(out, item)
		end
	end
	return out
end

local function dia()
	return math.floor(os.time() / 86400)
end

function p.rotar(frame)
	local items = lineas(frame.args.pagina or '')
	local n = tonumber(frame.args.n) or 1
	if #items == 0 then
		return "''Aún no hay elementos. Añádelos en [[" .. (frame.args.pagina or '') .. "]].''"
	end
	n = math.min(n, #items)
	local inicio = (dia() * n) % #items
	local out = {}
	for i = 0, n - 1 do
		table.insert(out, '* ' .. items[((inicio + i) % #items) + 1])
	end
	return frame:preprocess(table.concat(out, '\n'))
end

function p.personaje(frame)
	local pagina = frame.args.pagina or 'Plantilla:Portada/Personajes'
	local items = lineas(pagina)
	if #items == 0 then
		return "''Añade personajes en [[" .. pagina .. "]].''"
	end
	local partes = mw.text.split(items[(dia() % #items) + 1], '|', true)
	local nombre = mw.text.trim(partes[1] or '')
	local archivo = mw.text.trim(partes[2] or '')
	local desc = mw.text.trim(partes[3] or '')

	local caja = mw.html.create('div'):addClass('mc-pdd')
	local img = caja:tag('div'):addClass('mc-pdd__img')
	if archivo ~= '' and mw.title.new('Archivo:' .. archivo).file.exists then
		img:wikitext('[[Archivo:' .. archivo .. '|160x160px|link=' .. nombre .. '|alt=' .. nombre .. ']]')
	else
		img:tag('span'):addClass('mc-pdd__sin'):wikitext('🕵️')
	end
	local txt = caja:tag('div'):addClass('mc-pdd__txt')
	txt:tag('div'):addClass('mc-pdd__nombre'):wikitext('[[' .. nombre .. ']]')
	if desc ~= '' then
		txt:tag('div'):addClass('mc-pdd__desc'):wikitext(desc)
	end
	txt:tag('div'):addClass('mc-pdd__mas'):wikitext('[[' .. nombre .. '|Leer artículo →]]')
	return frame:preprocess(tostring(caja))
end

-- Primer párrafo legible de un artículo (sin plantillas, tablas, imágenes ni referencias)
local function intro(texto)
	texto = texto:gsub('<!%-%-.-%-%->', '')
	texto = texto:gsub('%b{}', function(s)
		local ini = s:sub(1, 2)
		if ini == '{{' or ini == '{|' then
			return ''
		end
		return s
	end)
	texto = texto:gsub('<ref[^>]-/>', ''):gsub('<ref.-</ref>', '')
	texto = texto:gsub('%b[]', function(s)
		local ns = s:match('^%[%[%s*([^:|%]]+):')
		if ns then
			ns = mw.ustring.lower(ns)
			if ns == 'archivo' or ns == 'file' or ns == 'imagen' or ns == 'image' or ns == 'categoría' or ns == 'category' then
				return ''
			end
		end
		return s
	end)
	texto = texto:gsub('__[A-ZÁÉÍÓÚ]+__', '')
	texto = texto:gsub('%s*%(%s*[,;]?%s*%)', '')
	for parrafo in mw.text.gsplit(texto, '\n%s*\n') do
		parrafo = mw.text.trim(parrafo)
		local c = parrafo:sub(1, 1)
		if parrafo ~= '' and c ~= '=' and c ~= '<' and c ~= '*' and c ~= '#' and c ~= ':' and c ~= ';' then
			if mw.ustring.len(parrafo) > 480 then
				local corto = mw.ustring.sub(parrafo, 1, 480)
				local corte = corto:match('^(.*[%.!%?])%s')
				parrafo = (corte or corto) .. ' …'
			end
			return parrafo
		end
	end
	return nil
end

function p.destacado(frame)
	local fuente = mw.title.new('MediaWiki:Portada-destacado')
	local nombre = fuente and fuente:getContent()
	nombre = nombre and mw.text.trim(nombre) or ''
	if nombre == '' then
		return "''Aún no hay artículo destacado. Los administradores lo eligen escribiendo su título en [[MediaWiki:Portada-destacado]].''"
	end
	local t = mw.title.new(nombre)
	if not t or not t.exists then
		return "''El artículo destacado, [[" .. nombre .. "]], todavía no existe. ¡Puedes crearlo!''"
	end
	if t.isRedirect and t.redirectTarget then
		t = t.redirectTarget
	end
	local contenido = t:getContent() or ''
	local imagen = contenido:match('|%s*imagen%s*=%s*([^|\n}]+)')
	imagen = imagen and mw.text.trim(imagen):gsub('^[Aa]rchivo:', ''):gsub('^[Ff]ile:', '')

	local caja = mw.html.create('div'):addClass('mc-dest')
	if imagen and imagen ~= '' and mw.title.new('Archivo:' .. imagen).file.exists then
		caja:tag('div'):addClass('mc-dest__img')
			:wikitext('[[Archivo:' .. imagen .. '|220px|link=' .. t.prefixedText .. '|alt=]]')
	end
	local txt = caja:tag('div'):addClass('mc-dest__txt')
	txt:tag('div'):addClass('mc-dest__titulo'):wikitext('[[' .. t.prefixedText .. ']]')
	txt:tag('div'):addClass('mc-dest__intro'):newline():wikitext(intro(contenido) or '')
	txt:tag('div'):addClass('mc-pdd__mas'):wikitext('[[' .. t.prefixedText .. '|Seguir leyendo →]]')
	return frame:preprocess(tostring(caja))
end

return p