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
4 changes: 4 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Unreleased
* 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)
* Behaviour change: the query condition "prop=~re" now matches a regular expression, like "prop~=re". It used to be read as the equality "prop=" with the value "~re", which could never have been intended
* Fix Element#type? and Entry#type? raising NoMethodError when given a class, e.g. entry.type?(BibTeX::Entry)
* Fix BibTeX::String#to_xml raising NoMethodError, and Element#to_xml producing an empty tag name for elements whose type is a symbol
* Fix Names#add raising TypeError when given a string, and Name.looks_like? raising NameError and returning the inverse of its documented result

6.2.0 / 2025-07-20
==================
Expand Down
16 changes: 8 additions & 8 deletions lib/bibtex/elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def names
end

def type?(type)
self.type == type.intern || (defined?(type) == 'constant' && is_a?(type))
type.is_a?(Module) ? is_a?(type) : self.type == type.intern
end

alias has_type? type?
Expand Down Expand Up @@ -169,7 +169,7 @@ def to_json(options = {})

def to_xml(_options = {})
require 'rexml/document'
xml = REXML::Element.new(type)
xml = REXML::Element.new(type.to_s)
xml.text = content
xml
end
Expand Down Expand Up @@ -201,7 +201,7 @@ def inspect
private

def meets_condition?(condition)
property, operator, value = condition.split(%r{\s*([!~/^<>]?=|!~|=~|<|>)\s*})
property, operator, value = condition.split(%r{\s*(=~|[!~/^<>]?=|!~|<|>)\s*})

if property.nil?
true
Expand Down Expand Up @@ -318,15 +318,15 @@ def to_hash(_options = {})
def to_xml(_options = {})
require 'rexml/document'

xml = REXML::Element.new(:string)
xml = REXML::Element.new('string')

k = REXML::Element.new(:key)
v = REXML::Element.new(:value)
k = REXML::Element.new('key')
v = REXML::Element.new('value')
k.text = key.to_s
v.text = value.to_s(quotes: '"')

xml.add_elements(k)
xml.add_elements(v)
xml.add_element(k)
xml.add_element(v)

xml
end
Expand Down
2 changes: 2 additions & 0 deletions lib/bibtex/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ def type=(type)
end

def type?(type)
return super if type.is_a?(Module)

type.to_s.match(/^(?:entry|\*)$/i) || @type.casecmp?(type.to_sym) || super
end

Expand Down
4 changes: 2 additions & 2 deletions lib/bibtex/names.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def add(name)
if name.is_a?(Name)
@tokens << name
elsif name.respond_to?(:to_s)
@tokens += Names.parse(name.to_s)
@tokens += Names.parse(name.to_s).to_a
else
raise ArgumentError, "failed to add #{name.inspect}: not a name."
end
Expand Down Expand Up @@ -132,7 +132,7 @@ def parse(string)
# Returns true if thing looks like a name.
# Actually converts thing to a string and tries to parse it.
def looks_like?(thing)
thing.respond_to?(:to_s) && [Name.new.parse(string)].flatten.compact.empty?
thing.respond_to?(:to_s) && !Name.parse(thing.to_s).nil?
end
end

Expand Down
50 changes: 50 additions & 0 deletions test/bibtex/test_bibliography_rdf_converter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

require 'helper'

module BibTeX
class BibliographyRDFConverterTest < Minitest::Spec
before do
skip 'RDF support not available' unless defined?(::RDF)
end

let(:bibliography) do
BibTeX.parse <<~BIB
@book{a, author = {Doe, Jane}, title = {First}, year = {2001}}
@article{b, author = {Doe, Jane and Roe, Rick}, title = {Second}, journal = {J}, year = {2002}}
@string{foo = "bar"}
BIB
end

describe '.convert' do
it 'returns an RDF graph with one statement set per entry' do
graph = BibTeX::Bibliography::RDFConverter.convert(bibliography)

assert_kind_of ::RDF::Graph, graph
refute_empty graph
titles = graph.query([nil, ::RDF::Vocab::DC.title, nil]).map { |s| s.object.to_s }

assert_includes titles, 'First'
assert_includes titles, 'Second'
end

it 'returns an empty graph for an empty bibliography' do
assert_empty BibTeX::Bibliography::RDFConverter.convert(BibTeX::Bibliography.new)
end

it 'adds to a supplied graph' do
graph = ::RDF::Graph.new
result = BibTeX::Bibliography::RDFConverter.new(bibliography, graph).convert!

assert_same graph, result
refute_empty graph
end
end

describe 'Bibliography#to_rdf' do
it 'returns a graph' do
assert_kind_of ::RDF::Graph, bibliography.to_rdf
end
end
end
end
25 changes: 25 additions & 0 deletions test/bibtex/test_compatibility.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require 'helper'

module BibTeX
class TestCompatibility < Minitest::Test
def test_transliterate_ascii_is_unchanged
assert_equal 'Poe, Edgar Allen', BibTeX.transliterate('Poe, Edgar Allen')
end

def test_transliterate_strips_diacritics
assert_equal 'Muller', BibTeX.transliterate('Müller')
end

def test_transliterate_drops_characters_without_ascii_form
assert_equal 'ab', BibTeX.transliterate('a中b')
end

def test_transliterate_falls_back_when_normalization_fails
binary = 'café ok'.b

assert_equal 'caf ok', BibTeX.transliterate(binary)
end
end
end
Loading
Loading