bibtex-ruby 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of bibtex-ruby might be problematic. Click here for more details.
- data.tar.gz.sig +0 -0
- data/History.txt +3 -0
- data/LICENSE +621 -0
- data/Manifest +34 -0
- data/README.rdoc +152 -0
- data/Rakefile +54 -0
- data/bibtex-ruby.gemspec +36 -0
- data/examples/bib2html.rb +28 -0
- data/examples/markdown.bib +39 -0
- data/lib/bibtex.rb +51 -0
- data/lib/bibtex/bibliography.rb +175 -0
- data/lib/bibtex/bibtex.y +129 -0
- data/lib/bibtex/elements.rb +262 -0
- data/lib/bibtex/entry.rb +154 -0
- data/lib/bibtex/error.rb +37 -0
- data/lib/bibtex/lexer.rb +328 -0
- data/lib/bibtex/parser.output +578 -0
- data/lib/bibtex/parser.rb +467 -0
- data/lib/bibtex/string_replacement.rb +43 -0
- data/lib/extensions/core.rb +35 -0
- data/test/bib/00_empty.bib +0 -0
- data/test/bib/01_no_bibtex.bib +9 -0
- data/test/bib/02_string.bib +1 -0
- data/test/bib/03_string.bib +26 -0
- data/test/bib/04_string_replacement.bib +16 -0
- data/test/bib/05_comment.bib +15 -0
- data/test/bib/06_preamble.bib +12 -0
- data/test/bib/07_entry.bib +20 -0
- data/test/bib/08_decoret.bib +83 -0
- data/test/bib/09_errors.bib +67 -0
- data/test/bib/10_bibdesk.bib +47 -0
- data/test/test_bibtex.rb +58 -0
- data/test/test_comment.rb +24 -0
- data/test/test_entry.rb +35 -0
- data/test/test_preamble.rb +24 -0
- data/test/test_string.rb +77 -0
- metadata +155 -0
- metadata.gz.sig +0 -0
@@ -0,0 +1,43 @@
|
|
1
|
+
#--
|
2
|
+
# BibTeX-Ruby
|
3
|
+
# Copyright (C) 2010 Sylvester Keil <http://sylvester.keil.or.at>
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
#++
|
18
|
+
|
19
|
+
module BibTeX
|
20
|
+
# This module contains functions to manipulate BibTeX
|
21
|
+
# string literals.
|
22
|
+
module StringReplacement
|
23
|
+
|
24
|
+
# Returns a string representation of the literal.
|
25
|
+
def self.to_s(value,options={})
|
26
|
+
return if value.nil?
|
27
|
+
options[:delimiter] ||= ['"','"']
|
28
|
+
#options[:delimiter] ||= ['{','}']
|
29
|
+
|
30
|
+
if value.empty? || (value.length == 1 && !value[0].kind_of?(Symbol))
|
31
|
+
[options[:delimiter][0],value,options[:delimiter][1]].join
|
32
|
+
else
|
33
|
+
value.map { |s| s.kind_of?(Symbol) ? s.to_s : s.inspect}.join(' # ')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Replaces all string constants in +value+ which are defined in +hsh+.
|
38
|
+
def self.replace(value,hsh)
|
39
|
+
return if value.nil?
|
40
|
+
value.map { |s| s.kind_of?(Symbol) && hsh.has_key?(s) ? hsh[s] : s }.flatten
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# BibTeX-Ruby
|
2
|
+
# Copyright (C) 2010-2011 Sylvester Keil <http://sylvester.keil.or.at>
|
3
|
+
#
|
4
|
+
# This program is free software: you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
6
|
+
# the Free Software Foundation, either version 3 of the License, or
|
7
|
+
# (at your option) any later version.
|
8
|
+
#
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
#
|
14
|
+
# You should have received a copy of the GNU General Public License
|
15
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
#++
|
17
|
+
|
18
|
+
#
|
19
|
+
# This file contains extensions to the Ruby core
|
20
|
+
#
|
21
|
+
|
22
|
+
class StringScanner
|
23
|
+
|
24
|
+
alias orig_scan_until scan_until
|
25
|
+
|
26
|
+
def pre_match_offset(offset)
|
27
|
+
self.pre_match[offset..-1]
|
28
|
+
end
|
29
|
+
|
30
|
+
# Like the original +scan_until+ but sets the start of `pre_match' to the
|
31
|
+
# current position not to zero
|
32
|
+
def scan_until(pattern)
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
@string{ foo = "bar" }
|
@@ -0,0 +1,26 @@
|
|
1
|
+
%%
|
2
|
+
%% This is a valid BibTeX file
|
3
|
+
%% String assignments Test
|
4
|
+
%%
|
5
|
+
|
6
|
+
@string{foo="bar"}
|
7
|
+
@ string{foo="bar"}
|
8
|
+
@string {foo="bar"}
|
9
|
+
@string{ foo="bar"}
|
10
|
+
@string{foo ="bar"}
|
11
|
+
@string{foo= "bar"}
|
12
|
+
@string{foo="bar" }
|
13
|
+
@ string { foo = "bar" }
|
14
|
+
@string { foo= "bar"}
|
15
|
+
@string{ foo = "bar" }
|
16
|
+
|
17
|
+
@string{foo="bar"}
|
18
|
+
@string{foo="'bar'"}
|
19
|
+
@string{foo="{"}bar{"}"}
|
20
|
+
|
21
|
+
Using some interesting symbols
|
22
|
+
@string{foo="@bar@"}
|
23
|
+
@string{foo="'bar'"}
|
24
|
+
@string{foo="{"}bar{"}"}
|
25
|
+
@string{foo="{bar}"}
|
26
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
%%
|
2
|
+
%% A valid BibTeX file
|
3
|
+
%% String assignment and replacement
|
4
|
+
%%
|
5
|
+
|
6
|
+
@string{ foo = "foo" }
|
7
|
+
@string{ bar = "bar" }
|
8
|
+
@string{ foobar = foo # "bar" }
|
9
|
+
@string{ foobarfoo = foobar # foo }
|
10
|
+
@string{ barfoobar = bar # "foo" # bar }
|
11
|
+
|
12
|
+
@preamble { "foo" # foo # foobarfoo # "bar" }
|
13
|
+
|
14
|
+
@manual {manual:1,
|
15
|
+
title = "foo" # barfoobar
|
16
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
%
|
2
|
+
% A valid BibTeX file
|
3
|
+
%
|
4
|
+
|
5
|
+
% Simple comments
|
6
|
+
|
7
|
+
@comment{ A comment can contain pretty much anything }
|
8
|
+
|
9
|
+
Here a comment is used to comment out otherwise valid
|
10
|
+
entries:
|
11
|
+
@comment{
|
12
|
+
@string{ foo = "bar" }
|
13
|
+
|
14
|
+
@string{ bar = "foo" }
|
15
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
%%
|
2
|
+
%% A valid BibTeX file
|
3
|
+
%%
|
4
|
+
|
5
|
+
%% This file contains a few entries
|
6
|
+
|
7
|
+
@book{key:0,
|
8
|
+
author = "Poe, Edgar A.",
|
9
|
+
title = {Selected \emph{Poetry} and `Tales'},
|
10
|
+
publisher = "American Library",
|
11
|
+
year = 2003
|
12
|
+
}
|
13
|
+
|
14
|
+
@article { key:1 ,
|
15
|
+
author={Hawthorne, Nathaniel},
|
16
|
+
title={Tales and Sketches},
|
17
|
+
publisher={American Library},
|
18
|
+
year={2001},}
|
19
|
+
|
20
|
+
@article{foo,}
|
@@ -0,0 +1,83 @@
|
|
1
|
+
%%
|
2
|
+
%% This BibTeX file contains all the examples of valid BibTeX objects
|
3
|
+
%% in Xavier Decoret's `A summary of BibTeX' at
|
4
|
+
%% http://artis.imag.fr/~Xavier.Decoret/resources/xdkbibtex/bibtex_summary.html
|
5
|
+
%%
|
6
|
+
|
7
|
+
@Article{py03,
|
8
|
+
author = {Xavier D\'ecoret},
|
9
|
+
title = "PyBiTex",
|
10
|
+
year = 2003
|
11
|
+
}
|
12
|
+
|
13
|
+
@Article{key03,
|
14
|
+
title = "A {bunch {of} braces {in}} title"
|
15
|
+
}
|
16
|
+
|
17
|
+
@Article{key01,
|
18
|
+
author = "Simon {"}the {saint"} Templar",
|
19
|
+
}
|
20
|
+
|
21
|
+
@Article{key01,
|
22
|
+
title = "The history of @ sign"
|
23
|
+
}
|
24
|
+
|
25
|
+
Some {{comments} with unbalanced braces
|
26
|
+
....and a "commented" entry...
|
27
|
+
|
28
|
+
Book{landru21,
|
29
|
+
author = {Landru, Henri D\'esir\'e},
|
30
|
+
title = {A hundred recipes for you wife},
|
31
|
+
publisher = {Culinary Expert Series},
|
32
|
+
year = 1921
|
33
|
+
}
|
34
|
+
|
35
|
+
..some other comments..before a valid entry...
|
36
|
+
|
37
|
+
@Book{steward03,
|
38
|
+
author = { Martha Steward },
|
39
|
+
title = {Cooking behind bars},
|
40
|
+
publisher = {Culinary Expert Series},
|
41
|
+
year = 2003
|
42
|
+
}
|
43
|
+
|
44
|
+
...and finally an entry commented by the use of the special Comment entry type.
|
45
|
+
|
46
|
+
@Comment{steward03,
|
47
|
+
author = {Martha Steward},
|
48
|
+
title = {Cooking behind bars},
|
49
|
+
publisher = {Culinary Expert Series},
|
50
|
+
year = 2003
|
51
|
+
}
|
52
|
+
[Note: here BibTeX-Ruby differs from bibtex in that it does not parse
|
53
|
+
the nested book as an object, but treats it as a comment!]
|
54
|
+
@Comment{
|
55
|
+
@Book{steward03,
|
56
|
+
author = {Martha Steward},
|
57
|
+
title = {Cooking behind bars},
|
58
|
+
publisher = {Culinary Expert Series},
|
59
|
+
year = 2003
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
@String{mar = "march"}
|
64
|
+
|
65
|
+
@Book{sweig42,
|
66
|
+
Author = { Stefan Sweig },
|
67
|
+
title = { The impossible book },
|
68
|
+
publisher = { Dead Poet Society},
|
69
|
+
year = 1942,
|
70
|
+
month = "1~" # mar
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
@String {firstname = "Xavier"}
|
75
|
+
@String {lastname = "Decoret"}
|
76
|
+
@String {email = firstname # "." # lastname # "@imag.fr"}
|
77
|
+
|
78
|
+
@preamble {"This bibliography was generated on \today"}
|
79
|
+
|
80
|
+
@String {maintainer = "Xavier D\'ecoret"}
|
81
|
+
|
82
|
+
@preamble { "Maintained by " # maintainer }
|
83
|
+
|
@@ -0,0 +1,67 @@
|
|
1
|
+
%%
|
2
|
+
%% A BibTeX file with various errors.
|
3
|
+
%%
|
4
|
+
|
5
|
+
In strict mode we cannot have the AT symbol anywhere except inside literals
|
6
|
+
or comment objects.
|
7
|
+
|
8
|
+
Unbalanced braces in string literal:
|
9
|
+
@string{ foo = "foo }
|
10
|
+
|
11
|
+
Another unbalanced braces in string literal:
|
12
|
+
@string{ foo = "foo{"} }
|
13
|
+
|
14
|
+
Another unterminated string literal:
|
15
|
+
@string{ foo = "foo{" }
|
16
|
+
|
17
|
+
A valid objects after two invalid ones:
|
18
|
+
@string { foo="foo"}
|
19
|
+
|
20
|
+
Unterminated literal:
|
21
|
+
@preamble { "{" # foo # "bar" }
|
22
|
+
|
23
|
+
Valid object:
|
24
|
+
@preamble {
|
25
|
+
"preamble"
|
26
|
+
|
27
|
+
}
|
28
|
+
|
29
|
+
Bad Syntax:
|
30
|
+
@book{
|
31
|
+
author = "An Author"
|
32
|
+
}
|
33
|
+
|
34
|
+
@book{key
|
35
|
+
author = "An Author"
|
36
|
+
}
|
37
|
+
|
38
|
+
@book{key,
|
39
|
+
author = "An Author"
|
40
|
+
title = "The Title"
|
41
|
+
}
|
42
|
+
|
43
|
+
Unterminated string:
|
44
|
+
@book{key,
|
45
|
+
author = "An Author,
|
46
|
+
title = "The Title"
|
47
|
+
}
|
48
|
+
|
49
|
+
Better syntax:
|
50
|
+
@book{ key, author ="An Author", title = "The Title"}
|
51
|
+
|
52
|
+
Unterminated object:
|
53
|
+
@preamble{ "hello"
|
54
|
+
|
55
|
+
@string
|
56
|
+
{ bar =
|
57
|
+
"bar"
|
58
|
+
|
59
|
+
Better:
|
60
|
+
@string
|
61
|
+
|
62
|
+
{
|
63
|
+
bar
|
64
|
+
=
|
65
|
+
"bar"
|
66
|
+
|
67
|
+
}
|
@@ -0,0 +1,47 @@
|
|
1
|
+
%% This BibTeX bibliography file was created using BibDesk.
|
2
|
+
%% http://bibdesk.sourceforge.net/
|
3
|
+
|
4
|
+
|
5
|
+
%% Created for Sylvester Keil at 2010-08-05 10:07:07 +0200
|
6
|
+
|
7
|
+
|
8
|
+
%% Saved with string encoding Unicode (UTF-8)
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
@book{rails,
|
13
|
+
Address = {Raleigh, North Carolina},
|
14
|
+
Author = {Ruby, Sam, and Thomas, Dave, and Hansson, David Heinemeier},
|
15
|
+
Booktitle = {Agile Web Development with Rails},
|
16
|
+
Date-Added = {2010-08-05 10:01:36 +0200},
|
17
|
+
Date-Modified = {2010-08-05 10:06:46 +0200},
|
18
|
+
Edition = {third},
|
19
|
+
Isbn = {978-1-9343561-6-6},
|
20
|
+
Keywords = {ruby, rails},
|
21
|
+
Publisher = {The Pragmatic Bookshelf},
|
22
|
+
Series = {The Facets of Ruby},
|
23
|
+
Title = {Agile Web Development with Rails},
|
24
|
+
Year = {2009}}
|
25
|
+
|
26
|
+
@book{dragon,
|
27
|
+
Address = {Boston},
|
28
|
+
Author = {Aho, Alfred V., and Lam, Monica S., and Ullman, Jeffrey D.},
|
29
|
+
Booktitle = {Compilers: Principles, Techniques, and Tools},
|
30
|
+
Date-Added = {2010-08-05 09:57:15 +0200},
|
31
|
+
Date-Modified = {2010-08-05 10:06:32 +0200},
|
32
|
+
Edition = {second},
|
33
|
+
Keywords = {compiler, lex, yacc},
|
34
|
+
Publisher = {Addison Wesley},
|
35
|
+
Title = {Compilers: Principles, Techniques, and Tools},
|
36
|
+
Year = {2007}}
|
37
|
+
|
38
|
+
@book{pickaxe,
|
39
|
+
Address = {Raleigh, North Carolina},
|
40
|
+
Author = {Thomas, Dave, and Fowler, Chad, and Hunt, Andy},
|
41
|
+
Date-Added = {2010-08-05 09:54:07 +0200},
|
42
|
+
Date-Modified = {2010-08-05 10:07:01 +0200},
|
43
|
+
Keywords = {ruby},
|
44
|
+
Publisher = {The Pragmatic Bookshelf},
|
45
|
+
Series = {The Facets of Ruby},
|
46
|
+
Title = {Programming Ruby 1.9: The Pragmatic Programmer's Guide},
|
47
|
+
Year = {2009}}
|
data/test/test_bibtex.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bibtex'
|
3
|
+
require 'minitest/unit'
|
4
|
+
require 'minitest/autorun'
|
5
|
+
|
6
|
+
class TestBibtex < MiniTest::Unit::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
end
|
10
|
+
|
11
|
+
def teardown
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_empty
|
15
|
+
bib = BibTeX::Bibliography.open('test/bib/00_empty.bib', :debug => true)
|
16
|
+
refute_nil(bib)
|
17
|
+
assert_equal(BibTeX::Bibliography, bib.class)
|
18
|
+
assert(bib.empty?)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_no_bibtex
|
22
|
+
bib = BibTeX::Bibliography.open('test/bib/01_no_bibtex.bib', :debug => true)
|
23
|
+
refute_nil(bib)
|
24
|
+
assert_equal(BibTeX::Bibliography, bib.class)
|
25
|
+
assert(bib.empty?)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_decoret
|
29
|
+
bib = BibTeX::Bibliography.open('test/bib/08_decoret.bib', :debug => true)
|
30
|
+
refute_nil(bib)
|
31
|
+
assert_equal(BibTeX::Bibliography, bib.class)
|
32
|
+
assert_equal(15, bib.length)
|
33
|
+
assert_equal([BibTeX::Entry,BibTeX::Comment,BibTeX::String,BibTeX::Preamble], bib.data.map(&:class).uniq)
|
34
|
+
assert_equal('py03', bib.data[0].key)
|
35
|
+
assert_equal(:article, bib['py03'].type)
|
36
|
+
assert_equal(['Xavier D\\\'ecoret'], bib['py03'][:author])
|
37
|
+
assert_equal(['PyBiTex'], bib['py03'][:title])
|
38
|
+
assert_equal(['2003'], bib['py03'][:year])
|
39
|
+
assert_equal(:article, bib['key03'].type)
|
40
|
+
assert_equal(['A {bunch {of} braces {in}} title'], bib['key03'][:title])
|
41
|
+
#TODO missing assertions
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_errors
|
45
|
+
bib = BibTeX::Bibliography.open('test/bib/09_errors.bib', :debug => true)
|
46
|
+
#refute_nil(bib)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_bibdesk
|
50
|
+
bib = BibTeX::Bibliography.open('test/bib/10_bibdesk.bib', :debug => true)
|
51
|
+
refute_nil(bib)
|
52
|
+
assert_equal(BibTeX::Bibliography, bib.class)
|
53
|
+
assert_equal(3, bib.length)
|
54
|
+
assert_equal('rails', bib.data[0].key)
|
55
|
+
assert_equal(['2010-08-05 10:06:32 +0200'], bib[:dragon]['date-modified'])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|