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 =
Defines 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
- FIELD_ALIASES =
Defines the default fallbacks for values defined in cross-references
{ :booktitle => :title, # :editor => :author }.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 type genre }.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 article article-journal }.map(&:intern)]).freeze
- BIBO_FIELDS =
Hash[*%w{ pages pages number issue isbn isbn issn issn doi doi edition edition abstract abstract volume volume }.map(&:intern)].freeze
- BIBO_TYPES =
Hash.new(:Document).merge(Hash[*%w{ booklet Book book Book conference Conference inbook Article incollection Article inproceedings Article manual Manual mastersthesis Thesis phdthesis Thesis proceedings Proceedings techreport Report journal Journal periodical Periodical unpublished Manuscript article Article }.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
(also: #get)
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: #<<)
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”) add(:author => Names.new(Name.new(:first => ‘Edgar A.’, :last => ‘Poe’))).
-
#added_to_bibliography(bibliography) ⇒ Object
Called when the element was added to a bibliography.
-
#aliases ⇒ Object
Returns the Entry’s field name aliases.
-
#children ⇒ Object
(also: #cross_referenced_by)
Returns a list of all entries in the Bibliography containing a cross-reference to this entry or [] if there are no references to this entry.
-
#contained? ⇒ Boolean
Returns true if this entry is published inside a book, collection or journal.
- #container_title ⇒ Object
-
#content(options = {}) ⇒ Object
Returns a string of all the entry’s fields.
-
#convert(*filters) ⇒ Object
Returns a duplicate entry with all values converted using the filter(s).
-
#convert!(*filters) ⇒ Object
In-place variant of @see #convert.
- #date ⇒ Object
-
#delete(name) ⇒ Object
Removes the field with a given name from the entry.
-
#digest(filter = []) ⇒ String
Creates the entry’s digest based on the passed-in filters.
-
#each ⇒ Object
(also: #each_pair)
call-seq: entry.each { |key, value| block } -> entry entry.each_pair { |key, value| block } -> entry entry.each -> an_enumerator entry.each_pair -> an_enumerator.
- #fetch(name, default = nil) ⇒ Object
-
#field_names(filter = [], include_inherited = true) ⇒ Object
Returns a sorted list of the Entry’s field names.
-
#has_children? ⇒ Boolean
(also: #cross_referenced?)
Returns true if the entry is cross-referenced by another entry in the Bibliography.
- #has_field?(*names) ⇒ Boolean (also: #field?)
-
#has_parent? ⇒ Boolean
(also: #has_cross_reference?)
Returns true if the Entry has a valid cross-reference in the Bibliography.
- #has_type?(type) ⇒ Boolean (also: #type?)
- #identifier ⇒ Object
-
#inherited_fields ⇒ Object
Returns a sorted list of all field names referenced by this Entry’s cross-reference.
- #inherits?(*names) ⇒ 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 Entry’s key.
- #merge(other, filter = field_names) ⇒ Object
- #merge!(other, filter = field_names) ⇒ Object
- #method_missing(name, *args, &block) ⇒ Object
- #month=(month) ⇒ Object
-
#names ⇒ Object
Returns a list of all names (authors, editors, translators).
- #pages_from ⇒ Object
- #pages_to ⇒ Object
-
#parent ⇒ Object
(also: #cross_reference)
Returns the cross-referenced Entry from the Bibliography or nil if this Entry does define a cross-reference.
-
#parent_missing? ⇒ Boolean
(also: #cross_reference_missing?)
Returns true if the Entry cross-references an Entry which is not registered in the current Bibliography.
- #parse_month ⇒ Object (also: #parse_months)
-
#parse_names ⇒ Object
Parses all name values of the entry.
-
#provide(name) ⇒ Object
Returns the field value referenced by the passed-in name.
-
#provides?(*names) ⇒ Boolean
Returns true if the Entry has a field (or alias) for any of the passed-in names.
- #provides_or_inherits?(*names) ⇒ Boolean
-
#register(key) ⇒ Object
Registers this Entry in the associated Bibliographies entries hash.
-
#registered? ⇒ Boolean
Returns true if the Entry is currently registered with the associated Bibliography.
-
#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
-
#save_inherited_fields ⇒ Object
If the Entry has a cross-reference, copies all referenced all inherited values from the parent.
- #to_citeproc(options = {}) ⇒ Object
- #to_hash(options = {}) ⇒ Object
-
#to_rdf(options = {}) ⇒ Object
(also: #to_bibo)
Returns a RDF::Graph representation of the entry using the BIBO ontology.
-
#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.
-
#values_at(*arguments) ⇒ Object
Returns an array containing the values associated with the given keys.
Methods inherited from Element
#inspect, #matches?, #meets?, #meets_all?, #meets_any?, 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.
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/bibtex/entry.rb', line 146 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
353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/bibtex/entry.rb', line 353 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) when has_parent? && parent.provides?(name) parent.provide(name) else super end end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
141 142 143 |
# File 'lib/bibtex/entry.rb', line 141 def fields @fields end |
#type ⇒ Object
Returns the value of attribute type.
141 142 143 |
# File 'lib/bibtex/entry.rb', line 141 def type @type end |
Instance Method Details
#<=>(other) ⇒ Object
670 671 672 |
# File 'lib/bibtex/entry.rb', line 670 def <=>(other) type != other.type ? type <=> other.type : key != other.key ? key <=> other.key : to_s <=> other.to_s end |
#[](name) ⇒ Object Also known as: get
Returns the value of the field with the given name. If the value is not defined and the entry has cross-reference, returns the cross-referenced value instead.
396 397 398 |
# File 'lib/bibtex/entry.rb', line 396 def [](name) fields[name.to_sym] || parent && parent.provide(name) end |
#[]=(name, value) ⇒ Object
Adds a new field (name-value pair) to the entry. Returns the new value.
408 409 410 |
# File 'lib/bibtex/entry.rb', line 408 def []=(name, value) add(name.to_sym, value) end |
#add(*arguments) ⇒ Object Also known as: <<
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")
add(:author => Names.new(Name.new(:first => 'Edgar A.', :last => 'Poe')))
Adds a new field (name-value pair) or multiple fields to the entry. Returns the entry for chainability.
431 432 433 434 435 436 437 |
# File 'lib/bibtex/entry.rb', line 431 def add(*arguments) Hash[*arguments.flatten].each_pair do |name, value| fields[name.to_sym] = Value.create(value) end self end |
#added_to_bibliography(bibliography) ⇒ Object
Called when the element was added to a bibliography.
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/bibtex/entry.rb', line 494 def added_to_bibliography(bibliography) super @key = register(key) [:parse_names, :parse_months].each do |parser| send(parser) if bibliography.[parser] end if bibliography..has_key?(:filter) [*bibliography.[:filter]].each do |filter| convert!(filter) end end self end |
#aliases ⇒ Object
Returns the Entry’s field name aliases.
228 229 230 |
# File 'lib/bibtex/entry.rb', line 228 def aliases @aliases ||= FIELD_ALIASES.dup end |
#children ⇒ Object Also known as: cross_referenced_by
Returns a list of all entries in the Bibliography containing a cross-reference to this entry or [] if there are no references to this entry.
620 621 622 |
# File 'lib/bibtex/entry.rb', line 620 def children bibliography && bibliography.q("@entry[crossref=#{key}]") or [] end |
#contained? ⇒ Boolean
Returns true if this entry is published inside a book, collection or journal
639 640 641 |
# File 'lib/bibtex/entry.rb', line 639 def contained? has_field?(:booktitle, :container, :journal) end |
#container_title ⇒ Object
626 627 628 |
# File 'lib/bibtex/entry.rb', line 626 def container_title get(:booktitle) || get(:journal) || get(:container) end |
#content(options = {}) ⇒ Object
Returns a string of all the entry’s fields.
676 677 678 |
# File 'lib/bibtex/entry.rb', line 676 def content( = {}) fields.map { |k,v| "#{k} = #{ fields[k].to_s() }" }.join(",\n") end |
#convert(*filters) ⇒ Object
Returns a duplicate entry with all values converted using the filter(s). 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).
655 656 657 |
# File 'lib/bibtex/entry.rb', line 655 def convert(*filters) block_given? ? dup.convert!(*filters, &Proc.new) : dup.convert!(*filters) end |
#convert!(*filters) ⇒ Object
In-place variant of @see #convert
660 661 662 663 664 665 666 667 668 |
# File 'lib/bibtex/entry.rb', line 660 def convert!(*filters) filters = filters.flatten.map { |f| Filters.resolve!(f) } fields.each_pair do |k, v| (!block_given? || yield(k, v)) ? v.convert!(*filters) : v end self end |
#date ⇒ Object
560 561 562 |
# File 'lib/bibtex/entry.rb', line 560 def date get(:date) || get(:year) 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.
443 444 445 |
# File 'lib/bibtex/entry.rb', line 443 def delete(name) fields.delete(name.to_sym) end |
#digest(filter = []) ⇒ String
Creates the entry’s digest based on the passed-in filters.
The digest contains the type and all key-value pairs based on the passed in filter.
If a block is given, the computed digest will be passed to the block for post-processing (the entry itself will be passed as the second parameter).
468 469 470 471 472 473 474 475 476 477 478 |
# File 'lib/bibtex/entry.rb', line 468 def digest(filter = []) names = field_names(filter) digest = type.to_s names.zip(values_at(*names)).each do |key, value| digest << "|#{key}:#{value}" end digest = yield(digest, self) if block_given? digest end |
#each ⇒ Object Also known as: each_pair
call-seq:
entry.each { |key, value| block } -> entry
entry.each_pair { |key, value| block } -> entry
entry.each -> an_enumerator
entry.each_pair -> an_enumerator
Calls block once for each key in entry, passing the key-value pair as parameters.
If no block is given, an enumerator is returned instead.
215 216 217 218 219 220 221 222 |
# File 'lib/bibtex/entry.rb', line 215 def each if block_given? fields.each(&Proc.new) self else to_enum end end |
#fetch(name, default = nil) ⇒ Object
402 403 404 |
# File 'lib/bibtex/entry.rb', line 402 def fetch(name, default = nil) get(name) || block_given? ? yield(name) : default end |
#field_names(filter = [], include_inherited = true) ⇒ Object
Returns a sorted list of the Entry’s field names. If a filter
is passed as argument, returns all field names that are also defined by the filter. If the filter
is empty, returns all field names.
If the second optional argument is true (default) and the Entry contains a cross-reference, the list will include all inherited fields.
326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/bibtex/entry.rb', line 326 def field_names(filter = [], include_inherited = true) names = fields.keys if include_inherited && has_parent? names.concat(inherited_fields) end unless filter.empty? names = names & filter.map(&:to_sym) end names.sort! names end |
#has_children? ⇒ Boolean Also known as: cross_referenced?
Returns true if the entry is cross-referenced by another entry in the Bibliography.
611 612 613 |
# File 'lib/bibtex/entry.rb', line 611 def has_children? !children.empty? end |
#has_field?(*names) ⇒ Boolean Also known as: field?
270 271 272 273 274 |
# File 'lib/bibtex/entry.rb', line 270 def has_field?(*names) names.flatten.any? do |name| name.respond_to?(:to_sym) ? fields.has_key?(name.to_sym) : false end end |
#has_parent? ⇒ Boolean Also known as: has_cross_reference?
Returns true if the Entry has a valid cross-reference in the Bibliography.
586 587 588 |
# File 'lib/bibtex/entry.rb', line 586 def has_parent? !parent.nil? end |
#has_type?(type) ⇒ Boolean Also known as: type?
263 264 265 |
# File 'lib/bibtex/entry.rb', line 263 def has_type?(type) type.to_s.match(/^(?:entry|\*)$/i) || @type == type.to_sym || super end |
#identifier ⇒ Object
480 481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/bibtex/entry.rb', line 480 def identifier case when provides?(:doi) "info:doi/#{get(:doi)}" when provides?(:isbn) "urn:isbn:#{get(:isbn)}" when provides?(:issn) "urn:issn:#{get(:issn)}" else "urn:bibtex:#{key}" end end |
#inherited_fields ⇒ Object
Returns a sorted list of all field names referenced by this Entry’s cross-reference.
342 343 344 345 346 347 348 349 350 |
# File 'lib/bibtex/entry.rb', line 342 def inherited_fields return [] unless has_parent? names = parent.fields.keys - fields.keys names.concat(parent.aliases.reject { |k,v| !parent.has_field?(v) }.keys) names.sort! names end |
#inherits?(*names) ⇒ Boolean
278 279 280 281 282 |
# File 'lib/bibtex/entry.rb', line 278 def inherits?(*names) names.flatten.any? do |name| !has_field(name) && has_parent? && parent.provides?(name) end end |
#initialize_copy(other) ⇒ Object
157 158 159 160 161 162 163 164 |
# File 'lib/bibtex/entry.rb', line 157 def initialize_copy(other) @fields = {} self.type = other.type self.key = other.key add(other.fields) end |
#issued ⇒ Object Also known as: citeproc_date
722 723 724 725 726 727 |
# File 'lib/bibtex/entry.rb', line 722 def issued m = MONTHS.find_index(fields[:month].to_s.intern) unless !has_field?(:month) m = m + 1 unless m.nil? Hash['date-parts', [[fields[:year],m].compact.map(&:to_i)]] end |
#join ⇒ Object
544 545 546 547 |
# File 'lib/bibtex/entry.rb', line 544 def join fields.values.each(&:join) self end |
#key ⇒ Object Also known as: id
251 252 253 |
# File 'lib/bibtex/entry.rb', line 251 def key @key ||= default_key end |
#key=(key) ⇒ Object Also known as: id=
Sets the Entry’s key. If the Entry is currently registered with a Bibliography, re-registers the Entry with the new key; note that this may change the key value if another Entry is already regsitered with the same key.
Returns the new key.
238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/bibtex/entry.rb', line 238 def key=(key) key = key.to_s if registered? bibliography.entries.delete(@key) key = register(key) end @key = key rescue => e raise BibTeXError, "failed to set key to #{key.inspect}: #{e.}" end |
#merge(other, filter = field_names) ⇒ Object
166 167 168 |
# File 'lib/bibtex/entry.rb', line 166 def merge(other, filter = field_names) dup.merge!(other, filter) end |
#merge!(other, filter = field_names) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/bibtex/entry.rb', line 170 def merge!(other, filter = field_names) raise InvalidArgument, "failed to merge entries: type mismatch: #{type} #{other.type}" unless type == other.type other.each do |name, value| if has_field?(name) get(name).merge!(value) if filter.include?(name) else add name, value.dup end end self end |
#month=(month) ⇒ Object
549 550 551 |
# File 'lib/bibtex/entry.rb', line 549 def month=(month) fields[:month] = MONTHS_FILTER[month] end |
#names ⇒ Object
Returns a list of all names (authors, editors, translators).
580 581 582 |
# File 'lib/bibtex/entry.rb', line 580 def names NAME_FIELDS.map { |k| has_field?(k) ? @fields[k].tokens : nil }.flatten.compact end |
#pages_from ⇒ Object
630 631 632 |
# File 'lib/bibtex/entry.rb', line 630 def pages_from fetch(:pages, '').split(/\D+/)[0] end |
#pages_to ⇒ Object
634 635 636 |
# File 'lib/bibtex/entry.rb', line 634 def pages_to fetch(:pages, '').split(/\D+/)[-1] end |
#parent ⇒ Object Also known as: cross_reference
Returns the cross-referenced Entry from the Bibliography or nil if this Entry does define a cross-reference.
602 603 604 |
# File 'lib/bibtex/entry.rb', line 602 def parent bibliography && bibliography[fields[:crossref]] end |
#parent_missing? ⇒ Boolean Also known as: cross_reference_missing?
Returns true if the Entry cross-references an Entry which is not registered in the current Bibliography.
594 595 596 |
# File 'lib/bibtex/entry.rb', line 594 def parent_missing? has_field?(:crossref) && !has_parent? end |
#parse_month ⇒ Object Also known as: parse_months
553 554 555 556 |
# File 'lib/bibtex/entry.rb', line 553 def parse_month fields[:month] = MONTHS_FILTER[fields[:month]] if has_field?(:month) self end |
#parse_names ⇒ Object
Parses all name values of the entry. Tries to replace and join the value prior to parsing.
566 567 568 569 570 571 572 573 574 575 576 577 |
# File 'lib/bibtex/entry.rb', line 566 def parse_names strings = bibliography ? bibliography.strings.values : [] NAME_FIELDS.each do |key| if name = fields[key] name = name.dup.replace(strings).join.to_name fields[key] = name unless name.nil? end end self end |
#provide(name) ⇒ Object
Returns the field value referenced by the passed-in name. For example, this will return the ‘title’ value for ‘booktitle’ if a corresponding alias is defined.
302 303 304 305 306 |
# File 'lib/bibtex/entry.rb', line 302 def provide(name) return nil unless name.respond_to?(:to_sym) name = name.to_sym fields[name] || fields[aliases[name]] end |
#provides?(*names) ⇒ Boolean
Returns true if the Entry has a field (or alias) for any of the passed-in names.
285 286 287 288 289 290 291 292 293 |
# File 'lib/bibtex/entry.rb', line 285 def provides?(*names) names.flatten.any? do |name| if name.respond_to?(:to_sym) has_field?(name) || has_field?(aliases[name.to_sym]) else false end end end |
#provides_or_inherits?(*names) ⇒ Boolean
295 296 297 |
# File 'lib/bibtex/entry.rb', line 295 def provides_or_inherits?(*names) provides?(names) || inherits?(names) end |
#register(key) ⇒ Object
Registers this Entry in the associated Bibliographies entries hash. This method may change the Entry’s key, if another entry is already registered with the current key.
Returns the key or nil if the Entry is not associated with a Bibliography.
529 530 531 532 533 534 535 536 |
# File 'lib/bibtex/entry.rb', line 529 def register(key) return nil if bibliography.nil? k = key.dup k.succ! while bibliography.has_key?(k) bibliography.entries[k] = self k end |
#registered? ⇒ Boolean
Returns true if the Entry is currently registered with the associated Bibliography.
520 521 522 |
# File 'lib/bibtex/entry.rb', line 520 def registered? !!(bibliography && bibliography.entries[key].equal?(self)) end |
#removed_from_bibliography(bibliography) ⇒ Object
Called when the element was removed from a bibliography.
513 514 515 516 517 |
# File 'lib/bibtex/entry.rb', line 513 def removed_from_bibliography(bibliography) super bibliography.entries.delete(key) self end |
#rename(*arguments) ⇒ Object Also known as: rename_fields
Returns a copy of the Entry with all the field names renamed.
374 375 376 |
# File 'lib/bibtex/entry.rb', line 374 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.
380 381 382 383 384 385 386 387 388 |
# File 'lib/bibtex/entry.rb', line 380 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
538 539 540 541 542 |
# File 'lib/bibtex/entry.rb', line 538 def replace(*arguments) arguments = bibliography.q('@string') if arguments.empty? fields.values.each { |v| v.replace(*arguments) } self end |
#respond_to?(method) ⇒ Boolean
368 369 370 371 |
# File 'lib/bibtex/entry.rb', line 368 def respond_to?(method) provides?(method.to_sym) || method.to_s.match(/=$/) || method =~ /^(?:convert|from)_([a-z]+)(!)?$/ || (has_parent? && parent.respond_to?(method)) || super end |
#save_inherited_fields ⇒ Object
If the Entry has a cross-reference, copies all referenced all inherited values from the parent.
Returns the Entry.
312 313 314 315 316 317 318 |
# File 'lib/bibtex/entry.rb', line 312 def save_inherited_fields inherited_fields.each do |name| fields[name] = parent.provide(name) end self end |
#to_citeproc(options = {}) ⇒ Object
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 |
# File 'lib/bibtex/entry.rb', line 693 def to_citeproc( = {}) [:quotes] ||= [] parse_names parse_month hash = {} each_pair do |k,v| hash[CSL_FILTER[k].to_s] = v.to_citeproc() unless DATE_FIELDS.include?(k) end hash['id'] = key.to_s hash['type'] = CSL_TYPES[type].to_s case type when :mastersthesis hash['genre'] = "Master's thesis" when :phdthesis hash['genre'] = 'PhD thesis' else # empty end unless hash.key?('genre') hash['issued'] = citeproc_date hash end |
#to_hash(options = {}) ⇒ Object
686 687 688 689 690 691 |
# File 'lib/bibtex/entry.rb', line 686 def to_hash( = {}) [:quotes] ||= %w({ }) hash = { :key => key, :type => type } each_pair { |k,v| hash[k] = v.to_s() } hash end |
#to_rdf(options = {}) ⇒ Object Also known as: to_bibo
Returns a RDF::Graph representation of the entry using the BIBO ontology. TODO: improve level of detail captured by export
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 |
# File 'lib/bibtex/entry.rb', line 757 def to_rdf( = {}) require 'rdf' bibo = RDF::Vocabulary.new('http://purl.org/ontology/bibo/') graph = RDF::Graph.new entry = RDF::URI.new(identifier) graph << [entry, RDF.type, bibo[BIBO_TYPES[type]]] [:title, :language].each do |key| graph << [entry, RDF::DC[key], get(key).to_s] if field?(key) end graph << [entry, RDF::DC.date, get(:year).to_s] if field?(:year) if field?(:publisher) address = RDF::Vocabulary.new('http://schemas.talis.com/2005/address/schema#') pub = RDF::Node.new graph << [pub, RDF.type, RDF::FOAF[:Organization]] graph << [pub, RDF::FOAF.name, get(:publisher)] graph << [pub, address[:localityName], get(:address)] if field?(:address) graph << [entry, RDF::DC.published, pub] end [:doi, :edition, :volume].each do |key| graph << [entry, bibo[key], get(key).to_s] if field?(key) end if has_field?(:pages) if get(:pages).to_s =~ /^\s*(\d+)\s*-+\s*(\d+)\s*$/ graph << [entry, bibo[:pageStart], $1] graph << [entry, bibo[:pageEnd], $2] else graph << [entry, bibo[:pages], get(:pages).to_s] end end if has_field?(:author) seq = RDF::Node.new graph << [seq, RDF.type, RDF[:Seq]] graph << [entry, bibo[:authorList], seq] .each do || a = RDF::Node.new graph << [a, RDF.type, RDF::FOAF[:Person]] if .is_a?(Name) [:given, :family, :prefix, :suffix].each do |part| graph << [a, bibo["#{part}Name"], .send(part).to_s] end else graph << [a, RDF::FOAF.name, .to_s] end graph << [entry, RDF::DC.creator, a] graph << [seq, RDF.li, a] end end if has_field?(:editor) seq = RDF::Node.new graph << [seq, RDF.type, RDF[:Seq]] graph << [entry, bibo[:editorList], seq] editors.each do |editor| e = RDF::Node.new graph << [e, RDF.type, RDF::FOAF[:Person]] if editor.is_a?(Name) [:given, :family, :prefix, :suffix].each do |part| graph << [e, bibo["#{part}Name"], editor.send(part).to_s] end else graph << [e, RDF::FOAF.name, editor.to_s] end graph << [entry, bibo.editor, a] graph << [seq, RDF.li, e] end end graph rescue LoadError BibTeX.log.error "Please gem install rdf for RDF support." end |
#to_s(options = {}) ⇒ Object
Returns a string representation of the entry.
681 682 683 684 |
# File 'lib/bibtex/entry.rb', line 681 def to_s( = {}) [:quotes] ||= %w({ }) ["@#{type}{#{key},", content().gsub(/^/,' '), "}\n"].join("\n") end |
#to_xml(options = {}) ⇒ Object
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 |
# File 'lib/bibtex/entry.rb', line 731 def to_xml( = {}) require 'rexml/document' xml = REXML::Element.new('bibtex:entry') xml.attributes['id'] = key entry = REXML::Element.new("bibtex:#{type}") fields.each do |key, value| field = REXML::Element.new("bibtex:#{key}") if [:extended] && value.name? value.each { |n| entry.add_element(n.to_xml) } else field.text = value.to_s() end entry.add_element(field) end xml.add_element(entry) 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.
449 450 451 452 453 |
# File 'lib/bibtex/entry.rb', line 449 def valid? REQUIRED_FIELDS[@type].all? do |f| f.is_a?(Array) ? !(f & fields.keys).empty? : !fields[f].nil? end end |
#values_at(*arguments) ⇒ Object
Returns an array containing the values associated with the given keys.
644 645 646 647 648 |
# File 'lib/bibtex/entry.rb', line 644 def values_at(*arguments) arguments.map do |key| get key end end |