Diferencia entre revisiones de «Módulo:Formato texto»

De Hispanopedia
Sin resumen de edición
Supremo (discusión | contribs.)
m 34 revisiones importadas
 
(No se muestran 18 ediciones intermedias de 3 usuarios)
Línea 98: Línea 98:


-- Generar un enlace (implementación extendida)
-- Generar un enlace (implementación extendida)
-- @param enlace string En enlace a analizar
-- @param opciones mixed Las opciones, según lo siguiente:
--  @param opciones string La etiqueta (texto alternativo) del enlace
--  @param opciones table Tabla (array) de opciones, según lo siguiente
--    @param opciones['etiqueta'] string La etiqueta (texto alternativo) del enlace
--    @param opciones['namespace'] number El espacio de nombres en formato numérico (donde 6 es "Archivo:")
--    @param opciones['tamaño'] string El tamaño de la imagen en el caso de enlaces en espacio de nombres 6
--    @param opciones['calificativo'] string Texto adicional entre paréntesis, en tamaño de fuente más pequeño
--                                    (no aplicado a namespace 6)
--    @param opciones['debe existir'] bool Si es true, comprobar si el destino existe y devolver el enlace formateado,
--                                          de lo contrario, devolver solo la etiqueta (por defecto true).
--                                          Si es false o nil, devolver el enlace formateado aún si el destino no existe.
--                                          Si el espacio de nombres es "Archivo:", el parámetro siempre se considera true.
function z.enlazar2(enlace, opciones)
function z.enlazar2(enlace, opciones)
if enlace and opciones then
if enlace then
local etiqueta, namespace, tamano, calificativo, debeExistir
local etiqueta, namespace, tamano, borde, enlace2, pieImagen, debeExistir, solotexto


if type(opciones) == 'string' then
if type(opciones) == 'table' then
etiqueta = opciones
elseif type(opciones) == 'table' then
etiqueta = opciones['etiqueta'] or ''
etiqueta = opciones['etiqueta'] or ''
namespace = tonumber(opciones['namespace']) or 0
namespace = tonumber(opciones['namespace']) or 0
tamano = opciones['tamaño'] or '250px'
tamano = opciones['tamaño'] or '250px'
calificativo= opciones['calificativo']
borde = opciones['border'] or opciones['borde']
enlace2 = opciones['link']
pieImagen = opciones['pie']
debeExistir = opciones['debe existir'] or opciones['debeExistir'] or opciones['debeexistir']
debeExistir = opciones['debe existir'] or opciones['debeExistir'] or opciones['debeexistir']
solotexto = opciones['solotexto']
elseif type(opciones) == 'string' then
etiqueta = opciones
elseif type(opciones) == 'number' then
namespace = opciones
else
else
return
etiqueta = enlace
namespace = 0
end
end


local tituloObj = mw.title.new(enlace, namespace) or {}
local tituloObj = mw.title.new(enlace, namespace) or {}
if not debeExistir and not namespace == 6 then return '[[' .. tituloObj.fullText .. '|' .. etiqueta .. ']]' end
 
local titulo2Obj = {}
if enlace2 then titulo2Obj= mw.title.new(enlace2, 0) end
 
local resultado = {}
 
table.insert(resultado, tituloObj.fullText)
 
if not debeExistir and namespace ~= 6 and namespace ~= 14 then
if etiqueta then table.insert(resultado, etiqueta) end
if calificativo then return '[[' .. prefix .. table.concat(resultado, '|') .. ']]' .. ' <small>(' .. calificativo .. ')</small>' end
return '[[' .. table.concat(resultado, '|') .. ']]'
end
 
if tituloObj.exists or tituloObj.fileExists then
if tituloObj.exists or tituloObj.fileExists then
if namespace == 6 then return '[[' .. tituloObj.fullText .. '|' .. tamano .. '|' .. etiqueta .. ']]' end
-- Archivo:
if calificativo then return '[[' .. tituloObj.fullText .. '|' .. etiqueta .. ']]' .. ' <small>(' .. calificativo .. ')</small>' end
if namespace == 6 then
return '[[' .. tituloObj.fullText .. '|' .. etiqueta .. ']]'
if solotexto then
if etiqueta then table.insert(resultado, etiqueta) end
return '[[:' .. table.concat(resultado, '|') .. ']]'
end
if tamano then table.insert(resultado, tamano) end
if borde then table.insert(resultado, 'border') end
if titulo2Obj.exists then table.insert(resultado, 'link=' .. titulo2Obj.fullText) end
if etiqueta then table.insert(resultado, etiqueta) end
if pieImagen then return '[[' .. table.concat(resultado, '|') .. ']]' .. '<br>' .. pieImagen end
return '[[' .. table.concat(resultado, '|') .. ']]'
end
-- Categoría:
if namespace == 14 then
if solotexto then
if etiqueta then table.insert(resultado, etiqueta) end
return '[[:' .. table.concat(resultado, '|') .. ']]'
end
return '[[' .. tituloObj.fullText .. ']]'
end
 
-- El resto
if etiqueta then table.insert(resultado, etiqueta) end
return '[[' .. table.concat(resultado, '|') .. ']]'
 
end
end
return etiqueta
return etiqueta
end
end

Revisión actual - 10:49 25 oct 2023

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

local z = {}

function z.enCursivas(texto, ref)
	if ref and texto then
		return '<i>' .. texto .. '</i>' .. ref
	elseif texto then
	    return '<i>' .. texto .. '</i>'
	end
