Módulo:Centrar

De Wikisource, la biblioteca libre.
Documentación del módulo


Uso

En: {{centrar}}, {{derecha}}, {{Flotador izquierda}}, {{Flotador derecha}}, {{sangría colgante}}, {{bloque centro}}, {{bloque derecha}}, {{bloque izquierda}}
Esta documentación está transcluida desde Módulo:Centrar/doc.
Los editores pueden experimentar en la zona de pruebas de este módulo.
Por favor, añade las categorías e interwikis a la subpágina de documentación. Subpáginas de este módulo.

require('strict')

-- módulo para plantillas de formato
local p = {}
local make_style_string = require('Module:Optional style').make_style_string

local tamFuente = { --tamaños de letra segun nombre de las plantillas
['xx-menor']="58%" ,
['x-menor']="69%" ,
['menor']="83%" ,
['grande']="120%" ,
['x-grande']="144%" ,
['xx-grande']="182%" ,
['xxx-grande']="207%" ,
['xxxx-grande']="249%" ,
['xxxxx-grande']="283%" ,
['xxxxxx-grande']="323%", 
['sm']="smaller"
}
local textTransform = { 
	['lc']='lowercase' , 
	['uc']='uppercase' ,
	['cap']='capitalize' 
}

local fontVariant = {
['sc']='small-caps',
['may']='small-caps',
['asc']='all-small-caps',
['all-small-caps']='all-small-caps'
}

local fontFamily = { --familia genérica de fuentes
['monospace']='monospace', 
['serif']='serif', 
['sans-serif']='sans-serif', 
['cursive']='cursive', 
['fantasy']='fantasy'
}
local fontWeight = { --negrita
['negrita']	= 'bold',
['bold'] = 'bold',
}
local fontStyle = { --cursivas
['cursiva'] = 'italic',
['itálica'] = 'italic',
['italic'] = 'italic',
['normal'] = 'normal', -- sin cursiva
}
local textDecoration = { --subrayado
['subrayado'] = 'underline',
['underline'] = 'underline',
}
local altparams= {['font-size']='fs', ['tamaño']='fs', 
	['line-height']='lh', ['interlineado']='lh', 
	['letter-spacing']='sp', ['espaciado']='sp',
	['offset']='margen',
	['id']='anclaje',
	['width']='ancho',
	['align'] = 'alinear',
	['style'] = 'estilo',
	['class'] = 'clase'
}
local params ={['tag']=1, ['open']=1, ['alinear']=1,['ancho']=1,['float']=1,['fs']=1,['lh']=1,['sp']=1,['margen']=1,['max-width']=1,['anclaje']=1,
	['ancho']=1,['estilo']=1,['color']=1,['fondo']=1,['plantilla']=1,['colgante']=1,['bloque']=1, ['clase']=1}

