The base class for BibTeX objects.
Returns an array of BibTeX elements.
# File lib/bibtex/elements.rb, line 31 def self.parse(input, options = {}) case input when Element [input] when Hash [Entry.new(input)] when Array input.inject([]) { |s,a| s.concat(parse(a, options)) } when ::String Parser.new(options).parse(input).data.each do |e| e.parse_names unless !e.respond_to?(:parse_names) || options[:parse_names] == false e.parse_month unless !e.respond_to?(:parse_month) || options[:parse_months] == false end else raise ArgumentError, "failed to parse Element from #{input.inspect}" end end
# File lib/bibtex/elements.rb, line 181 def <=>(other) return nil unless other.respond_to? :type and other.respond_to? :to_s [type, to_s] <=> [other.type, other.to_s] end
Called when the element was added to a bibliography.
# File lib/bibtex/elements.rb, line 169 def added_to_bibliography(bibliography) # raise BibTeXError, "failed to add element to Bibliography: already registered with another Bibliography" unless @bibliography.nil? @bibliography = bibliography self end
Returns a string containing the object's content.
# File lib/bibtex/elements.rb, line 50 def content(options = {}) '' end
# File lib/bibtex/elements.rb, line 59 def digest(*arguments) [type, content].join('|') end
# File lib/bibtex/elements.rb, line 82 def has_type?(type) self.type == type.intern || defined?(type) == 'constant' && is_a?(type) end
Returns the element's id.
# File lib/bibtex/elements.rb, line 70 def id; @id ||= object_id.to_s; end
Returns the Element as a nicely formatted string.
# File lib/bibtex/elements.rb, line 187 def inspect "#<#{self.class} #{content.gsub(/\n/, ' ')}>" end
Invokes BibTeX string joining on this element.
# File lib/bibtex/elements.rb, line 67 def join; self; end
Returns true if the element matches the given query.
# File lib/bibtex/elements.rb, line 92 def matches?(query) return true if query.nil? || query.respond_to?(:empty?) && query.empty? case query when Symbol query.to_s == id.to_s when Element query == self when Regexp to_s.match(query) when /^\/(.+)\/$/ to_s.match(Regexp.new($1)) when /@(\*|\w+)(?:\[([^\]]*)\])?/ query.scan(/(!)?@(\*|\w+)(?:\[([^\]]*)\])?/).any? do |non, type, condition| if (non ? !has_type?(type) : has_type?(type)) if condition.nil? || condition.empty? true else condition.to_s.split(/\s*\|\|\s*/).any? do |conditions| meets_all? conditions.split(/\s*(?:,|&&)\s*/) end end end end else id.to_s == query end end
Returns true if the element meets all or any of the given conditions.
# File lib/bibtex/elements.rb, line 135 def meets?(conditions, op = :all?) conditions.send(op) do |condition| meets_condition? condition end end
# File lib/bibtex/elements.rb, line 124 def meets_all?(*conditions) meets? conditions.flatten, :all? end
# File lib/bibtex/elements.rb, line 129 def meets_any?(*conditions) meets? conditions.flatten, :any? end
Returns a list of names for that Element. All Elements except Entries return an empty list.
# File lib/bibtex/elements.rb, line 78 def names [] end
Called when the element was removed from a bibliography.
# File lib/bibtex/elements.rb, line 176 def removed_from_bibliography(bibliography) @bibliography = nil self end
Invokes BibTeX string replacement on this element.
# File lib/bibtex/elements.rb, line 64 def replace(*arguments); self; end
# File lib/bibtex/elements.rb, line 144 def to_hash(options = {}) { type => content } end
# File lib/bibtex/elements.rb, line 153 def to_json(options = {}) # Some JSON implementations pass an argument # to this method. options = {} unless options.is_a?(::Hash) ::JSON.dump(to_hash(options)) end
# File lib/bibtex/elements.rb, line 161 def to_xml(options = {}) require 'rexml/document' xml = REXML::Element.new(type) xml.text = content xml end
# File lib/bibtex/elements.rb, line 148 def to_yaml(options = {}) require 'yaml' to_hash.to_yaml end
Returns the BibTeX type (if applicable) or the normalized class name.
# File lib/bibtex/elements.rb, line 73 def type self.class.name.split(/::/).last.gsub(/([[:lower:]])([[:upper:]])/) { "#{$1}_#{$2}" }.downcase.intern end
Returns a string containing the object's content.
# File lib/bibtex/elements.rb, line 55 def values_at(*arguments) [] end
# File lib/bibtex/elements.rb, line 193 def meets_condition?(condition) property, operator, value = condition.split(/\s*([!~\/\^<>]?=|!~)\s*/) if property.nil? true else property.strip! value.strip! unless value.nil? if operator.nil? && value.nil? respond_to?(:provides?) && provides?(property) else # Hack: we need to get rid of #type returning the bibtex_type, # because type is a valid BibTeX property. This mitigates the # issue but is no fix! if property == 'type' actual = respond_to?(:fields) ? fields[:type] : nil else actual = respond_to?(property) ? send(property) : nil end case operator when '!=', '/=' actual.nil? || actual.to_s != value when '^=' !actual.nil? && actual.to_s.match("^#{value}") when '~=' !actual.nil? && actual.to_s.match(value) when '!~' actual.nil? || !actual.to_s.match(value) when '<=' !actual.nil? && actual.to_i <= value.to_i when '>=' !actual.nil? && actual.to_i >= value.to_i else !actual.nil? && actual.to_s == value end end end end