Módulo:ABNT

De Wikincat
Revisão de 11h24min de 9 de junho de 2021 por imported>Jaideraf
Ir para navegação Ir para pesquisar

A documentação para este módulo pode ser criada em Módulo:ABNT/doc

local p = {}

function p.CreateReference( frame )
    -- Define o alvo da referência: pode ser o primeiro parâmetro passado à predefinição, 
    -- o valor da query string "fullpagename" (via UrlGetParameters) ou o título da página.
    local target = frame:getParent().args[1] 
    
    if target == '' or target == nil then
        target = mw.uri.decode( frame:callParserFunction( '#urlget', 'fullpagename' ) ) 
    end
    if target == '' or target == nil then
        target = mw.title.getCurrentTitle().prefixedText
    end
    -- Alvo definido 
    -- Definir se há criador(es) para o início da referência
    local creator = frame:callParserFunction{ name = '#show', args = { target, '?Has creator#', link = 'none', valuesep = '; '} }
    -- Definir se há editores(es) para o início da referência
    local contributor = frame:callParserFunction{ name = '#show', args = { target, '?Has contributor#', link = 'none', valuesep = '; '} }
    -- Definir o título e o subtítulo 
    local title = frame:callParserFunction{ name = '#show', args = { target, '?Display title of#', link = 'none' } }
    title = string.gsub( title, '(:.*)', '<span style="font-weight: normal;">%1</span>')
    -- Condição: entrada pelo autor, pelo contribuidor OU pelo título
    
    local agents={}
    local newagents={}
    if #creator >= 1 or #contributor >= 1 then

	    table.insert(agents, creator)
	    
	    for str in string.gmatch(contributor, "([^;]+)") do
	    	table.insert(agents, str)
	    end
	    
	    for k, v in pairs(agents) do
	    	if string.match( v, '^(.*,.*),') then
	        	v = string.match( v, '^(.*,.*),') -- Assis, Machado de, 1839-1908
	        	table.insert(newagents, v)
	    	end
	    end
	    for k, v in pairs(agents) do
	    	if string.match( v, '^(.*)%s%(') then
	        	v = string.match( v, '^(.*)%s%(') -- Tolkien, J. R. R. (John Ronald Reuel), 1892-1973
	        	table.insert(newagents, v)
	        end
		end

    	agentstr = table.concat(newagents, "; ")
    	agentstr = frame:callParserFunction{ name = '#regex', args = { agent, '/(^.*?,)|(;.*?,)/', '<span style="text-transform: uppercase;">$1$2</span>' } }
        reference = agentstr .. '. <b>' .. title .. '</b>. '
    
    else
        reference = string.gsub( title, '^(.-%s)', '<span style="text-transform: uppercase;">%1</span>') .. '. '

    end
    -- Entrada e título definidos
    -- Verificar se há edição
    local edition = frame:callParserFunction{ name = '#show', args = { target, '?Has designation of edition#', link = 'none' } }

    if #edition >= 1 then
        edition = string.gsub( edition, 'edição', 'ed.')
        reference = reference .. edition .. '. '
    end
    -- Verificar se há local de publicação
    local place = frame:callParserFunction{ name = '#show', args = { target, '?Has place of publication#', link = 'none', valuesep = ';&#32;' } }
    place = string.gsub( place, ';.*$', '')

    if #place >= 1 then
        reference = reference .. place .. ': '
    else
        reference = reference .. '[S.l.]: '
    end
    -- Verificar se há editora
    local publisher = frame:callParserFunction{ name = '#show', args = { target, '?Has publisher\'s name#', link = 'none', valuesep = ':&#32;' } }
    publisher = string.gsub( publisher, ':.*$', '')

    if #publisher >= 1 then
        reference = reference .. publisher .. ', '
    else
        reference = reference .. '[s.n.], '
    end
    -- Verificar se há ano de publicação
    local year = frame:callParserFunction{ name = '#show', args = { target, '?Has date of publication#-f[Y]', link = 'none' } }
    local cyear = frame:callParserFunction{ name = '#show', args = { target, '?Has copyright date#-f[Y]', link = 'none' } }

    if #year >= 1 then
        reference = reference .. year .. '. '
    elseif #cyear >= 1 then
        reference = reference .. cyear .. '. '
    else
        reference = reference .. '[19--? ou 20--?]. '
    end
    -- Verificar se há páginas
    local pages = frame:callParserFunction{ name = '#show', args = { target, '?Has extent#', link = 'none' } }
    pages = string.gsub( pages, 'páginas', 'p. ')

    if #pages >= 1 then
        reference = reference .. pages
    end
    -- Verificar se há ISBN
    local isbn = frame:callParserFunction{ name = '#show', args = { target, '?Has ISBN#', link = 'none' } }
    isbn = string.match( isbn, '^%d+')

    if #isbn >= 1 then
        isbn = frame:callParserFunction{ name = '#invoke:Converter', args = { 'ISBN', isbn, 'Sim' } }
        reference = reference .. ' ISBN ' .. isbn .. '. '
    end
    -- Verificar se há URL
    local url = frame:callParserFunction{ name = '#show', args = { target, '?Has uniform resource locator#', link = 'none' } }

    if #url >= 1 then
        reference = reference .. ' Disponível em: ' .. url .. '. Acesso em: ' .. string.lower( frame:callParserFunction{ name = '#time',  args = { 'j M. Y' } } ) .. '.'
    end
    -- Retirar pontos repetidos
    reference = string.gsub( reference, '%.%.', '.')
    -- Exibe a referência
    return reference

end
return p