Class: BibTexFile
- Inherits:
-
Object
- Object
- BibTexFile
- Defined in:
- lib/rbbt/sources/bibtex.rb
Defined Under Namespace
Classes: Entry
Class Method Summary collapse
- .clean_string(string) ⇒ Object
- .load_file(file) ⇒ Object
- .parse_bibtex(bibtex) ⇒ Object
- .parse_entry(entry) ⇒ Object
Instance Method Summary collapse
- #add(bibtex) ⇒ Object
- #entries ⇒ Object
- #entry(bibentry) ⇒ Object
-
#initialize(file) ⇒ BibTexFile
constructor
A new instance of BibTexFile.
- #save(file) ⇒ Object
Constructor Details
#initialize(file) ⇒ BibTexFile
Returns a new instance of BibTexFile.
93 94 95 |
# File 'lib/rbbt/sources/bibtex.rb', line 93 def initialize(file) @entries = BibTexFile.load_file(file) end |
Class Method Details
.clean_string(string) ⇒ Object
53 54 55 |
# File 'lib/rbbt/sources/bibtex.rb', line 53 def self.clean_string(string) string.gsub(/[{}]/,'') end |
.load_file(file) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rbbt/sources/bibtex.rb', line 73 def self.load_file(file) entries = {} case when File.exists?(file) self.parse_bibtex File.open(file).read when IO === file self.parse_bibtex file.read when String === file self.parse_bibtex file else raise "Input format not recognized" end.each do |entry| type, name, info = self.parse_entry entry entries[name] = Entry.new name, type, info end entries end |
.parse_bibtex(bibtex) ⇒ Object
57 58 59 |
# File 'lib/rbbt/sources/bibtex.rb', line 57 def self.parse_bibtex(bibtex) bibtex.scan(/@\w+\{.*?^\}\s*/m) end |
.parse_entry(entry) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rbbt/sources/bibtex.rb', line 61 def self.parse_entry(entry) info = {} type, name = entry.match(/@([^\s]+)\{([^\s]+)\s*,/).values_at(1,2) entry.scan(/\s*(.*?)\s*=\s*\{?\s*(.*?)\s*\}?\s*,?\s*$/).each do |pair| info[pair.first.chomp] = pair.last.chomp end [ type.chomp, name.chomp, info] end |
Instance Method Details
#add(bibtex) ⇒ Object
111 112 113 114 |
# File 'lib/rbbt/sources/bibtex.rb', line 111 def add(bibtex) type, name, info = BibTexFile.parse_entry bibtex @entries[name] = BibTexFile::Entry.new name, type, info end |
#entries ⇒ Object
116 117 118 |
# File 'lib/rbbt/sources/bibtex.rb', line 116 def entries @entries.keys end |
#entry(bibentry) ⇒ Object
120 121 122 |
# File 'lib/rbbt/sources/bibtex.rb', line 120 def entry(bibentry) @entries[bibentry] end |
#save(file) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rbbt/sources/bibtex.rb', line 97 def save(file) text = entries.collect{|e| entry e }.sort{|a,b| if a.year.to_i != b.year.to_i a.year.to_i <=> b.year.to_i else a.name <=> b.name end }.reverse.collect do |entry| entry.to_s end * "\n" File.open(file, 'w') do |fout| fout.puts text end end |