Class: BibTeX::BibTeXFile

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rbibtex/types.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*elements) ⇒ BibTeXFile

Returns a new instance of BibTeXFile.



262
263
264
# File 'lib/rbibtex/types.rb', line 262

def initialize(*elements)
  @elements = elements.compact
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



255
256
257
# File 'lib/rbibtex/types.rb', line 255

def elements
  @elements
end

Class Method Details

.parse(string) ⇒ Object

Create a File from a string



267
268
269
# File 'lib/rbibtex/types.rb', line 267

def self.parse(string)
  Parser.new.parse(string)
end

Instance Method Details

#abbreviation(key) ⇒ Object

Get an abbrevation by key



303
304
305
# File 'lib/rbibtex/types.rb', line 303

def abbreviation(key)
  abbreviations.select { | a | norm_key(a.key) == norm_key(key) }.first
end

#abbreviationsObject

Return an array with the abbrevations in this file



298
299
300
# File 'lib/rbibtex/types.rb', line 298

def abbreviations
  @elements.select { | element | element.is_a?Abbreviation }
end

#each(&block) ⇒ Object



257
258
259
# File 'lib/rbibtex/types.rb', line 257

def each &block
  @elements.each &block
end

#entriesObject

Return all entries in this file.



272
273
274
# File 'lib/rbibtex/types.rb', line 272

def entries
  @elements.select { | e | e.is_a?Entry }
end

#has_abbreviation?(key) ⇒ Boolean

Check if abbreviation is defined

Returns:

  • (Boolean)


308
309
310
# File 'lib/rbibtex/types.rb', line 308

def has_abbreviation?(key)
  abbreviation(key) != nil
end

#interpolate!Object

Replace abbreviations by their string equivalent. (Does not replace predefined abbreviations like jan, feb if they are not explicitely specified in this bibtex file



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/rbibtex/types.rb', line 326

def interpolate!
  self.entries.each do | entry |
	entry.each do | (k, v) |
      interpolated = v.map{ | e | 
 if e.is_a?AbbreviationRef and has_abbreviation?(e)
   abbreviation(e).string
 else
   e
 end
      }
	  shrunk = interpolated.normalize
 entry[k] = Value.new(*shrunk)
	end
  end
end

#normalize!Object

  • Remove additional whitespace from the filelist

  • Deindent and unwrap text



314
315
316
317
318
319
320
321
# File 'lib/rbibtex/types.rb', line 314

def normalize!
  @elements = @elements.reject { | e | e.is_a?Space }
  @elements.map! { | e |
	if e.respond_to?:normalize
	  next e.normalize
	end
  }
end

#sort_by(*keys) ⇒ Object

When keys are given sorts the bibtex file according to the keys, discarding any empty spaces and sorting comments and strings to the top.

When a block is given the sortorder need to be inferred in the block



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/rbibtex/types.rb', line 280

def sort_by(*keys)
  raise "Give either a block or keys to sort on" if block_given? and !keys.empty?      
  self.normalize!
  @elements = @elements.sort_by { | element |
	case element
	when Abbreviation
	  [0, element.key]
	when Comment
	  [1, element.to_s]
	when Entry
	  [2, keys.map { | k | (element[k] || "").to_s }]
	else 
	  [10]
	end
  } 
end