local function argus_estilo(argus)
	-- Función que procesa la lista de parámetros (argus) y 
	-- regresa una table con todos los estilos CSS

	local css = {}
	local clases = {}
	
	css['text-align'] = argus.alinear
	css['width'] = argus.ancho
	css['float'] = argus.float
    
    -- sección de parámetros
    ----  tamaño
    if argus.fs and tamFuente[argus.fs]~=nil then
    	css['font-size'] = tamFuente[argus.fs] -- tamaños usando el nombre de las plantillas de tamaño de fuente
    elseif argus.fs then
    	css['font-size'] = argus.fs -- tamaño personalizado
    end
    
    if argus.lh then
    	css['line-height'] = argus.lh
    end
    if argus.sp then
    	css['letter-spacing'] = argus.sp
    end
	
    -- sección de opciones
    for k,v in pairs(argus) do -- opciones
	if type(k) == 'number' and k~= 1  then
		if textTransform[argus[k]] ~= nil then
			css['text-transform'] = textTransform[argus[k]] --opciones transformación de texto
		elseif fontVariant[argus[k]] ~= nil then  -- opcion variante de fuente (small caps)
			css['font-variant'] = fontVariant[argus[k]]
		elseif fontFamily[argus[k]] ~= nil then  -- opcion familia de fuentes
			css['font-family'] = fontFamily[argus[k]]
		elseif tamFuente[argus[k]] ~= nil then	-- opciones de tamaño de fuente sin usar el parámetro fs
			css['font-size'] = tamFuente[argus[k]]
		elseif fontWeight[argus[k]] ~= nil then	-- opcion de peso de fuente (negrita)
			css['font-weight'] = fontWeight[argus[k]]
		elseif fontStyle[argus[k]] ~= nil then	-- opcion de estilo de fuente (cursiva)
			css['font-style'] = fontStyle[argus[k]]		
		elseif textDecoration[argus[k]] ~= nil then	-- opcion de decoracion de texto (subrayado)
			css['text-decoration'] = textDecoration[argus[k]]		
		end
	end
    end
	-- css['clear'] = 'both' — No utilidad aparente?

    --otros parámetros
    if argus.plantilla == 'derecha' then --para reutilizar código en {{derecha}}
    	css['margin-right'] =  argus.margen or argus[2]
	end
	if argus.float == 'right' then --asimilar comportamieto de {{float right}} y {{flotador derecha}}
		table.insert(clases, 'ws-flotador-d')
		css['margin-right'] =  argus.margen or argus[2]
		css['margin-top'] =  argus[3]
		css['margin-bottom'] =  argus[3]
		css['margin-left'] =  argus[4]
	end	
	if argus.float == 'left' then --asimilar comportamieto de {{float left}} y {{flotador izquierda}}
		table.insert(clases, 'ws-flotador-i')
		css['margin-top'] =  argus[2]
		css['margin-bottom'] =  argus[2]
		css['margin-right'] =  argus[3]
	end	
	if argus.colgante == 's' then --asimilar comportamieto de {{sangría colgante}}
		table.insert(clases, 'ws-sangria-colgante')
		local margen = argus.margen or argus[2] or 'NaN'
		if tonumber(margen:sub(1,1)) then
			css['margin-left'] =  margen
			css['text-indent'] =  '-'..margen
		end
	if argus['colgante'] == 'm' then --asimilar comportamieto de {{sangría colgante/m}}
		table.insert(clases,'ws-sangria-colgante-m')
		local margen = argus['margen'] or argus[2] or 'NaN'
		if tonumber(margen:sub(1,1)) then
			css['margin-left'] = margen
		end
	end
	end
	if argus.bloque == 'centro' then --asimilar comportamiento de  {{bloque centro}}
		table.insert(clases, 'ws-bloque-centro')
		if argus['max-width'] then
			css['max-width'] = argus['max-width']
		end
	elseif argus.bloque == 'derecha' then --asimilar comportamiento de  {{bloque derecha}}
		table.insert(clases, 'ws-bloque-derecha')	
		css['margin-right'] =  argus.margen
		css['text-align'] =  argus.alinear
		if argus['max-width'] then
			css['max-width'] = argus['max-width']
		end

	elseif argus.bloque == 'izquierda' then --asimilar comportamiento de  {{bloque izquierda}}
		table.insert(clases, 'ws-bloque-izquierda')
		css['margin-left'] =  argus.margen or argus[2]
		if argus['max-width'] then
			css['max-width'] = argus['max-width']
		end
	end
	if argus.estilo then  --estilo personalizado, al final para poder sobreescribir todo
		css['estilo'] = argus.estilo
	end
	return css, clases
end
local function color_string(frame, color)
	if mw.ustring.sub( color, 0, 1 ) == '#' then
		color = color
	else
		color = frame:expandTemplate{ title = 'RGB', args = { color } }
	end
	color = mw.ustring.gsub(color, '#', '#')
	return color
end
-- función principal para llamar desde {{centrar}} o similar
function p._centrar(frame, argus ) 
	
    local texto = argus[1]
    local id = argus.anclaje or ''
    local css, clases = argus_estilo(argus)
	local tag = argus.tag or 'div'
	
	-- manejo especial para el color
	if argus.color  then
		css['color'] = color_string(frame, argus.color)
	end
	if argus.fondo  then
		css['background'] = color_string(frame, argus.fondo)
	end
	-- anclaje (ID)
	if id ~= '' then
		table.insert(clases, 'anclaje')
	end
	-- clases CSS adicionales
	if argus.clase then
    	table.insert(clases, argus.clase)	
    end  	
	
	-- inicio de tag
	local contenido = '<'..tag..' '
	-- atributos HTML
	local atributos = {
		'id="'..id..'"',
		'class="'..table.concat(clases, ' ')..'"',
		make_style_string(css)
	}
	-- agregar atributos
	contenido = contenido..table.concat(atributos, ' ')..'>'
	
	-- agregar texto y cerrar tag si no estamos en {{plantilla/c}}
	if not argus.open then
		contenido = contenido .. (texto or '') .. '</'..tag..'>'
		if argus.bloque == 'derecha' then -- fix para {{bloque derecha}}
			contenido = contenido..'<br style="clear:right"/>'
		end
	end
	-- categorías de mantenimiento
	for k,v in pairs(argus) do
		if (type(k) ~= 'number') and (params[k]==nil) and (altparams[k]==nil) then
			contenido = contenido .. '[[Categoría:Wikisource:Artículos que usan parámetros no reconocidos en el módulo Centrar]]'
		end
	end
	return contenido
end


-- metafunciones
local function main(frame, argus)
	--parámetros con nobmres alternativos
	for k,v in pairs(altparams) do 
		if argus[k] and not argus[v] then
			argus[v] = argus[k]
		end
    end
    return p._centrar(frame, argus)
end
-- función para plantillas cerradas
function p.centrar(frame)
	local argus = require('Module:Arguments').getArgs(frame)
    return main(frame, argus)
end
-- función para plantillas abiertas
function p.open(frame)
	local argus = require('Module:Arguments').getArgs(frame)
	mw.logObject(argus[1])
	argus[12] = argus[1]
	argus.open = true
	mw.logObject(argus)
    return main(frame, argus)
end
return p