Module: AnyStyle::Format::CSL

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

Instance Method Summary collapse

Instance Method Details

#dates_to_citeproc(hash, date_format: 'edtf', **opts) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/anystyle/format/csl.rb', line 28

def dates_to_citeproc(hash, date_format: 'edtf', **opts)
  date, = *hash.delete(:date)

  case date_format.to_s
  when 'citeproc'
    hash[:issued] = begin
      require 'citeproc'
      ::CiteProc::Date.parse!(date.tr('X~', 'u?')).to_citeproc.symbolize_keys
    rescue
      { raw: date }
    end
  else
    hash[:issued] = date
  end
end

#format_csl(dataset, **opts) ⇒ Object Also known as: format_citeproc



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/anystyle/format/csl.rb', line 4

def format_csl(dataset, **opts)
  format_hash(dataset).map do |hash|
    dates_to_citeproc(hash, **opts) if hash.key?(:date)
    flatten_values hash, skip: Normalizer::Names.keys

    rename_value hash, :pages, :page
    rename_value hash, :location, :'publisher-place'
    rename_value hash, :url, :URL
    rename_value hash, :doi, :DOI
    rename_value hash, :pmid, :PMID
    rename_value hash, :pmcid, :PMCID

    Normalizer::Names.keys.each do |role|
      if hash.key?(role)
        hash[role].reject! { |name| name[:others] }
      end
    end

    hash
  end
end