Module: AnyStyle::Format::BibTeX

Included in:
Parser
Defined in:
lib/anystyle/format/bibtex.rb

Constant Summary collapse

TYPES =
{
  'article-journal' => 'article',
  'chapter' => 'incollection',
  'manuscript' => 'unpublished',
  'paper-conference' => 'inproceedings',
  'report' => 'techreport'
}

Instance Method Summary collapse

Instance Method Details

#format_bibtex(dataset, **opts) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/anystyle/format/bibtex.rb', line 12

def format_bibtex(dataset, **opts)
  require 'bibtex'

  b = ::BibTeX::Bibliography.new
  format_hash(dataset).each do |hash|
    flatten_values hash, skip: Normalizer::Names.keys

    hash[:bibtex_type] = TYPES[hash[:type]] || hash[:type] || 'misc'
    hash.delete :type

    case hash[:bibtex_type]
    when 'article'
      rename_value hash, :'container-title', :journal
      rename_value hash, :issue, :number
    when 'techreport'
      rename_value hash, :publisher, :institution
    when 'thesis'
      rename_value hash, :publisher, :school
    end

    Normalizer::Names.keys.each do |role|
      names_to_bibtex hash, role
    end

    rename_value hash, :'collection-title', :series
    rename_value hash, :'container-title', :booktitle
    rename_value hash, :accessed, :urldate
    rename_value hash, :genre, :type
    rename_value hash, :location, :address

    b << ::BibTeX::Entry.new(hash)
  end
  b
end

#names_to_bibtex(hash, role) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/anystyle/format/bibtex.rb', line 47

def names_to_bibtex(hash, role)
  if hash.key?(role)
    hash[role] = hash[role].map { |name|
      case
      when name.key?(:literal)
        name[:literal]
      when name.key?(:family) || name.key?(:given)
        name.values_at(:family, :suffix, :given).compact.join(', ')
      else
        nil
      end
    }.compact.join(' and ')
  end
end