Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bibliography transformation and saving

  • Loading branch information...
commit 65fe95310b41d1065a8f484f0648f472def384ca 1 parent d3a5a13
@nickg authored
View
45 bibtex/bibliography.rb
@@ -0,0 +1,45 @@
+require 'bibtex/entry'
+
+module BibTeX
+
+ class Bibliography
+ attr_reader :entries
+
+ def initialize
+ @entries = {}
+ end
+
+ def <<(e)
+ if e.kind_of? Entry then
+ @entries[e.key] = e
+ else
+ raise 'Cannot add non-entries to bibliography'
+ end
+ end
+
+ def [](key)
+ @entries[key] or raise "No entry #{key}"
+ end
+
+ # Transform the entries in some way and return a
+ # new bibliography
+ def map
+ r = Bibliography.new
+ @entries.each do |k, e|
+ r << yield(e)
+ end
+ return r
+ end
+
+ def save(filename)
+ f = File.new(filename, 'w')
+ f.puts self.to_s
+ f.close
+ end
+
+ def to_s
+ @entries.keys.sort.collect { |k| @entries[k].to_s }.join
+ end
+ end
+
+end
View
15 bibtex/entry.rb
@@ -28,7 +28,7 @@ def [](key)
end
def to_s
- fs = @fields.collect { |k, f| " #{f.to_s}" }.join ",\n"
+ fs = @fields.collect { |k, f| " #{f.to_s}" }.sort.join ",\n"
"@#{@type}{#{@key},\n#{fs}\n}\n\n"
end
@@ -52,6 +52,19 @@ def select_fields(keys)
# Different types of entries
module EntryType
Book = 'book'
+ Article = 'article'
+ Booklet = 'booklet'
+ Conference = 'conference'
+ InBook = 'inbook'
+ InCollection = 'incollection'
+ InProceedings = 'inproceedings'
+ Manual = 'manual'
+ MastersThesis = 'mastersthesis'
+ Misc = 'misc'
+ PhDThesis = 'phdthesis'
+ Proceedings = 'proceedings'
+ TechReport = 'techreport'
+ Unpublished = 'unpublished'
end
end
View
76 bibtex/test_bibliography.rb
@@ -0,0 +1,76 @@
+require 'bibtex/bibliography'
+require 'test/unit'
+
+class TestBibliography < Test::Unit::TestCase
+ include BibTeX
+
+ def setup
+ @b = Bibliography.new
+
+ @foo01 = Entry.new(EntryType::Book, 'foo01')
+ @foo01.add_field :author, 'C. Doof'
+ @foo01.add_field :year, 2007
+ @foo01.add_field Field.new(:url, 'www.doof.me.uk')
+
+ @bar99 = Entry.new(EntryType::Article, 'bar99')
+ @bar99.add_field :author, 'N. Cakesniffer'
+ @bar99.add_field :year, 1999
+ @bar99.add_field Field.new(:url, 'www.cakesniffer.co.uk')
+
+ @b << @foo01
+ @b << @bar99
+ end
+
+ def test_basic
+ assert_equal 2, @b.entries.length
+ assert_equal @foo01, @b['foo01']
+ end
+
+ def test_map
+ expect = <<END
+@article{bar99,
+ author = {N. Cakesniffer},
+ year = {1999}
+}
+
+@book{foo01,
+ author = {C. Doof},
+ year = {2007}
+}
+
+END
+ urlless = @b.map do |e|
+ e.reject_fields [:url]
+ end
+ assert_equal expect, urlless.to_s
+ end
+
+ def test_to_s
+ expect = <<END
+@article{bar99,
+ author = {N. Cakesniffer},
+ url = {www.cakesniffer.co.uk},
+ year = {1999}
+}
+
+@book{foo01,
+ author = {C. Doof},
+ url = {www.doof.me.uk},
+ year = {2007}
+}
+
+END
+ assert_equal expect, @b.to_s
+ end
+
+ def test_save
+ fname = '/tmp/_test.bib'
+ @b.save fname
+
+ f = File.new(fname)
+ assert_equal @b.to_s, f.read
+ f.close
+ File.delete fname
+ end
+
+end
View
4 bibtex/test_entry.rb
@@ -33,8 +33,8 @@ def test_to_s
expect = <<END
@book{foo01,
author = {C. Doof},
- year = {2007},
- url = {www.doof.me.uk}
+ url = {www.doof.me.uk},
+ year = {2007}
}
END
Please sign in to comment.
Something went wrong with that request. Please try again.