Class: BibTeX::Bibliography
- Inherits:
-
Object
- Object
- BibTeX::Bibliography
- 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
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#errors ⇒ Object
readonly
Returns all objects which could not be parsed successfully.
-
#path ⇒ Object
Returns the value of attribute path.
-
#strings ⇒ Object
readonly
Returns the value of attribute strings.
Class Method Summary collapse
-
.open(path, options = {}) ⇒ Object
Opens and parses the ‘.bib’ file at the given
path
.
Instance Method Summary collapse
-
#<<(obj) ⇒ Object
Add an object to the bibliography.
-
#[](key) ⇒ Object
Returns the first entry with a given key.
-
#add(data) ⇒ Object
Adds a new element, or a list of new elements to the bibliography.
-
#comments ⇒ Object
Returns all @comment objects.
-
#delete(obj) ⇒ Object
Delete an object from the bibliography.
- #delete_all ⇒ Object
-
#empty? ⇒ Boolean
Returns true if the bibliography is currently empty.
-
#errors? ⇒ Boolean
Returns true if there are object which could not be parsed.
-
#initialize(data = []) ⇒ Bibliography
constructor
Creates a new bibliography; empty if no path is specified, otherwise by parsing the file at the given path.
-
#length ⇒ Object
Returns the number of objects in the bibliography (including meta comments).
-
#meta_comments ⇒ Object
Returns all meta comments, i.e., all text outside of BibTeX objects.
-
#preamble ⇒ Object
Returns all @preamble objects.
-
#replace_strings(options = {}) ⇒ Object
Replaces all string constants which are defined in the bibliography.
-
#save ⇒ Object
Saves the bibliography to the current path.
-
#save_to(path) ⇒ Object
Saves the bibliography to a file at the given path.
-
#to_s ⇒ Object
Returns a string representation of the bibliography.
- #to_xml ⇒ Object
- #to_yaml ⇒ Object
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
28 29 30 |
# File 'lib/bibtex/bibliography.rb', line 28 def data @data end |
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
28 29 30 |
# File 'lib/bibtex/bibliography.rb', line 28 def entries @entries end |
#errors ⇒ Object (readonly)
Returns all objects which could not be parsed successfully.
110 111 112 |
# File 'lib/bibtex/bibliography.rb', line 110 def errors @errors end |
#path ⇒ Object
Returns the value of attribute path.
27 28 29 |
# File 'lib/bibtex/bibliography.rb', line 27 def path @path end |
#strings ⇒ Object (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, ={}) Log.debug('Opening file ' + path.to_s) BibTeX::Parser.new().parse(File.read(path)) end |
Instance Method Details
#<<(obj) ⇒ Object
Add an object to the bibliography. Returns the bibliography.
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.
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 |
#comments ⇒ Object
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_all ⇒ Object
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.
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.
115 116 117 |
# File 'lib/bibtex/bibliography.rb', line 115 def errors? !errors.empty? end |
#length ⇒ Object
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_comments ⇒ Object
Returns all meta comments, i.e., all text outside of BibTeX objects.
105 106 107 |
# File 'lib/bibtex/bibliography.rb', line 105 def find_by_type(BibTeX::MetaComment) end |
#preamble ⇒ Object
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(={}) [:include] ||= [BibTeX::String, BibTeX::Preamble, BibTeX::Entry] find_by_type([:include]).each { |e| e.replace!(@strings) if e.respond_to?(:replace!)} end |
#save ⇒ Object
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_s ⇒ Object
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_xml ⇒ Object
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_yaml ⇒ Object
152 153 154 |
# File 'lib/bibtex/bibliography.rb', line 152 def to_yaml @data.map(&:to_yaml).join end |