class BibTeX::Entry::CiteProcConverter

Constants

CSL_FIELDS
CSL_FILTER
CSL_TYPES

Attributes

bibtex[R]
hash[R]
options[R]

Public Class Methods

convert(bibtex, options = {}) click to toggle source
# File lib/bibtex/entry/citeproc_converter.rb, line 54
def self.convert(bibtex, options = {})
  new(bibtex, options).convert!
end
new(bibtex, options = {}) click to toggle source
# File lib/bibtex/entry/citeproc_converter.rb, line 58
def initialize(bibtex, options = {})
  @bibtex = bibtex
  @hash = {}
  @options = { quotes: [] }.merge(options)
end

Public Instance Methods

accessed() click to toggle source
# File lib/bibtex/entry/citeproc_converter.rb, line 127
def accessed
  return unless hash.key? 'accessed'

  hash['accessed'] =
    case hash['accessed']
    when %r{^[\d/\s-]+$}
      {
        'date-parts' =>
          hash['accessed'].split('/').map { |pt| pt.split('-').map(&:to_i) }
      }
    else
      { 'literal' => hash['accessed'] }
    end
end
conferences() click to toggle source
# File lib/bibtex/entry/citeproc_converter.rb, line 82
def conferences
  return unless %i[conference proceedings inproceedings].include?(bibtex.type)

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

  hash['event-place'] = bibtex[:address] if bibtex.field? :address
end
convert!() click to toggle source
# File lib/bibtex/entry/citeproc_converter.rb, line 64
def convert!
  bibtex.parse_names
  bibtex.parse_month

  bibtex.each_pair do |key, value|
    convert key, value
  end

  bibtex.inherited_fields.each do |key|
    convert key, bibtex.parent.provide(key)
  end

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

  hash
end
date() click to toggle source
# File lib/bibtex/entry/citeproc_converter.rb, line 99
def date
  if bibtex.field?(:date)
    hash['issued'] = {
      'date-parts' => bibtex.date.to_s.split('/').map do |part|
        part.split('-').map(&:to_i)
      end
    }

  elsif 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?

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

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

  end
end
key() click to toggle source
# File lib/bibtex/entry/citeproc_converter.rb, line 142
def key
  hash['id'] = bibtex.key.to_s
end
techreport() click to toggle source
# File lib/bibtex/entry/citeproc_converter.rb, line 93
def techreport
  return unless %i[techreport report].include?(bibtex.type)

  hash['number'] = bibtex[:number].to_s if bibtex.field? :number
end
type() click to toggle source
# File lib/bibtex/entry/citeproc_converter.rb, line 146
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

Private Instance Methods

convert(key, value) click to toggle source
# File lib/bibtex/entry/citeproc_converter.rb, line 163
def convert(key, value)
  return if BibTeX::Entry::DATE_FIELDS.include?(key)
  return include_url(value) if howpublished_with_url?(key, value)

  citeproc_key = CSL_FILTER[key].to_s

  if hash.key?(citeproc_key)
    hash[key] = value.to_citeproc(options)
  else
    hash[citeproc_key] = value.to_citeproc(options)
  end
end
howpublished_with_url?(key, value) click to toggle source
# File lib/bibtex/entry/citeproc_converter.rb, line 176
def howpublished_with_url?(key, value)
  key == :howpublished && value.include?('url')
end
include_url(value) click to toggle source
# File lib/bibtex/entry/citeproc_converter.rb, line 180
def include_url(value)
  hash['URL'] = value.to_s[/url{?([^}]*)/, 1]
end