Skip to content

Commit

Permalink
Fixes name parsing of nested braced expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
inukshuk committed Jun 6, 2011
1 parent 2680cf4 commit 762fa80
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 22 deletions.
4 changes: 4 additions & 0 deletions History.txt
@@ -1,3 +1,7 @@
=== 1.3.3

* Fixed name parsing of nested braced expressions

=== 1.3.2

* Fixed numeric keys issue
Expand Down
2 changes: 2 additions & 0 deletions features/names.feature
Expand Up @@ -64,6 +64,8 @@ Feature: BibTeX Names
| Ludwig von Beethoven | Ludwig | von | Beethoven | |
| von Beethoven, Ludwig | Ludwig | von | Beethoven | |
| {von Beethoven}, Ludwig | Ludwig | | {von Beethoven} | |
| {{von} Beethoven}, Ludwig | Ludwig | | {{von} Beethoven} | |
| John {}Paul Jones | John {}Paul | | Jones | |
| Ford, Jr., Henry | Henry | | Ford | Jr. |
| Brinch Hansen, Per | Per | | Brinch Hansen | |
| {Barnes and Noble, Inc.} | | | {Barnes and Noble, Inc.} | |
Expand Down
4 changes: 2 additions & 2 deletions lib/bibtex.rb
Expand Up @@ -50,8 +50,8 @@ def self.log; BibTeX::Log end
end

# Load debugger
# require 'ruby-debug'
# Debugger.start
require 'ruby-debug'
Debugger.start

require 'bibtex/extensions'
require 'bibtex/value'
Expand Down
1 change: 1 addition & 0 deletions lib/bibtex/elements.rb
Expand Up @@ -139,6 +139,7 @@ def removed_from_bibliography(bibliography)
end

def <=>(other)
return nil unless other.respond_to? :type and other.respond_to? :to_s
[type, to_s] <=> [other.type, other.to_s]
end

Expand Down
2 changes: 1 addition & 1 deletion lib/bibtex/names.rb
Expand Up @@ -143,7 +143,7 @@ def to_hash
[:strip!, :upcase!, :downcase!, :sub!, :gsub!, :chop!, :chomp!, :rstrip!].each do |method_id|
define_method(method_id) do |*arguments, &block|
each do |part|
part.send(method_id, *arguments, &block)
part.send(method_id, *arguments, &block) unless part.nil?
end
self
end
Expand Down
33 changes: 17 additions & 16 deletions lib/bibtex/names.y
Expand Up @@ -136,7 +136,8 @@ require 'strscan'
@word[1] << @src.matched

when @src.scan(/\{/o)
scan_literal(@src.matched)
@word[1] << @src.matched
scan_literal

when @src.scan(/\}/o)
error_unbalanced
Expand Down Expand Up @@ -165,21 +166,21 @@ require 'strscan'
@word[0] = :UWORD if @word[0] == :PWORD
end

def scan_literal(content = '')
@brace_level += 1
content << @src.scan_until(/[^\\][\{\}]/o).to_s # TODO accept empty braces {}
case @src.matched
when /\{/
scan_braced_expression(content)
when /\}/
@brace_level -= 1
if @brace_level >= 0
@word[1] << content
else
error_unbalanced
end
else
error_unbalanced
def scan_literal
@brace_level = 1

while @brace_level > 0
@word[1] << @src.scan_until(/[\{\}]/o).to_s

case @src.matched
when '{'
@brace_level += 1
when '}'
@brace_level -= 1
else
@brace_level = 0
error_unbalanced
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/bibtex/value.rb
Expand Up @@ -88,7 +88,7 @@ def add(argument)
[:strip!, :upcase!, :downcase!, :sub!, :gsub!, :chop!, :chomp!, :rstrip!].each do |method_id|
define_method(method_id) do |*arguments, &block|
@tokens.each do |part|
part.send(method_id, *arguments, &block)
part.send(method_id, *arguments, &block) unless part.nil?
end
self
end
Expand Down
2 changes: 1 addition & 1 deletion lib/bibtex/version.rb
Expand Up @@ -18,6 +18,6 @@

module BibTeX
module Version
STRING = '1.3.2'.freeze
STRING = '1.3.3'.freeze
end
end
3 changes: 3 additions & 0 deletions test/bibtex/test_bibliography.rb
Expand Up @@ -137,5 +137,8 @@ class BibliographyTest < MiniTest::Spec

end




end
end
9 changes: 8 additions & 1 deletion test/test_export.rb
Expand Up @@ -5,7 +5,13 @@

module BibTeX
class TestString < MiniTest::Unit::TestCase


# def test_yaml_roundtrip
# b1 = BibTeX.open(Test.fixtures(:bibdesk))
# b2 = Bibliography.new(YAML.load(b1.to_yaml))
# assert_equal b1, b2
# end

def test_yaml
bib = BibTeX::Bibliography.open(Test.fixtures(:bibdesk), :debug => false)
yaml = YAML.load(bib.to_yaml)
Expand All @@ -23,5 +29,6 @@ def test_json
assert_equal(%w[ dragon pickaxe rails], json.map { |y| y['key'] }.sort)
assert_equal('{The Facets of Ruby}', json[0]['series'])
end

end
end

0 comments on commit 762fa80

Please sign in to comment.