Class: BibTeX::Bibliography

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

Overview

The Bibliography class models a BibTeX bibliography; typically, it corresponds to a ‘.bib’ file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = []) ⇒ Bibliography

Creates a new bibliography; empty if no path is specified, otherwise by parsing the file at the given path.



45
46
47
48
49
50
51
52
# File 'lib/bibtex/bibliography.rb', line 45

def initialize(data=[])
  @path = path
  @data = []
  @strings = {}
  @entries = {}
  @errors = []
  add(data)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



28
29
30
# File 'lib/bibtex/bibliography.rb', line 28

def data
  @data
end

#entriesObject (readonly)

Returns the value of attribute entries.



28
29
30
# File 'lib/bibtex/bibliography.rb', line 28

def entries
  @entries
end

#errorsObject (readonly)

Returns all objects which could not be parsed successfully.



110
111
112
# File 'lib/bibtex/bibliography.rb', line 110

def errors
  @errors
end

#pathObject

Returns the value of attribute path.



27
28
29
# File 'lib/bibtex/bibliography.rb', line 27

def path
  @path
end

#stringsObject (readonly)

Returns the value of attribute strings.



28
29
30
# File 'lib/bibtex/bibliography.rb', line 28

def strings
  @strings
end

Class Method Details

.open(path, options = {}) ⇒ Object

Opens and parses the ‘.bib’ file at the given path. Returns a new Bibliography instance corresponding to the file.

The options argument is passed on to BibTeX::Parser.new.



36
37
38
39
# File 'lib/bibtex/bibliography.rb', line 36

def self.open(path, options={})
  Log.debug('Opening file ' + path.to_s)
  BibTeX::Parser.new(options).parse(File.read(path))
end

Instance Method Details

#<<(obj) ⇒ Object

Add an object to the bibliography. Returns the bibliography.

Raises:

  • (ArgumentError)


72
73
74
75
76
# File 'lib/bibtex/bibliography.rb', line 72

def <<(obj)
  raise(ArgumentError, 'A BibTeX::Bibliography can contain only BibTeX::Elements; was: ' + obj.class.name) unless obj.kind_of?(Element)
  @data << obj.added_to_bibliography(self)
  self
end

#[](key) ⇒ Object

Returns the first entry with a given key.



95
96
97
# File 'lib/bibtex/bibliography.rb', line 95

def [](key)
  @entries[key.to_s]
end

#add(data) ⇒ Object

Adds a new element, or a list of new elements to the bibliography.

Raises:

  • (ArgumentError)


55
56
57
58
59
# File 'lib/bibtex/bibliography.rb', line 55

def add(data)
  raise(ArgumentError,'BibTeX::Bibliography.add data expected to be enumerable or of type BibTeX::Element; was: ' + data.class.name) unless data.respond_to?(:each) || data.kind_of?(Element)
  data.kind_of?(Element) ? self << data : data.each { |d| self << d }
  self
end

#commentsObject

Returns all @comment objects.



100
101
102
# File 'lib/bibtex/bibliography.rb', line 100

def comments
  find_by_type(BibTeX::Comment)
end

#delete(obj) ⇒ Object

Delete an object from the bibliography. Returns the object, or nil if the object was not part of the bibliography.



80
81
82
# File 'lib/bibtex/bibliography.rb', line 80

def delete(obj)
  @data.delete(obj.removed_from_bibliography(self))
end

#delete_allObject



84
85
86
87
# File 'lib/bibtex/bibliography.rb', line 84

def delete_all
  @data.each { |obj| obj.removed_from_bibliography(self) }
  @data = []
end

#empty?Boolean

Returns true if the bibliography is currently empty.

Returns:

  • (Boolean)


138
139
140
# File 'lib/bibtex/bibliography.rb', line 138

def empty?
  @data.empty?
end

#errors?Boolean

Returns true if there are object which could not be parsed.

Returns:

  • (Boolean)


115
116
117
# File 'lib/bibtex/bibliography.rb', line 115

def errors?
  !errors.empty?
end

#lengthObject

Returns the number of objects in the bibliography (including meta comments).



143
144
145
# File 'lib/bibtex/bibliography.rb', line 143

def length
  @data.length
end

#meta_commentsObject

Returns all meta comments, i.e., all text outside of BibTeX objects.



105
106
107
# File 'lib/bibtex/bibliography.rb', line 105

def meta_comments
  find_by_type(BibTeX::MetaComment)
end

#preambleObject

Returns all @preamble objects.



90
91
92
# File 'lib/bibtex/bibliography.rb', line 90

def preamble
  find_by_type(BibTeX::Preamble)
end

#replace_strings(options = {}) ⇒ Object

Replaces all string constants which are defined in the bibliography.

By default constants in @string, @preamble and entries are defined; this behaviour can be changed using the options argument by setting the :include option to a list of types.

Note that strings are replaced in the order in which they occur in the bibliography.

call-seq: replace_strings replace_strings({ :include => [BibTeX::String,BibTeX::Preamble]})



132
133
134
135
# File 'lib/bibtex/bibliography.rb', line 132

def replace_strings(options={})
  options[:include] ||= [BibTeX::String, BibTeX::Preamble, BibTeX::Entry]
  find_by_type(options[:include]).each { |e| e.replace!(@strings) if e.respond_to?(:replace!)}
end

#saveObject

Saves the bibliography to the current path.



62
63
64
# File 'lib/bibtex/bibliography.rb', line 62

def save
  save_to(@path)
end

#save_to(path) ⇒ Object

Saves the bibliography to a file at the given path.



67
68
69
# File 'lib/bibtex/bibliography.rb', line 67

def save_to(path)
  File.write(path,to_s)
end

#to_sObject

Returns a string representation of the bibliography.



148
149
150
# File 'lib/bibtex/bibliography.rb', line 148

def to_s
  @data.map(&:to_s).join
end

#to_xmlObject



156
157
158
159
160
161
162
163
# File 'lib/bibtex/bibliography.rb', line 156

def to_xml
  xml = REXML::Document.new
  xml << REXML::XMLDecl.new('1.0','UTF-8')
  root = REXML::Element.new('bibliography')
  @data.each { |e| root.add_element(e.to_xml) }
  xml << root
  xml
end

#to_yamlObject



152
153
154
# File 'lib/bibtex/bibliography.rb', line 152

def to_yaml
  @data.map(&:to_yaml).join
end