end

-- Adaptado de [[:en:Module:String2]], función sentence
function z.enMayusculas(texto)
	if not texto or texto == '' then
		return texto
	end
	
	-- [[hipótesis (método científico)|hipótesis]]
	
	if texto:find("^%[%[[^|]+|[^%]]+%]%]") then
		local b, c = texto:find("|%A*%a") 
		return string.sub(texto, 1, c-1) .. string.upper(string.sub(texto, c, c)) .. string.sub(texto, c+1)
	end
	
	local letterpos = texto:find('%a')
	if letterpos then
		local first = texto:sub(1, letterpos - 1)
		local letter = texto:sub(letterpos, letterpos)
		local rest = texto:sub(letterpos + 1)
		return first .. string.upper(letter) .. rest
	else
		return texto
	end
end

function z.enVersalitas(texto)
	if not texto or texto == '' then
		return texto
	end	
	return '<span style="font-variant:small-caps">' .. texto .. '</span>'
end

function z.enVariasLineas(lista)
	local resultado
	local copia={}
	require('Módulo:Tablas').insertarElementosConValor(lista, copia)
	
	-- Solo devolver algo si hay al menos un elemento
	if copia[1] then
		return table.concat(copia, '<br/>')
	end
end

function z.entreComillas(texto, ref)
	if not texto or texto == '' then
		return texto
	elseif ref and ref ~='' then
		return '«' .. texto .. '»' .. ref
	else 
	    return '«' .. texto .. '»'
	end
end

function z.entreParentesis(texto, calificativo)
	if not texto or texto == '' then
		return
	elseif calificativo and calificativo ~= '' then
	    return texto .. ' (' .. calificativo .. ')'
	else
		return texto
	end
end

function z.separadosPorComa(lista)
	local copia={}
	require('Módulo:Tablas').insertarElementosConValor(lista, copia)
	
	return table.concat(copia, ', ')
end

-- :: Generar un enlace
function z.enlazar(enlace, texto, calificativo)
	local resultado 
	
	if enlace and texto then
		resultado = '[[' .. enlace .. '|' .. texto .. ']]'
	elseif enlace then
		resultado = '[[' .. enlace .. ']]'
	else
		resultado = texto
	end
	
	if resultado and calificativo then
		return resultado .. ' <small>(' .. calificativo .. ')</small>'
	else
		return resultado
	end
end

-- Generar un enlace (implementación extendida)
function z.enlazar2(enlace, opciones)
	if enlace then
		local etiqueta, namespace, tamano, borde, enlace2, pieImagen, debeExistir, solotexto

		if type(opciones) == 'table' then
			etiqueta	= opciones['etiqueta'] or ''
			namespace	= tonumber(opciones['namespace']) or 0
			tamano		= opciones['tamaño'] or '250px'
			borde		= opciones['border'] or opciones['borde']
			enlace2		= opciones['link']
			pieImagen	= opciones['pie']
			debeExistir	= opciones['debe existir'] or opciones['debeExistir'] or opciones['debeexistir']
			solotexto	= opciones['solotexto']
		elseif type(opciones) == 'string' then
			etiqueta = opciones
		elseif type(opciones) == 'number' then
			namespace = opciones
		else
			etiqueta = enlace
			namespace = 0
		end

		local tituloObj = mw.title.new(enlace, namespace) or {}

		local titulo2Obj = {}
		if enlace2 then titulo2Obj= mw.title.new(enlace2, 0) end

		local resultado = {}

		table.insert(resultado, tituloObj.fullText)

		if not debeExistir and namespace ~= 6 and namespace ~= 14 then
			if etiqueta then table.insert(resultado, etiqueta) end
			if calificativo then return '[[' .. prefix .. table.concat(resultado, '|') .. ']]' .. ' <small>(' .. calificativo .. ')</small>' end
			return '[[' .. table.concat(resultado, '|') .. ']]'
		end

		if tituloObj.exists or tituloObj.fileExists then
			-- Archivo:
			if namespace == 6 then
				if solotexto then
					if etiqueta then table.insert(resultado, etiqueta) end
					return '[[:' .. table.concat(resultado, '|') .. ']]'
				end
				
				if tamano then table.insert(resultado, tamano) end
				if borde then table.insert(resultado, 'border') end
				if titulo2Obj.exists then table.insert(resultado, 'link=' .. titulo2Obj.fullText) end
				if etiqueta then table.insert(resultado, etiqueta) end
				if pieImagen then return '[[' .. table.concat(resultado, '|') .. ']]' .. '<br>' .. pieImagen end
				return '[[' .. table.concat(resultado, '|') .. ']]'
			end
			
			-- Categoría:
			if namespace == 14 then
				if solotexto then
					if etiqueta then table.insert(resultado, etiqueta) end
					return '[[:' .. table.concat(resultado, '|') .. ']]'
				end
				return '[[' .. tituloObj.fullText .. ']]'
			end

			-- El resto
			if etiqueta then table.insert(resultado, etiqueta) end
			return '[[' .. table.concat(resultado, '|') .. ']]'

		end

		return etiqueta
	end
end

function z.llamadaDesdeUnaPlantilla(frame)
	local args = frame.args
	
	if args['tipo argumento'] == 'tabla' then
		local tabla = {args[2], args[3], args[4]}
		return z[args[1]](tabla)
	else
		return z[args[1]](args[2], args[3], args[4])
	end
end

return z