diff --git a/History.txt b/History.txt index 59a1366..7c2ec9d 100644 --- a/History.txt +++ b/History.txt @@ -17,6 +17,8 @@ Unreleased * Accept every character BibTeX allows in identifiers in field names, @string names and macro references, e.g. x;y, p[1], a|b, a^b, a~b, a, a@b and @foo; these used to raise an unexpected-token error. An '@' followed by a name and '{' or '(' still starts a new entry, and a name that starts with '@' ends before the next '@' * Bibliography#save_to (and #save) replace the file atomically, so a failure while serialising or writing leaves the existing file untouched instead of empty or partly written; symlinked targets and permission bits are kept, and devices, pipes and files in read-only directories are written directly * Bibliography#save_to no longer adds :quotes to the options hash passed in +* Entry.new no longer removes :bibtex_type and :bibtex_key from the hash passed to it, and accepts a frozen hash +* Entry#to_s and #to_hash, and so Bibliography#to_s, #to_a and #to_hash, no longer add :quotes to the options hash passed in, and accept a frozen hash * Reimplement Entry's dynamic field writers, Bibliography#save_to and the lexer's identifier and key rules from a written specification, without the code they replace (#154) 6.2.0 / 2025-07-20 diff --git a/lib/bibtex/bibliography.rb b/lib/bibtex/bibliography.rb index ff2353b..3e617c2 100644 --- a/lib/bibtex/bibliography.rb +++ b/lib/bibtex/bibliography.rb @@ -390,6 +390,7 @@ def sort_by!(*, &) # Returns a string representation of the bibliography. def to_s(options = {}) + options = options.merge(quotes: %w[{ }]) unless options[:quotes] map { |o| o.to_s(options) }.join end @@ -398,12 +399,13 @@ def inspect end def to_a(options = {}) + options = options.merge(quotes: %w[{ }]) unless options[:quotes] map { |o| o.to_hash(options) } end # Returns a Ruby hash representation of the bibliography. def to_hash(options = {}) - { bibliography: map { |o| o.to_hash(options) } } + { bibliography: to_a(options) } end # Returns a YAML representation of the bibliography. diff --git a/lib/bibtex/entry.rb b/lib/bibtex/entry.rb index f0fe30b..40d31a3 100644 --- a/lib/bibtex/entry.rb +++ b/lib/bibtex/entry.rb @@ -69,10 +69,13 @@ def initialize(attributes = {}) @fields = {} @key = nil - self.type = attributes.delete(:bibtex_type) if attributes.key?(:bibtex_type) - self.key = attributes.delete(:bibtex_key) if attributes.key?(:bibtex_key) - - add(attributes) + attributes.each_pair do |name, value| + case name + when :bibtex_type then self.type = value + when :bibtex_key then self.key = value + else fields[name.to_sym] = Value.create(value) + end + end yield self if block_given? end @@ -610,12 +613,12 @@ def content(options = {}) # Returns a string representation of the entry. def to_s(options = {}) - options[:quotes] ||= %w[{ }] + options = options.merge(quotes: %w[{ }]) unless options[:quotes] ["@#{type}{#{key},", content(options).gsub(/^/, ' '), "}\n"].join("\n") end def to_hash(options = {}) - options[:quotes] ||= %w[{ }] + options = options.merge(quotes: %w[{ }]) unless options[:quotes] hash = { bibtex_key: key, bibtex_type: type } each_pair { |k, v| hash[k] = v.to_s(options) } hash diff --git a/test/bibtex/test_bibliography.rb b/test/bibtex/test_bibliography.rb index 7d79e27..5c76de2 100644 --- a/test/bibtex/test_bibliography.rb +++ b/test/bibtex/test_bibliography.rb @@ -282,6 +282,25 @@ class BibliographyTest < Minitest::Spec end end + describe '#to_s, #to_a and #to_hash' do + it 'leave the options hash unchanged' do + options = {} + @bib.to_s(options) + @bib.to_a(options) + @bib.to_hash(options) + + _(options).must_be_empty + end + + it 'accept a frozen options hash' do + options = {}.freeze + + _(@bib.to_s(options)).must_include '@book{rails,' + _(@bib.to_a(options)).wont_be_empty + _(@bib.to_hash(options)[:bibliography]).wont_be_empty + end + end + describe 'duplicates' do it 'understands select_duplicates_by' do assert_equal 1, @bib.select_duplicates_by.length diff --git a/test/bibtex/test_entry.rb b/test/bibtex/test_entry.rb index 1d00e3d..80a8f12 100644 --- a/test/bibtex/test_entry.rb +++ b/test/bibtex/test_entry.rb @@ -14,6 +14,37 @@ def apply(value) it "won't be nil" do _(Entry.new).wont_be_nil end + + describe 'given an attribute hash' do + before do + @attributes = { bibtex_type: :book, bibtex_key: 'rails', title: 'Agile Web Development', year: 2009 } + end + + it 'takes type and key from the hash and the remaining keys as fields' do + entry = Entry.new(@attributes) + + _(entry.type).must_equal :book + _(entry.key).must_equal 'rails' + _(entry.fields.keys).must_equal %i[title year] + _(entry[:title]).must_equal 'Agile Web Development' + _(entry[:year]).must_equal '2009' + end + + it 'leaves the hash unchanged' do + expected = @attributes.dup + Entry.new(@attributes) + + _(@attributes).must_equal expected + end + + it 'accepts a frozen hash' do + entry = Entry.new(@attributes.freeze) + + _(entry.type).must_equal :book + _(entry.key).must_equal 'rails' + _(entry.fields.keys).must_equal %i[title year] + end + end end describe '#add' do @@ -617,6 +648,34 @@ def @filter.apply(value) "month = dec\n" \ "}\n", @bib['foo'].to_s(filter: :linebreaks) end + + it 'leaves the options hash unchanged' do + options = { filter: :linebreaks } + @bib['foo'].to_s(options) + + _(options).must_equal({ filter: :linebreaks }) + end + + it 'accepts a frozen options hash' do + _(@bib['foo'].to_s({}.freeze)).must_include 'title = {A' + end + end + + describe '#to_hash' do + before do + @entry = Entry.new(bibtex_type: :book, bibtex_key: 'rails', title: 'Rails') + end + + it 'leaves the options hash unchanged' do + options = {} + @entry.to_hash(options) + + _(options).must_be_empty + end + + it 'accepts a frozen options hash' do + _(@entry.to_hash({}.freeze)[:title]).must_equal '{Rails}' + end end describe 'default keys' do diff --git a/test/test_bibtex.rb b/test/test_bibtex.rb index 4d204ad..9be5048 100644 --- a/test/test_bibtex.rb +++ b/test/test_bibtex.rb @@ -113,7 +113,7 @@ def test_roundtrip def test_construct file = File.read(Test.fixtures(:roundtrip)) bib = BibTeX::Bibliography.new - bib << BibTeX::Entry.new(RAILS_ATTRIBUTES.dup) # Entry.new deletes keys from its argument + bib << BibTeX::Entry.new(RAILS_ATTRIBUTES) assert_equal file.gsub(/\s+/, ''), bib.to_s.gsub(/\s+/, '') end