Módulo:ABNT

De Wikincat
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); o parâmetro nomeado Page (via WSSearchFront); 
    -- 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 = frame:getParent().args.Page
    end
    if target == '' or target == nil then
        target = mw.title.getCurrentTitle().prefixedText
    end
    local reference
    -- 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á colaboradores 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 pelos agentes OU pelo título
    local agents={}
    if #creator >= 1 or #contributor >= 1 then
      if creator ~= '' then
        table.insert(agents, creator) -- como criador vem do campo 1XX, não há mais de um valor
      end

      for v in string.gmatch(contributor, "([^;]+)") do -- como colaborador vem do campo 7XX, pode haver mais de um valor
        table.insert(agents, v)
      end

      for k, v in pairs(agents) do
        if string.match( v, '^(.*,.*),') then
          -- 'Assis, Machado de, 1839-1908' > 'Assis, Machado de'
            agents[k] = string.match( v, '^(.*,.*),')
        end
      end

      for k, v in pairs(agents) do
        if string.match( v, '^(.*)%s%(') then
          -- 'Tolkien, J. R. R. (John Ronald Reuel), 1892-1973' > 'Tolkien, J. R. R.'
            agents[k] = string.match( v, '^(.*)%s%(')
        end
      end

      local agentStr = table.concat(agents, "; ")
      agentStr = frame:callParserFunction{ name = '#rreplace', args = { agentStr, '/(^.*?,)|(;.*?,)/', '<span style="text-transform: uppercase;">$1$2</span>' } }
      reference = agentStr .. '. <b>' .. title .. '</b>. '

    else
      -- Entrada pelo título
      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+') or ''

    if #isbn >= 1 then
        isbn = frame:callParserFunction{ name = '#invoke:Hyphenate', args = { 'ISBN', isbn } }
        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