module AnyStyle::Format::BibTeX

Constants

TYPES

Public Instance Methods

format_bibtex(dataset, **opts) click to toggle source
   # File lib/anystyle/format/bibtex.rb
12 def format_bibtex(dataset, **opts)
13   require 'bibtex'
14 
15   b = ::BibTeX::Bibliography.new
16   format_hash(dataset).each do |hash|
17     flatten_values hash, skip: Normalizer::Names.keys
18 
19     hash[:bibtex_type] = TYPES[hash[:type]] || hash[:type] || 'misc'
20     hash.delete :type
21 
22     case hash[:bibtex_type]
23     when 'article'
24       rename_value hash, :'container-title', :journal
25       rename_value hash, :issue, :number
26     when 'techreport'
27       rename_value hash, :publisher, :institution
28     when 'thesis'
29       rename_value hash, :publisher, :school
30     end
31 
32     Normalizer::Names.keys.each do |role|
33       names_to_bibtex hash, role
34     end
35 
36     rename_value hash, :'collection-title', :series
37     rename_value hash, :'container-title', :booktitle
38     rename_value hash, :accessed, :urldate
39     rename_value hash, :genre, :type
40     rename_value hash, :location, :address
41 
42     b << ::BibTeX::Entry.new(hash)
43   end
44   b
45 end
names_to_bibtex(hash, role) click to toggle source
   # File lib/anystyle/format/bibtex.rb
47 def names_to_bibtex(hash, role)
48   if hash.key?(role)
49     hash[role] = hash[role].map { |name|
50       case
51       when name.key?(:literal)
52         name[:literal]
53       when name.key?(:family) || name.key?(:given)
54         name.values_at(:family, :suffix, :given).compact.join(', ')
55       else
56         nil
57       end
58     }.compact.join(' and ')
59   end
60 end