Class: RCite::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/rcite/processor.rb

Overview

The Processor class is responsible for chaining the two steps involved in creating a citation or bibliography together:

  1. Load a BibTeX file with bibliographic information about all the texts.
  2. Load a style that defines how to construct citations and bibliography entries from the former.

Moreover, it provides shortcuts for citing and bibliography entry creation by mapping Style#cite and Style#bib.

Constant Summary

@@style_counter =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bibliographyBibTeX::Bibliography

A BibTeX::Bibliography with bibliographic data loaded from a BibTeX file. Can be nil if no data file has been loaded yet.



38
39
40
# File 'lib/rcite/processor.rb', line 38

def bibliography
  @bibliography
end

#styleRCite::Style?

The style that is used to turn bibliographic data gathered from a BibTeX file to actual citations/bibliographic entries. Should usually be a subclass of Style. Can be nil if no style has been loaded yet.



25
26
27
# File 'lib/rcite/processor.rb', line 25

def style
  @style
end

#style_classClass? (readonly)

Returns the class that the current #style is an instance of. The classname changes with each call of #load_style. Can be nil if no style has been loaded yet.



32
33
34
# File 'lib/rcite/processor.rb', line 32

def style_class
  @style_class
end

Instance Method Details

#bib(id, fields = {}) ⇒ String

Generates a bibliography entry for the text with given id. This method searches the #bibliography for a text where key == id and -- if it happens to find one -- returns @style.bib(text, fields).

Raises:

  • (ArgumentError)

    if #style or #bibliography are nil, or if there is no text with given id in bib.



134
135
136
137
138
139
140
141
142
143
# File 'lib/rcite/processor.rb', line 134

def bib(id, fields = {})
  check_attrs

  text = find_text(id)
  if text
    @style.bib(text, fields)
  else
    raise ArgumentError.new "No text with id #{id} found."
  end
end

#cite(id, fields = {}) ⇒ String

Generates a citation for the text with given id. This method searches the #bibliography for a text where :key == id and -- if it happens to find one -- returns @style.cite(text, additional_fields).

Raises:

  • (ArgumentError)

    if #style or #bibliography are nil, or if there is no text with given id in bib.



112
113
114
115
116
117
118
119
120
121
# File 'lib/rcite/processor.rb', line 112

def cite(id, fields = {})
  check_attrs

  text = find_text(id)
  if text
    @style.cite(text, fields)
  else
    raise ArgumentError, "No text with id #{id} found."
  end
end

#load_data(file) ⇒ BibTeX::Bibliography

Loads the specified BibTeX file and sets #bibliography accordingly. This method is merely a wrapper for BibTeX::Bibliography#open.

Raises:

  • all errors that BibTeX::Bibliography raises.



92
93
94
# File 'lib/rcite/processor.rb', line 92

def load_data(file)
  @bibliography = BibTeX::Bibliography.open(file)
end

#load_style(file) ⇒ void

This method returns an undefined value.

Loads an RCite style.

Styles simply consist of Ruby code (mainly method definitions) that are executed in the context of a newly created class RCite::StyleClass that inherits from Style. Think of the code in the style file as being surrounded by

class RCite::StyleClass < Style
  # code from the style file
end

The style's StyleClass is different for each call of this method. Use #style_class to get the current Class object.

This method also sets #style to a new instance of the loaded class.

Raises:

  • (LoadError)

    if the specified file cannot be loaded by the Kernel method load.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rcite/processor.rb', line 67

def load_style(file)
  style = Class.new(RCite::Style)
  raise LoadError, "Could not read file: #{file}" unless
    File.exists?(file) && File.readable?(file)

  style.module_eval(File.read(File.absolute_path(file)))

  @@style_counter += 1
  classname = "Style#{@@style_counter}".to_sym
  RCite.const_set(classname, style)

  @style_class = style
  @style = @style_class.new
  @style.preamble if @style.respond_to?(:preamble)
end