Class: BibTeX::Entry::CiteProcConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/bibtex/entry/citeproc_converter.rb

Constant Summary collapse

CSL_FILTER =
Hash.new { |h, k| k }.merge(Hash[*%w{
  date         issued
  isbn         ISBN
  booktitle    container-title
  journal      container-title
  series       collection-title
  address      publisher-place
  pages        page
  number       issue
  url          URL
  doi          DOI
  year         issued
  type         genre
  school       publisher
  institution  publisher
  organization publisher
  howpublished publisher
  type         genre
}.map(&:intern)]).freeze
CSL_FIELDS =
%w{
  abstract annote archive archive_location archive-place
  authority call-number chapter-number citation-label citation-number
  collection-title container-title DOI edition event event-place
  first-reference-note-number genre ISBN issue jurisdiction keyword locator
  medium note number number-of-pages number-of-volumes original-publisher
  original-publisher-place original-title page page-first publisher
  publisher-place references section status title URL version volume
  year-suffix accessed container event-date issued original-date
  author editor translator recipient interviewer publisher composer
  original-publisher original-author container-author collection-editor
}.map(&:intern).freeze
CSL_TYPES =
Hash.new { |h, k| k }.merge(Hash[*%w{
  booklet        pamphlet
  conference     paper-conference
  inbook         chapter
  incollection   chapter
  inproceedings  paper-conference
  manual         book
  mastersthesis  thesis
  phdthesis      thesis
  proceedings    book
  techreport     report
  unpublished    manuscript
  article        article-journal
}.map(&:intern)]).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bibtex, options = {}) ⇒ CiteProcConverter

Returns a new instance of CiteProcConverter.



54
55
56
57
# File 'lib/bibtex/entry/citeproc_converter.rb', line 54

def initialize(bibtex, options = {})
  @bibtex = bibtex
  @options = { quotes: [] }.merge(options)
end

Class Method Details

.convert(bibtex, options = {}) ⇒ Object



50
51
52
# File 'lib/bibtex/entry/citeproc_converter.rb', line 50

def self.convert(bibtex, options = {})
  new(bibtex, options).convert!
end

Instance Method Details

#conferencesObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bibtex/entry/citeproc_converter.rb', line 81

def conferences
  return unless [:conference, :proceedings, :inproceedings].include?(bibtex.type)

  if bibtex.field?(:organization) && bibtex.field?(:publisher)
    hash['authority'] = bibtex[:organization]
    hash['publisher'] = bibtex[:publisher]
  end

  if bibtex.field? :address
    hash['event-place'] = bibtex[:address]
  end
end

#convert!Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bibtex/entry/citeproc_converter.rb', line 59

def convert!
  bibtex.parse_names
  bibtex.parse_month

  bibtex.each_pair do |key, value|
    unless BibTeX::Entry::DATE_FIELDS.include?(key)
      cp_key = CSL_FILTER[key].to_s

      if hash.key?(cp_key)
        hash[key] = value.to_citeproc(options)
      else
        hash[cp_key] = value.to_citeproc(options)
      end
    end
  end

  methods = self.class.instance_methods(false) - [:convert!, :hash]
  methods.each { |m| send(m) }

  hash
end

#dateObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/bibtex/entry/citeproc_converter.rb', line 99

def date
  case
  when bibtex.field?(:date)
    hash['issued'] = {
      'date-parts' => bibtex.date.to_s.split('/').map { |part|
        part.split('-').map(&:to_i)
      }
    }

  when bibtex.field?(:year)
    case bibtex[:year].to_s
    when /^\d+$/
      parts = [bibtex[:year].to_s]

      if bibtex.field?(:month)
        parts.push BibTeX::Entry::MONTHS.find_index(bibtex[:month].to_s.intern)
        parts[1] = parts[1] + 1 unless parts[1].nil?

        if bibtex.field?(:day)
          parts.push bibtex[:day]
        end
      end

      hash['issued'] = { 'date-parts' => [parts.compact.map(&:to_i)] }
    else
      hash['issued'] = { 'literal' => bibtex[:year].to_s }
    end

  else
    # no date present
  end
end

#keyObject



132
133
134
# File 'lib/bibtex/entry/citeproc_converter.rb', line 132

def key
  hash['id'] = bibtex.key.to_s
end

#techreportObject



94
95
96
97
# File 'lib/bibtex/entry/citeproc_converter.rb', line 94

def techreport
  return unless bibtex.type == :techreport
  hash['number'] = bibtex[:number].to_s if bibtex.field? :number
end

#typeObject



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/bibtex/entry/citeproc_converter.rb', line 136

def type
  hash['type'] = CSL_TYPES[bibtex.type].to_s

  return if hash.key?('genre')
  case bibtex.type
  when :mastersthesis
    hash['genre'] = "Master's thesis"
  when :phdthesis
    hash['genre'] = 'PhD thesis'
  end
end