Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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<b>, 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
Expand Down
4 changes: 3 additions & 1 deletion lib/bibtex/bibliography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
15 changes: 9 additions & 6 deletions lib/bibtex/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions test/bibtex/test_bibliography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 59 additions & 0 deletions test/bibtex/test_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/test_bibtex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading