Módulo:Centrar

De Wikisource, la biblioteca libre.
Ir a la navegación Ir a la búsqueda
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.

-- módulo para plantillas de formato
local p = {}
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,['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}

function p.centrar( frame ) -- función principal para llamar desde {{centrar}}
    local argus = {}
	for k,v in pairs(frame.args) do
		argus[k] = v 
	end
	for k,v in pairs(frame:getParent().args) do -- crea una tabla con los parámetros incluídos en la plantilla, y elimina parámetros vacíos
		argus[k] = v 
	end

    for k,v in pairs(altparams) do --parámetros con nobmres alternativos.
	if argus[k] and not argus[v] then
		argus[v] = argus[k]
	end
    end

    local html = mw.html.create() -- cuerpo principal de la plantilla
    local div = html:tag(argus['tag'] or 'div')
    
    div:css('text-align', argus['alinear'])
    if argus['ancho'] then
    	div:css('width', argus['ancho'])
     end
    if argus['float'] then
    	div:css('float', argus['float'])	
    end
    
    -- sección de parámetros
    if argus['fs'] and tamFuente[argus['fs']]~=nil then
    	div:css('font-size',tamFuente[argus['fs']]) --tamaños usando el nombre de las plantillas de tamaño de fuente
    elseif argus['fs'] then
    	div:css('font-size',argus['fs']) -- tamaño personalizado
    end
    if argus['lh'] then
    	div:css('line-height',argus['lh'])
    end
    if argus['sp'] then
    	div:css('letter-spacing',argus['sp'])
    end
    if argus['color']  then
		if mw.ustring.sub( argus['color'], 0, 1 ) == '#' then
			color = argus['color']	
		else
			color = frame:expandTemplate{ title = 'RGB', args = { argus['color'] } }
		end
		
		color = mw.ustring.gsub(color, '#', '#')
		
		div:css('color',color)
	end
	if argus['fondo']  then
		if mw.ustring.sub( argus['fondo'], 0, 1 ) == '#' then
			fondo = argus['fondo']	
		else
			fondo = frame:expandTemplate{ title = 'RGB', args = { argus['fondo'] } }
		end
		
		fondo = mw.ustring.gsub(fondo, '#', '#')
		
		div:css('background',fondo)
	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
			div:css('text-transform',textTransform[argus[k]]) --opciones transformación de texto
		elseif fontVariant[argus[k]] ~= nil then  -- opcion variante de fuente (small caps)
			div:css('font-variant',fontVariant[argus[k]])
		elseif fontFamily[argus[k]] ~= nil then  -- opcion familia de fuentes
			div:css('font-family',fontFamily[argus[k]])
		elseif tamFuente[argus[k]] ~= nil then	-- opciones de tamaño de fuente sin usar el parámetro fs
			div:css('font-size',tamFuente[argus[k]])
		elseif fontWeight[argus[k]] ~= nil then	-- opcion de peso de fuente (negrita)
			div:css('font-weight',fontWeight[argus[k]])
		elseif fontStyle[argus[k]] ~= nil then	-- opcion de estilo de fuente (cursiva)
			div:css('font-style',fontStyle[argus[k]])		
		elseif textDecoration[argus[k]] ~= nil then	-- opcion de decoracion de texto (subrayado)
			div:css('text-decoration',textDecoration[argus[k]])		
		end
	end
    end
	-- div:css('clear','both') — No utilidad aparente?

    --texto
    div:wikitext(argus[1])
    --otros parámetros
    if argus['plantilla'] == 'derecha' then --para reutilizar código en {{derecha}}
    	div:css('margin-right', argus['margen'] or argus[2])
	end
	if argus['float'] == 'right' then --asimilar comportamieto de {{float right}} y {{flotador derecha}}
		div:addClass('ws-flotador-d')
		div:css('margin-right', argus['margen'] or argus[2])
		div:css('margin-top', argus[3])
		div:css('margin-bottom', argus[3])
		div:css('margin-left', argus[4])
	end	
	if argus['float'] == 'left' then --asimilar comportamieto de {{float left}} y {{flotador izquierda}}
		div:addClass('ws-flotador-i')
		div:css('margin-top', argus[2])
		div:css('margin-bottom', argus[2])
		div:css('margin-right', argus[3])
	end	
	if argus['colgante'] == 's' then --asimilar comportamieto de {{sangría colgante}}
		div:addClass('ws-sangria-colgante')
		local margen = argus['margen'] or argus[2] or 'NaN'
		if tonumber(margen:sub(1,1)) then
			div:css('margin-left', margen)
			div:css('text-indent', '-'..margen)
		end
	end
	if argus['bloque'] == 'centro' then --asimilar comportamiento de  {{bloque centro}}
		div:addClass('ws-bloque-centro')
		if argus['max-width'] then
			div:css('max-width',argus['max-width'])
		end
	elseif argus['bloque'] == 'derecha' then --asimilar comportamiento de  {{bloque derecha}}
		div:addClass('ws-bloque-derecha')	
		div:css('margin-right', argus['margen'])
		div:css('text-align', argus['alinear'])
		if argus['max-width'] then
			div:css('max-width',argus['max-width'])
		end
		html:tag('div'):css('clear','right')
		
	elseif argus['bloque'] == 'izquierda' then --asimilar comportamiento de  {{bloque izquierda}}
		div:addClass('ws-bloque-izquierda')
		div:css('margin-left', argus['margen'] or argus[2])
		if argus['max-width'] then
			div:css('max-width',argus['max-width'])
		end
	end
	if argus['estilo'] then  --estilo personalizado, al final para poder sobreescribir todo
		div:cssText(argus['estilo'])
	end
	-- anclaje
	if argus['anclaje']  then
		div:attr('id', argus['anclaje'])
		div:addClass('anclaje')
	end
	
	if argus['clase'] then
    	div:addClass(argus['clase'])	
    end  	
	
	for k,v in pairs(argus) do
		if (type(k) ~= 'number') and (params[k]==nil) and (altparams[k]==nil) then
			html:wikitext('[[Categoría:Wikisource:Artículos que usan parámetros no reconocidos en el módulo Centrar]]')
		end
	end
	
    return tostring(html)
end

function p.open( frame ) -- función principal para llamar desde {{bloque centro/c}} y similares
    local argus = {}
	local clases = {}
	for k,v in pairs(frame.args) do
		argus[k] = v 
	end
	for k,v in pairs(frame:getParent().args) do -- crea una tabla con los parámetros incluídos en la plantilla, y elimina parámetros vacíos
		argus[k] = v 
	end

    for k,v in pairs(altparams) do --parámetros con nobmres alternativos.
	if argus[k] and not argus[v] then
		argus[v] = argus[k]
	end
    end

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

    --otros parámetros
    if argus['plantilla'] == 'derecha' then --para reutilizar código en {{derecha}}
    	div=div..'margin-right:'..(argus['margen'] or argus[1] or '')..';'
	end
	if argus['float'] == 'right' then --asimilar comportamieto de {{float right}} y {{flotador derecha}}
		table.insert(clases,'ws-flotador-d')
		div=div..'margin-right:'..( argus['margen'] or argus[1] or '')..';'
		div=div..'margin-top:'..( argus[2] or '')..';'
		div=div..'margin-bottom:'..( argus[2] or '')..';'
		div=div..'margin-left:'..( argus[3] or '')..';'
	end	
	if argus['float'] == 'left' then --asimilar comportamieto de {{float left}} y {{flotador izquierda}}
		table.insert(clases,'ws-flotador-i')
		div=div..'margin-top:'..(argus[1] or '')..';'
		div=div..'margin-bottom:'..(argus[1] or '')..';'
		div=div..'margin-right:'..(argus[2] or '')..';'
	end	
	if argus['colgante'] == 's' then --asimilar comportamieto de {{sangría colgante}}
		table.insert(clases,'ws-sangria-colgante')
		local margen = argus['margen'] or argus[1] or 'NaN'
		if tonumber(margen:sub(1,1)) then
			div=div..'margin-left:'..margen..';'
			div=div..'text-indent:'..'-'..margen..';'
		end
	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[1] or 'NaN'
		if tonumber(margen:sub(1,1)) then
			div=div..'margin-left:'..margen..';'
		end
	end
	if argus['bloque'] == 'centro' then --asimilar comportamiento de  {{bloque centro}}
		table.insert(clases,'ws-bloque-centro')
		if argus['max-width'] then
			div=div..'max-width:'..argus['max-width']..';'
		end
	elseif argus['bloque'] == 'derecha' then --asimilar comportamiento de  {{bloque derecha}}
		table.insert(clases,'ws-bloque-derecha')
		if argus['margen'] then
			div=div..'margin-right:'..(argus['margen'] or '')..';'
		end
		if argus['alinear'] then
			div=div..'text-align:'..(argus['alinear'])..';'
		end
		if argus['max-width'] then
			div=div..'max-width:'..(argus['max-width'])..';'
		end
		
		--html:tag('div'):css('clear','right') -- ATENCION!!!
		
	elseif argus['bloque'] == 'izquierda' then --asimilar comportamiento de  {{bloque izquierda}}
		table.insert(clases,'ws-bloque-izquierda')
		if (argus['margen'] or argus[1]) then
			div=div..'margin-left:'..(argus['margen'] or argus[1])..';'
		end
		if argus['max-width'] then
			div=div..'max-width:'..(argus['max-width'])..';'
		end
	end
	if argus['estilo'] then  --estilo personalizado, al final para poder sobreescribir todo
		div=div..argus['estilo']..';'
	end
	div=div..'" '
	-- anclaje
	if argus['anclaje']  then
		div=div..'id="'..argus['anclaje']..'"'
		div=div..'class="anclaje" '
	end
	--clases 
	div=div..'class="'
	for k,c in ipairs(clases) do
		div = div..c..' '
	end
	if argus['clase'] then
    	div=div..argus['clase']
	end  
	div=div..'"'
	--
    div=div..'>'
    mw.log(div)
    return tostring(div)
end

return p