Módulo:Índice

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


Uso

Este módulo intenta unificar distintas plantillas para la generación automática y estandarizada de índices alfabéticos.

{{#invoke:Índice|toc}}
Esta documentación está transcluida desde Módulo:Índice/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.

local p = {}

local function getArgNums(prefix, args)
    -- Returns a table containing the numbers of the arguments that exist for the specified prefix. For example, if the 
	-- prefix was 'data', and 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
    local nums = {}
    for k, v in pairs(args) do
        local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
        if num then table.insert(nums, tonumber(num)) end
    end
    table.sort(nums)
    return nums
end

function p.toc( frame ) 
    local argus = {}
	for k,v in pairs(frame:getParent().args) do -- crea una tabla con los parámetros incluídos en la plantilla
		if type(k) ~= 'number' then
			argus[mw.ustring.upper(k)] = v -- todos los parámetros en mayúsculas por defecto: 
		end
	end

    local html = mw.html.create()
    local tabla = html:tag('div'):addClass('plainlinks hlist'):cssText('display:table;text-align:center;zoom:1;padding:7px;border:1px solid #a2a9b1;background-color:#f8f9fa;font-size:95%')
    local titulo = tabla:tag('div'):attr('id','toctitle'):cssText('font-weight:bold')
    
    if argus['LADO'] then titulo:cssText('display:table-cell;vertical-align:middle;padding-right:0.5em') end
    
    titulo:wikitext(argus['NOMBRE'] or mw.message.new('toc'):plain())
    
    local tipos = {
    	['interno'] = '#',
    	['padre'] = '/',
    	['hijo'] = '../',
	}
	--preenlaces
	for i, n in ipairs(getArgNums('PRE', argus)) do
		tabla:wikitext('\n* [['..(tipos[argus['TIPO']] or argus['TIPO'] or '#')..(argus['PRE'..n..'ENLACE'] or argus['PRE'..n])..
			'|'..(argus['PRE'..n..'TEXTO'] or argus['PRE'..n])..']]')
	end
	if not argus['LINEAL'] then tabla:wikitext('\n') end --separados a menos que tenga el parámetro lineal
	
	--enlaces
	for i, c in ipairs({'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Ñ','O','P','Q','R','S','T','U','V','W','X','Y','Z'}) do
		if argus[c] and argus[c] ~= '' then 
			tabla:wikitext('\n* '..argus[c])
		elseif argus[c] then
			
		else
			tabla:wikitext('\n* [['..(tipos[argus['TIPO']] or argus['TIPO'] or '#')..c..'|'..c..']]')
		end
	end
	if not argus['LINEAL'] then tabla:wikitext('\n') end --separados a menos que tenga el parámetro lineal
	
	--posenlaces
	for i, n in ipairs(getArgNums('POS', argus)) do
		tabla:wikitext('\n* [['..(tipos[argus['TIPO']] or argus['TIPO'] or '#')..(argus['POS'..n..'ENLACE'] or argus['POS'..n])..
			'|'..(argus['POS'..n..'TEXTO'] or argus['POS'..n])..']]')
	end
    return tostring(html)
end 

return p