Class: CiteProc::Ruby::Engine

Inherits:
Engine
  • Object
show all
Includes:
SortItems
Defined in:
lib/citeproc/ruby/engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SortItems

#compare_items, #compare_items_by_key, #compare_items_by_keys, #sort!, #sort_case_sensitively?

Constructor Details

#initialize(*arguments) ⇒ Engine

Returns a new instance of Engine.



18
19
20
21
22
23
# File 'lib/citeproc/ruby/engine.rb', line 18

def initialize(*arguments)
  super(*arguments)
  @renderer = Renderer.new(self)

  update! unless processor.nil?
end

Instance Attribute Details

#rendererObject (readonly)

Returns the value of attribute renderer.



13
14
15
# File 'lib/citeproc/ruby/engine.rb', line 13

def renderer
  @renderer
end

#styleObject

Returns the value of attribute style.



13
14
15
# File 'lib/citeproc/ruby/engine.rb', line 13

def style
  @style
end

Instance Method Details

#appendObject

Raises:

  • (NotImplementedByEngine)


48
49
50
# File 'lib/citeproc/ruby/engine.rb', line 48

def append
  raise NotImplementedByEngine
end

#bibliography(selector) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/citeproc/ruby/engine.rb', line 52

def bibliography(selector)
  node = style.bibliography
  return unless node

  selection = processor.data.select do |item|
    selector.matches?(item) && !selector.skip?(item)
  end

  sort!(selection, node.sort_keys) unless selection.empty? || !node.sort?

  Bibliography.new(node.bibliography_options) do |bib|
    format.bibliography(bib)

    idx = 1

    selection.each do |item|
      begin
        bib.push item.id, renderer.render_bibliography(item.cite(idx), node)
      rescue => error
        bib.errors << [item.id.to_s, error]
      ensure
        idx += 1 unless error
      end
    end
  end
end

#process(data) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/citeproc/ruby/engine.rb', line 29

def process(data)
  node = style.citation

  return unless node
  return '' if data.empty?

  # populate item data
  data.each do |item|
    item.data = processor[item.id].dup
  end

  # TODO implement sort in citation data
  sort!(data, node.sort_keys) unless !node.sort?

  # TODO citation number (after sorting?)

  renderer.render_citation data, node
end

#render(mode, data) ⇒ String+

Returns:

  • (String, Array<String>)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/citeproc/ruby/engine.rb', line 88

def render(mode, data)
  case mode
  when :bibliography
    node = style.bibliography

    data.map do |item|
      item.data = processor[item.id].dup
      renderer.render item, node
    end

  when :citation
    node = style.citation

    data.each do |item|
      item.data = processor[item.id].dup
    end

    renderer.render_citation data, node

  else
    raise ArgumentError, "cannot render unknown mode: #{mode.inspect}"
  end
end

#update!Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/citeproc/ruby/engine.rb', line 113

def update!
  renderer.format = processor.options[:format]
  renderer.locale = processor.options[:locale]

  if processor.options[:style].is_a? CSL::Style
    @style = processor.options[:style]
  else
    @style = CSL::Style.load processor.options[:style]
  end

  # Preliminary locale override implementation!
  # Does not yet reverse merge default region and default locale.
  @style.locales.sort.reverse.each do |locale|
    renderer.locale.merge! locale if renderer.locale.like?(locale)
  end

  self
end

#update_itemsObject

Raises:

  • (NotImplementedByEngine)


79
80
81
# File 'lib/citeproc/ruby/engine.rb', line 79

def update_items
  raise NotImplementedByEngine
end

#update_uncited_itemsObject

Raises:

  • (NotImplementedByEngine)


83
84
85
# File 'lib/citeproc/ruby/engine.rb', line 83

def update_uncited_items
  raise NotImplementedByEngine
end