Class: BibTeX::Entry
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/bibtex/entry.rb
Overview
Represents a regular BibTeX entry.
Constant Summary collapse
- REQUIRED_FIELDS =
Hash containing the required fields of the standard entry types
Hash.new([]).merge({ :article => [:author,:title,:journal,:year], :book => [[:author,:editor],:title,:publisher,:year], :booklet => [:title], :conference => [:author,:title,:booktitle,:year], :inbook => [[:author,:editor],:title,[:chapter,:pages],:publisher,:year], :incollection => [:author,:title,:booktitle,:publisher,:year], :inproceedings => [:author,:title,:booktitle,:year], :manual => [:title], :mastersthesis => [:author,:title,:school,:year], :misc => [], :phdthesis => [:author,:title,:school,:year], :proceedings => [:title,:year], :techreport => [:author,:title,:institution,:year], :unpublished => [:author,:title,:note] }).freeze
- NAME_FIELDS =
[:author,:editor,:translator].freeze
- DATE_FIELDS =
[:year,:month].freeze
- MONTHS =
[:jan,:feb,:mar,:apr,:may,:jun,:jul,:aug,:sep,:oct,:nov,:dec].freeze
- MONTHS_FILTER =
Hash.new do |h,k| case k.to_s.strip when /^(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/i h[k] = Value.new(k.to_s[0,3].downcase.to_sym) when /^\d\d?$/ h[k] = Value.new(MONTHS[k.to_i - 1] || k) else h[k] = Value.new(k) end end
- CSL_FILTER =
Hash.new {|h,k|k}.merge(Hash[*%w{ date issued isbn ISBN booktitle container-title journal container-title series collection-title address publisher-place pages page number issue url URL doi DOI year issued }.map(&:intern)]).freeze
- CSL_FIELDS =
%w{ abstract annote archive archive_location archive-place authority call-number chapter-number citation-label citation-number collection-title container-title DOI edition event event-place first-reference-note-number genre ISBN issue jurisdiction keyword locator medium note number number-of-pages number-of-volumes original-publisher original-publisher-place original-title page page-first publisher publisher-place references section status title URL version volume year-suffix accessed container event-date issued original-date author editor translator recipient interviewer publisher composer original-publisher original-author container-author collection-editor }.map(&:intern).freeze
- CSL_TYPES =
Hash.new {|h,k|k}.merge(Hash[*%w{ booklet pamphlet conference paper-conference inbook chapter incollection chapter inproceedings paper-conference manual book mastersthesis thesis misc article phdthesis thesis proceedings paper-conference techreport report unpublished manuscript }.map(&:intern)]).freeze
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#type ⇒ Object
Returns the value of attribute type.
Attributes inherited from Element
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#[](name) ⇒ Object
Returns the value of the field with the given name.
-
#[]=(name, value) ⇒ Object
Adds a new field (name-value pair) to the entry.
-
#add(*arguments) ⇒ Object
(also: #<<)
Adds a new field (name-value pair) or multiple fields to the entry.
-
#added_to_bibliography(bibliography) ⇒ Object
Called when the element was added to a bibliography.
-
#content(options = {}) ⇒ Object
Returns a string of all the entry’s fields.
-
#convert(filter) ⇒ Object
Returns a duplicate entry with all values converted using the filter.
-
#convert!(filter) ⇒ Object
In-place variant of @see convert.
-
#delete(name) ⇒ Object
Removes the field with a given name from the entry.
- #has_field?(field) ⇒ Boolean
- #has_type?(type) ⇒ Boolean
-
#initialize(attributes = {}) {|_self| ... } ⇒ Entry
constructor
Creates a new instance.
- #initialize_copy(other) ⇒ Object
- #issued ⇒ Object (also: #citeproc_date)
- #join ⇒ Object
- #key ⇒ Object (also: #id)
-
#key=(key) ⇒ Object
(also: #id=)
Sets the key of the entry.
- #method_missing(name, *args, &block) ⇒ Object
- #month=(month) ⇒ Object
- #parse_month ⇒ Object (also: #parse_months)
-
#parse_names ⇒ Object
Parses all name values of the entry.
-
#removed_from_bibliography(bibliography) ⇒ Object
Called when the element was removed from a bibliography.
-
#rename(*arguments) ⇒ Object
(also: #rename_fields)
Returns a copy of the Entry with all the field names renamed.
-
#rename!(*arguments) ⇒ Object
(also: #rename_fields!)
Renames the given field names unless a field with the new name already exists.
- #replace(*arguments) ⇒ Object
- #respond_to?(method) ⇒ Boolean
- #to_citeproc(options = {}) ⇒ Object
- #to_hash(options = {}) ⇒ Object
-
#to_s(options = {}) ⇒ Object
Returns a string representation of the entry.
- #to_xml(options = {}) ⇒ Object
-
#valid? ⇒ Boolean
Returns false if the entry is one of the standard entry types and does not have definitions of all the required fields for that type.
Methods inherited from Element
#matches?, #meets?, parse, #to_json, #to_yaml
Constructor Details
#initialize(attributes = {}) {|_self| ... } ⇒ Entry
Creates a new instance. If a hash is given, the entry is populated accordingly.
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/bibtex/entry.rb', line 109 def initialize(attributes = {}) @fields = {} self.type = attributes.delete(:type) if attributes.has_key?(:type) self.key = attributes.delete(:key) if attributes.has_key?(:key) add(attributes) yield self if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/bibtex/entry.rb', line 163 def method_missing(name, *args, &block) case when @fields.has_key?(name) @fields[name] when name.to_s =~ /^(.+)=$/ send(:add, $1.to_sym, args[0]) when name =~ /^(?:convert|from)_([a-z]+)(!)?$/ $2 ? convert!($1, &block) : convert($1, &block) else super end end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
105 106 107 |
# File 'lib/bibtex/entry.rb', line 105 def fields @fields end |
#type ⇒ Object
Returns the value of attribute type.
105 106 107 |
# File 'lib/bibtex/entry.rb', line 105 def type @type end |
Instance Method Details
#<=>(other) ⇒ Object
361 362 363 |
# File 'lib/bibtex/entry.rb', line 361 def <=>(other) type != other.type ? type <=> other.type : key != other.key ? key <=> other.key : to_s <=> other.to_s end |
#[](name) ⇒ Object
Returns the value of the field with the given name.
201 202 203 |
# File 'lib/bibtex/entry.rb', line 201 def [](name) @fields[name.to_sym] end |
#[]=(name, value) ⇒ Object
Adds a new field (name-value pair) to the entry. Returns the new value.
207 208 209 |
# File 'lib/bibtex/entry.rb', line 207 def []=(name, value) add(name.to_sym, value) end |
#add(*arguments) ⇒ Object Also known as: <<
Adds a new field (name-value pair) or multiple fields to the entry. Returns the entry for chainability.
call-seq: add(:author, “Edgar A. Poe”) add(:author, “Edgar A. Poe”, :title, “The Raven”) add([:author, “Edgar A. Poe”, :title, “The Raven”]) add(:author => “Edgar A. Poe”, :title => “The Raven”)
220 221 222 223 224 225 |
# File 'lib/bibtex/entry.rb', line 220 def add(*arguments) Hash[*arguments.flatten].each_pair do |name, value| @fields[name.to_sym] = Value.new(value) end self end |
#added_to_bibliography(bibliography) ⇒ Object
Called when the element was added to a bibliography.
244 245 246 247 248 249 250 251 |
# File 'lib/bibtex/entry.rb', line 244 def added_to_bibliography(bibliography) super bibliography.entries[key] = self parse_names if bibliography.[:parse_names] parse_months if bibliography.[:parse_months] convert(bibliography.[:filter]) if bibliography.[:filter] self end |
#content(options = {}) ⇒ Object
Returns a string of all the entry’s fields.
297 298 299 |
# File 'lib/bibtex/entry.rb', line 297 def content( = {}) @fields.map { |k,v| "#{k} = #{ @fields[k].to_s() }" }.join(",\n") end |
#convert(filter) ⇒ Object
Returns a duplicate entry with all values converted using the filter. If an optional block is given, only those values will be converted where the block returns true (the block will be called with each key-value pair).
351 352 353 |
# File 'lib/bibtex/entry.rb', line 351 def convert (filter) block_given? ? dup.convert!(filter, &Proc.new) : dup.convert!(filter) end |
#convert!(filter) ⇒ Object
In-place variant of @see convert
356 357 358 359 |
# File 'lib/bibtex/entry.rb', line 356 def convert! (filter) @fields.each_pair { |k,v| !block_given? || yield(k,v) ? v.convert!(filter) : v } self end |
#delete(name) ⇒ Object
Removes the field with a given name from the entry. Returns the value of the deleted field; nil if the field was not set.
231 232 233 |
# File 'lib/bibtex/entry.rb', line 231 def delete(name) @fields.delete(name.to_sym) end |
#has_field?(field) ⇒ Boolean
159 160 161 |
# File 'lib/bibtex/entry.rb', line 159 def has_field?(field) @fields.has_key?(field) end |
#has_type?(type) ⇒ Boolean
155 156 157 |
# File 'lib/bibtex/entry.rb', line 155 def has_type?(type) type.to_s.match(/^entry$/i) || @type == type.to_sym || super end |
#initialize_copy(other) ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/bibtex/entry.rb', line 120 def initialize_copy (other) @fields = {} self.type = other.type self.key = other.key add(other.fields) end |
#issued ⇒ Object Also known as: citeproc_date
324 325 326 327 328 |
# File 'lib/bibtex/entry.rb', line 324 def issued m = MONTHS.find_index(@fields[:month] && @fields[:month].v) m = m + 1 unless m.nil? { 'date-parts' => [[@fields[:year],m].compact.map(&:to_i)] } end |
#join ⇒ Object
266 267 268 269 |
# File 'lib/bibtex/entry.rb', line 266 def join @fields.values.each(&:join) self end |
#key ⇒ Object Also known as: id
141 142 143 |
# File 'lib/bibtex/entry.rb', line 141 def key @key ||= default_key end |
#key=(key) ⇒ Object Also known as: id=
Sets the key of the entry
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/bibtex/entry.rb', line 130 def key=(key) raise(ArgumentError, "keys must be convertible to Symbol; was: #{type.class.name}.") unless type.respond_to?(:to_sym) unless @bibliography.nil? @bibliography.entries.delete(@key) @bibliography.entries[key] = self end @key = key.to_sym end |
#month=(month) ⇒ Object
271 272 273 |
# File 'lib/bibtex/entry.rb', line 271 def month=(month) @fields[:month] = MONTHS_FILTER[month] end |
#parse_month ⇒ Object Also known as: parse_months
275 276 277 278 |
# File 'lib/bibtex/entry.rb', line 275 def parse_month @fields[:month] = MONTHS_FILTER[@fields[:month]] if @fields.has_key?(:month) self end |
#parse_names ⇒ Object
Parses all name values of the entry. Tries to replace and join the value prior to parsing.
284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/bibtex/entry.rb', line 284 def parse_names strings = bibliography ? bibliography.strings.values : [] NAME_FIELDS.each do |key| if name = @fields[key] name.replace(strings).join name = name.to_name @fields[key] = name end end self end |
#removed_from_bibliography(bibliography) ⇒ Object
Called when the element was removed from a bibliography.
254 255 256 257 258 |
# File 'lib/bibtex/entry.rb', line 254 def removed_from_bibliography(bibliography) super bibliography.entries[key] = nil self end |
#rename(*arguments) ⇒ Object Also known as: rename_fields
Returns a copy of the Entry with all the field names renamed.
181 182 183 |
# File 'lib/bibtex/entry.rb', line 181 def rename(*arguments) dup.rename!(*arguments) end |
#rename!(*arguments) ⇒ Object Also known as: rename_fields!
Renames the given field names unless a field with the new name already exists.
187 188 189 190 191 192 193 194 195 |
# File 'lib/bibtex/entry.rb', line 187 def rename!(*arguments) Hash[*arguments.flatten].each_pair do |from,to| if @fields.has_key?(from) && !@fields.has_key?(to) @fields[to] = @fields[from] @fields.delete(from) end end self end |
#replace(*arguments) ⇒ Object
260 261 262 263 264 |
# File 'lib/bibtex/entry.rb', line 260 def replace(*arguments) arguments = bibliography.q('@string') if arguments.empty? @fields.values.each { |v| v.replace(*arguments) } self end |
#respond_to?(method) ⇒ Boolean
176 177 178 |
# File 'lib/bibtex/entry.rb', line 176 def respond_to?(method) @fields.has_key?(method.to_sym) || method.to_s.match(/=$/) || method =~ /^(?:convert|from)_([a-z]+)(!)?$/ || super end |
#to_citeproc(options = {}) ⇒ Object
314 315 316 317 318 319 320 321 322 |
# File 'lib/bibtex/entry.rb', line 314 def to_citeproc( = {}) [:quotes] ||= [] hash = { 'id' => key.to_s, 'type' => CSL_TYPES[type].to_s } each_pair do |k,v| hash[CSL_FILTER[k].to_s] = v.to_citeproc() unless DATE_FIELDS.include?(k) end hash['issued'] = citeproc_date hash end |
#to_hash(options = {}) ⇒ Object
307 308 309 310 311 312 |
# File 'lib/bibtex/entry.rb', line 307 def to_hash( = {}) [:quotes] ||= %w({ }) hash = { :key => key, :type => type } each_pair { |k,v| hash[k] = v.to_s() } hash end |
#to_s(options = {}) ⇒ Object
Returns a string representation of the entry.
302 303 304 305 |
# File 'lib/bibtex/entry.rb', line 302 def to_s( = {}) [:quotes] ||= %w({ }) ["@#{type}{#{key},", content().gsub(/^/,' '), "}\n"].join("\n") end |
#to_xml(options = {}) ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/bibtex/entry.rb', line 332 def to_xml( = {}) require 'rexml/document' xml = REXML::Element.new(type) xml.attributes['key'] = key @fields.each do |k,v| e = REXML::Element.new(k.to_s) e.text = v.to_s() xml.add_element(e) end xml end |
#valid? ⇒ Boolean
Returns false if the entry is one of the standard entry types and does not have definitions of all the required fields for that type.
237 238 239 240 241 |
# File 'lib/bibtex/entry.rb', line 237 def valid? REQUIRED_FIELDS[@type].all? do |f| f.is_a?(Array) ? !(f & @fields.keys).empty? : !@fields[f].nil? end end |