bibtex-ruby 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bibtex-ruby might be problematic. Click here for more details.

Files changed (67) hide show
  1. data/Gemfile +6 -1
  2. data/Gemfile.lock +48 -5
  3. data/History.txt +16 -1
  4. data/Manifest +43 -19
  5. data/README.md +178 -167
  6. data/Rakefile +26 -5
  7. data/auto.watchr +6 -0
  8. data/bibtex-ruby.gemspec +8 -5
  9. data/examples/bib2html.rb +28 -18
  10. data/features/bibtex.feature +96 -0
  11. data/features/entries.feature +67 -0
  12. data/features/issues/slash_keys.feature +21 -0
  13. data/features/names.feature +72 -0
  14. data/features/preambles.feature +27 -0
  15. data/features/query.feature +56 -0
  16. data/features/replacement.feature +68 -0
  17. data/features/step_definitions/bibtex_steps.rb +74 -0
  18. data/features/step_definitions/name_steps.rb +13 -0
  19. data/features/strings.feature +52 -0
  20. data/features/support/env.rb +7 -0
  21. data/lib/bibtex.rb +5 -1
  22. data/lib/bibtex/bibliography.rb +218 -95
  23. data/lib/bibtex/bibtex.y +18 -15
  24. data/lib/bibtex/elements.rb +130 -136
  25. data/lib/bibtex/entry.rb +133 -69
  26. data/lib/bibtex/extensions.rb +0 -35
  27. data/lib/bibtex/lexer.rb +9 -9
  28. data/lib/bibtex/name_parser.output +464 -0
  29. data/lib/bibtex/name_parser.rb +490 -0
  30. data/lib/bibtex/names.rb +162 -0
  31. data/lib/bibtex/names.y +196 -0
  32. data/lib/bibtex/parser.output +5 -5
  33. data/lib/bibtex/parser.rb +19 -16
  34. data/lib/bibtex/replaceable.rb +52 -0
  35. data/lib/bibtex/utilities.rb +23 -5
  36. data/lib/bibtex/value.rb +201 -0
  37. data/lib/bibtex/version.rb +1 -1
  38. data/test/benchmark.rb +52 -0
  39. data/test/bibtex/test_bibliography.rb +141 -0
  40. data/test/bibtex/test_elements.rb +40 -0
  41. data/test/bibtex/test_entry.rb +99 -0
  42. data/test/bibtex/test_names.rb +23 -0
  43. data/test/bibtex/test_parser.rb +79 -0
  44. data/test/bibtex/test_string.rb +83 -0
  45. data/test/bibtex/test_utilities.rb +34 -0
  46. data/test/bibtex/test_value.rb +70 -0
  47. data/test/{bib/10_bibdesk.bib → fixtures/bibdesk.bib} +1 -1
  48. data/test/{bib/05_comment.bib → fixtures/comment.bib} +0 -0
  49. data/test/{bib/08_decoret.bib → fixtures/decoret.bib} +0 -0
  50. data/test/{bib/00_empty.bib → fixtures/empty.bib} +0 -0
  51. data/test/{bib/07_entry.bib → fixtures/entry.bib} +0 -0
  52. data/test/{bib/09_errors.bib → fixtures/errors.bib} +0 -0
  53. data/test/{bib/01_no_bibtex.bib → fixtures/no_bibtex.bib} +0 -0
  54. data/test/{bib/06_preamble.bib → fixtures/preamble.bib} +1 -1
  55. data/test/{bib/11_roundtrip.bib → fixtures/roundtrip.bib} +1 -1
  56. data/test/helper.rb +17 -2
  57. data/test/test_bibtex.rb +87 -93
  58. data/test/test_export.rb +18 -22
  59. metadata +85 -30
  60. data/test/bib/02_string.bib +0 -1
  61. data/test/bib/03_string.bib +0 -25
  62. data/test/bib/04_string_replacement.bib +0 -16
  63. data/test/test_comment.rb +0 -21
  64. data/test/test_entry.rb +0 -98
  65. data/test/test_preamble.rb +0 -39
  66. data/test/test_string.rb +0 -97
  67. data/test/test_utilities.rb +0 -36
data/Gemfile CHANGED
@@ -1,2 +1,7 @@
1
1
  source :rubygems
2
- gemspec
2
+ gemspec
3
+
4
+ group :development do
5
+ gem 'ruby-debug19', :platforms => [:ruby_19]
6
+ gem 'ruby-debug', :platforms => [:ruby_18]
7
+ end
@@ -1,20 +1,63 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bibtex-ruby (1.1.2)
4
+ bibtex-ruby (1.3.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
8
8
  specs:
9
+ archive-tar-minitar (0.5.2)
10
+ autowatchr (0.1.5)
11
+ watchr
12
+ builder (3.0.0)
13
+ columnize (0.3.2)
14
+ cucumber (0.10.2)
15
+ builder (>= 2.1.2)
16
+ diff-lcs (>= 1.1.2)
17
+ gherkin (>= 2.3.5)
18
+ json (>= 1.4.6)
19
+ term-ansicolor (>= 1.0.5)
20
+ diff-lcs (1.1.2)
21
+ gherkin (2.3.5)
22
+ json (>= 1.4.6)
9
23
  json (1.5.1)
10
- minitest (2.0.2)
24
+ linecache (0.43)
25
+ linecache19 (0.5.12)
26
+ ruby_core_source (>= 0.1.4)
27
+ mini_shoulda (0.3.0)
28
+ minitest (~> 2.1.0)
29
+ minitest (2.1.0)
30
+ mynyml-redgreen (0.7.1)
31
+ term-ansicolor (>= 1.0.4)
11
32
  racc (1.4.6)
33
+ ruby-debug (0.10.4)
34
+ columnize (>= 0.1)
35
+ ruby-debug-base (~> 0.10.4.0)
36
+ ruby-debug-base (0.10.4)
37
+ linecache (>= 0.3)
38
+ ruby-debug-base19 (0.11.25)
39
+ columnize (>= 0.3.1)
40
+ linecache19 (>= 0.5.11)
41
+ ruby_core_source (>= 0.1.4)
42
+ ruby-debug19 (0.11.6)
43
+ columnize (>= 0.3.1)
44
+ linecache19 (>= 0.5.11)
45
+ ruby-debug-base19 (>= 0.11.19)
46
+ ruby_core_source (0.1.5)
47
+ archive-tar-minitar (>= 0.5.2)
48
+ term-ansicolor (1.0.5)
49
+ watchr (0.7)
12
50
 
13
51
  PLATFORMS
14
52
  ruby
15
53
 
16
54
  DEPENDENCIES
55
+ autowatchr (>= 0.1)
17
56
  bibtex-ruby!
18
- json (>= 1.5.0)
19
- minitest (>= 2.0.2)
20
- racc (>= 1.4.6)
57
+ cucumber (>= 0.10)
58
+ json (>= 1.5)
59
+ mini_shoulda (>= 0.3)
60
+ mynyml-redgreen (>= 0.7)
61
+ racc (>= 1.4)
62
+ ruby-debug
63
+ ruby-debug19
@@ -1,7 +1,22 @@
1
+ === 1.3.1
2
+
3
+ * Added CiteProc/CSL export format
4
+
5
+ === 1.3.0 / 2011-05-12
6
+
7
+ * Improved top-level utility functions
8
+ * Implemented a BibTeX name parser and name value objects
9
+ * Re-factored and cleaned up API and export
10
+ * Added cucumber features
11
+ * Re-implemented string handling using dedicated Value objects
12
+ * Implemented query language to search in bibliographies
13
+ * Improved testing environment
14
+ * Added cucumber features
15
+
1
16
  === 1.2.1 / 2011-02-26
2
17
 
3
18
  * Fixed several MacRuby compliancy issues.
4
- * Fixed compatibility issues and BibTeX parsing of Name tokens (by lyro)
19
+ * Fixed compatibility issues and BibTeX parsing of name tokens (lyro)
5
20
 
6
21
  === 1.2.0 / 2011-02-12
7
22
 
data/Manifest CHANGED
@@ -5,11 +5,27 @@ LICENSE
5
5
  Manifest
6
6
  README.md
7
7
  Rakefile
8
+ auto.watchr
8
9
  bibtex-ruby.gemspec
9
10
  examples
10
11
  examples/bib2html.rb
11
12
  examples/bib2yaml.rb
12
13
  examples/markdown.bib
14
+ features
15
+ features/bibtex.feature
16
+ features/entries.feature
17
+ features/issues
18
+ features/issues/slash_keys.feature
19
+ features/names.feature
20
+ features/preambles.feature
21
+ features/query.feature
22
+ features/replacement.feature
23
+ features/step_definitions
24
+ features/step_definitions/bibtex_steps.rb
25
+ features/step_definitions/name_steps.rb
26
+ features/strings.feature
27
+ features/support
28
+ features/support/env.rb
13
29
  lib
14
30
  lib/bibtex
15
31
  lib/bibtex/bibliography.rb
@@ -19,30 +35,38 @@ lib/bibtex/entry.rb
19
35
  lib/bibtex/error.rb
20
36
  lib/bibtex/extensions.rb
21
37
  lib/bibtex/lexer.rb
38
+ lib/bibtex/name_parser.output
39
+ lib/bibtex/name_parser.rb
40
+ lib/bibtex/names.rb
41
+ lib/bibtex/names.y
22
42
  lib/bibtex/parser.output
23
43
  lib/bibtex/parser.rb
44
+ lib/bibtex/replaceable.rb
24
45
  lib/bibtex/utilities.rb
46
+ lib/bibtex/value.rb
25
47
  lib/bibtex/version.rb
26
48
  lib/bibtex.rb
27
49
  test
28
- test/bib
29
- test/bib/00_empty.bib
30
- test/bib/01_no_bibtex.bib
31
- test/bib/02_string.bib
32
- test/bib/03_string.bib
33
- test/bib/04_string_replacement.bib
34
- test/bib/05_comment.bib
35
- test/bib/06_preamble.bib
36
- test/bib/07_entry.bib
37
- test/bib/08_decoret.bib
38
- test/bib/09_errors.bib
39
- test/bib/10_bibdesk.bib
40
- test/bib/11_roundtrip.bib
50
+ test/benchmark.rb
51
+ test/bibtex
52
+ test/bibtex/test_bibliography.rb
53
+ test/bibtex/test_elements.rb
54
+ test/bibtex/test_entry.rb
55
+ test/bibtex/test_names.rb
56
+ test/bibtex/test_parser.rb
57
+ test/bibtex/test_string.rb
58
+ test/bibtex/test_utilities.rb
59
+ test/bibtex/test_value.rb
60
+ test/fixtures
61
+ test/fixtures/bibdesk.bib
62
+ test/fixtures/comment.bib
63
+ test/fixtures/decoret.bib
64
+ test/fixtures/empty.bib
65
+ test/fixtures/entry.bib
66
+ test/fixtures/errors.bib
67
+ test/fixtures/no_bibtex.bib
68
+ test/fixtures/preamble.bib
69
+ test/fixtures/roundtrip.bib
41
70
  test/helper.rb
42
71
  test/test_bibtex.rb
43
- test/test_comment.rb
44
- test/test_entry.rb
45
- test/test_export.rb
46
- test/test_preamble.rb
47
- test/test_string.rb
48
- test/test_utilities.rb
72
+ test/test_export.rb
data/README.md CHANGED
@@ -1,12 +1,13 @@
1
1
  BibTeX-Ruby
2
2
  ===========
3
3
 
4
- The BibTeX-Ruby package contains a parser for BibTeX bibliography files and a
5
- class structure to manage or convert BibTeX objects in Ruby. It is designed to
6
- support all BibTeX objects (including @comment, string-replacements via @string,
7
- as well as string concatenation using '#') and handles all content outside of
8
- BibTeX objects as 'meta comments' which may or may not be included in
9
- post-processing.
4
+ BibTeX-Ruby is a fairly complete library and parser for BibTeX bibliography
5
+ files; it offers an interface to manage, search, or convert BibTeX objects in
6
+ Ruby. It is designed to support all BibTeX objects (including @comment,
7
+ string-replacements via @string, as well as string concatenation using '#')
8
+ and handles all content outside of BibTeX objects as 'meta content' which may
9
+ or may not be included in post-processing. BibTeX-Ruby also includes a name
10
+ parser to support comfortable access to the individual tokens of name values.
10
11
 
11
12
 
12
13
  Quickstart
@@ -14,26 +15,25 @@ Quickstart
14
15
 
15
16
  $ [sudo] gem install bibtex-ruby
16
17
  $ irb
17
- > require 'bibtex'
18
- => true
19
- > bib = BibTeX.open('./ruby.bib')
20
- => book{pickaxe,
21
- address = {Raleigh, North Carolina},
22
- author = {Thomas, Dave, and Fowler, Chad, and Hunt, Andy},
23
- date-added = {2010-08-05 09:54:07 0200},
24
- date-modified = {2010-08-05 10:07:01 0200},
25
- keywords = {ruby},
26
- publisher = {The Pragmatic Bookshelf},
27
- series = {The Facets of Ruby},
28
- title = {Programming Ruby 1.9: The Pragmatic Programmers Guide},
29
- year = {2009}
30
- }
31
- > bib[:pickaxe].year
32
- => "2009"
33
- > bib[:pickaxe][:title]
34
- => "Programming Ruby 1.9: The Pragmatic Programmer's Guide"
35
- > bib[:pickaxe].author = 'Thomas, D., Fowler, C., and Hunt, A.'
36
- => "Thomas, D., and Fowler, C., and Hunt, A."
18
+ >> require 'bibtex'
19
+ => true
20
+ >> b = BibTeX.open('./ruby.bib')
21
+ >> b[:pickaxe]
22
+ => "2009"
23
+ >> b[:pickaxe].title
24
+ => "Programming Ruby 1.9: The Pragmatic Programmer's Guide"
25
+ >> b[:pickaxe].author.length
26
+ => 3
27
+ >> b[:pickaxe].author.to_s
28
+ => "Thomas, Dave and Fowler, Chad and Hunt, Andy"
29
+ >> b[:pickaxe].author[2].first
30
+ => "Andy"
31
+ >> b['@book'].length
32
+ => 3
33
+ >> b['@article'].length
34
+ => 0
35
+ >> bib['@book[year=2009]'].length
36
+ => 1
37
37
 
38
38
 
39
39
  Installation
@@ -50,24 +50,24 @@ If you want to work with the sources:
50
50
  $ [sudo] bundle install
51
51
  $ rake racc
52
52
  $ rake rdoc
53
+ $ rake features
53
54
  $ rake test
54
55
 
55
- Or, alternatively, fork the [project on GitHub](http://github.com/inukshuk/bibtex-ruby.git).
56
+ For extra credit, fork the
57
+ [project on GitHub](http://github.com/inukshuk/bibtex-ruby.git).
56
58
 
57
59
 
58
60
  Requirements
59
61
  ------------
60
62
 
61
63
  * The parser generator [racc](http://i.loveruby.net/en/projects/racc/) is
62
- required to generate parser.
63
- * The *minitest* gem is required to run the tests in older Ruby versions.
64
- * The *json* gem is required on older Ruby versions for JSON export.
64
+ required to generate the BibTeX parser and the name parser.
65
+ * The **json** gem is required on older Ruby versions for JSON export.
65
66
 
66
67
  The bibtex-ruby gem has been tested on Ruby versions 1.8.7 and 1.9.2; it has
67
- been confirmed to work with REE 1.8.7 x86_64 and JRuby 1.5.6 x86_64-java. It
68
- does not work with MacRuby 0.8 because of a bug in MacRuby's implementation
69
- of the *StringScanner* class, however, this has been fixed in SVN (see
70
- [#1](https://github.com/inukshuk/bibtex-ruby/issues/closed#issue/1) for details).
68
+ been confirmed to work with REE 1.8.7 x86_64 and JRuby 1.5.6 x86_64-java;
69
+ however, there have been some [issues](https://github.com/inukshuk/bibtex-ruby/issues)
70
+ with MacRuby implementations.
71
71
 
72
72
 
73
73
 
@@ -78,18 +78,15 @@ It is very easy to use BibTeX-Ruby. You can use the top level utility methods
78
78
  **BibTeX.open** and **BibTeX.parse** to open a '.bib' file or to parse a string
79
79
  containing BibTeX contents. Normally, BibTeX-Ruby will discard all text outside
80
80
  of regular BibTeX elements; however, if you wish to include everything, simply add
81
- `:include => [:meta_comments]` to your invocation of **BibTeX.open** or **BibTeX.parse**.
81
+ `:include => [:meta_content]` to your invocation of **BibTeX.open** or **BibTeX.parse**.
82
82
 
83
83
  Once BibTeX-Ruby has parsed your '.bib' file, you can easily access individual entries.
84
84
  For example, if you set up your bibliography as follows:
85
85
 
86
- bib = BibTeX.parse <<-END
86
+ b = BibTeX.parse <<-END
87
87
  @book{pickaxe,
88
88
  address = {Raleigh, North Carolina},
89
- author = {Thomas, Dave, and Fowler, Chad, and Hunt, Andy},
90
- date-added = {2010-08-05 09:54:07 +0200},
91
- date-modified = {2010-08-05 10:07:01 +0200},
92
- keywords = {ruby},
89
+ author = {Thomas, Dave and Fowler, Chad and Hunt, Andy},
93
90
  publisher = {The Pragmatic Bookshelf},
94
91
  series = {The Facets of Ruby},
95
92
  title = {Programming Ruby 1.9: The Pragmatic Programmer's Guide},
@@ -97,20 +94,20 @@ For example, if you set up your bibliography as follows:
97
94
  }
98
95
  END
99
96
 
100
- You could easily access it, using the entry's key, 'pickaxe', like so: `bib[:pickaxe]`;
101
- you also have easy access to individual fields, for example: `bib[:pickaxe][:author]`.
97
+ You could easily access it, using the entry's key, 'pickaxe', like so: `b[:pickaxe]`;
98
+ you also have easy access to individual fields, for example: `b[:pickaxe][:author]`.
102
99
  Alternatively, BibTeX-Ruby accepts ghost methods to conveniently access an entry's fields,
103
100
  similar to **ActiveRecord::Base**. Therefore, it is equally possible to access the
104
- 'author' field above as `bib[:pickaxe].author`.
101
+ 'author' field above as `b[:pickaxe].author`.
105
102
 
106
- Instead of parsing strings you can also create BibTeX elements by using Ruby:
103
+ Instead of parsing strings you can also create BibTeX elements directly in Ruby:
107
104
 
108
105
  > bib = BibTeX::Bibliography.new
109
106
  > bib << BibTeX::Entry.new({
110
107
  > :type => :book,
111
- > :key => 'rails',
108
+ > :key => :rails,
112
109
  > :address => 'Raleigh, North Carolina',
113
- > :author => 'Ruby, Sam, and Thomas, Dave, and Hansson, David Heinemeier',
110
+ > :author => 'Ruby, Sam and Thomas, Dave, and Hansson, David Heinemeier',
114
111
  > :booktitle => 'Agile Web Development with Rails',
115
112
  > :edition => 'third',
116
113
  > :keywords => 'ruby, rails',
@@ -121,30 +118,78 @@ Instead of parsing strings you can also create BibTeX elements by using Ruby:
121
118
  > })
122
119
  > book = BibTeX::Entry.new
123
120
  > book.type = :book
124
- > book.key = 'mybook'
121
+ > book.key = :mybook
125
122
  > bib << book
126
123
 
127
124
 
125
+ ### Queries
126
+
127
+ Since version 1.3 BibTeX-Ruby implements a simple query language to search
128
+ Bibliographies via the `Bibliography#query` (or `Bibliography#q`) methods.
129
+ Additionally, you can access individual elements or groups of elements via
130
+ their index using `Bibliography#[]`; this accessor also exposes some of the
131
+ query functionality with the exception of yielding to a block. For instance:
132
+
133
+ >> bib[-1]
134
+ => Returns the last element of the Bibliography or nil
135
+ >> bib[1,2]
136
+ => Returns the second and third elements or nil
137
+ >> bib[1..2]
138
+ >> Same as above
139
+ >> bib[:key]
140
+ => Returns the first entry with key 'key' or nil
141
+ >> bib['key']
142
+ => Returns all entries with key 'key' or []
143
+ >> bib['@article']
144
+ => Returns all entries of type 'article' or []
145
+ >> bib['@preamble']
146
+ => Returns all preamble objects (this is the same as Bibliography#preambles) or []
147
+ >> bib[/ruby/]
148
+ => Returns all objects that match 'ruby' anywhere or []
149
+ >> bib['@book[keywords=ruby]']
150
+ => Returns all books whose keywords attribute equals 'ruby' or []
151
+ >> bib.query('@book') { |e| e.keywords.split(/,/).length > 1 }
152
+ => Returns all book entries with two or more keywords or []
153
+
154
+ Queries offer syntactic sugar for common enumerator invocations:
155
+
156
+ >> bib.query(:all, '@book')
157
+ => same as bib.select { |b| b.has_type?(:book) }
158
+ >> bib.query('@book')
159
+ => same as above
160
+ >> bib.query(:first, '@book')
161
+ => same as bib.detect { |b| b.has_type?(:book) }
162
+ >> bib.query(:none, '@book')
163
+ => same as bib.reject { |b| b.has_type?(:book) }
164
+
165
+ You can also use queries to delete entries in your bibliography:
166
+
167
+ >> bib.delete(/ruby/)
168
+ => deletes all object that match 'ruby' in their string representation
169
+ >> bib.delete('@comment')
170
+ => strips all BibTeX comments from the bibliography
171
+
128
172
 
129
173
  ### String Replacement
130
174
 
131
175
  If your bibliography contains BibTeX @string objects, you can let BibTeX-Ruby
132
176
  replace the strings for you. You have access to a bibliography's strings via
133
- **BibTeX::Bibliography#strings** and you can replace the strings of an entry using
134
- the **BibTeX::Entry#replace!** method. Thus, to replace all strings defined in your
135
- bibliography object **bib** your could use this code:
177
+ **BibTeX::Bibliography#strings** or by using a '@string' query.
178
+ You can replace the string symbols of an object by calling the object's
179
+ the **replace** method. Thus, to replace all strings defined in bibliography
180
+ b you could use the following code:
136
181
 
137
- bib.entries.each do |entry|
138
- entry.replace!(bib.strings)
182
+ b.each do |obj|
183
+ obj.replace(b.q('@string'))
139
184
  end
140
185
 
141
186
  A shorthand version for replacing all strings in a given bibliography is the
142
- `Bibliography#replace_strings` method. Similarly, you can use the
143
- `Bibliography#join_strings` method to join individual strings together. For instance:
187
+ `Bibliography#replace` method. Similarly, you can use the
188
+ `Bibliography#join` method to join individual strings together. For instance:
144
189
 
145
190
  > bib = BibTeX::Bibliography.new
146
191
  > bib.add BibTeX::Element.parse '@string{ foo = "foo" }'
147
- > bib.add BibTeX::Element.parse '@string{ bar = "bar" }'
192
+ > bib << BibTeX::Element.parse '@string{ bar = "bar" }'
148
193
  > bib.add BibTeX::Element.parse <<-END
149
194
  > @book{abook,
150
195
  > author = foo # "Author",
@@ -156,24 +201,50 @@ A shorthand version for replacing all strings in a given bibliography is the
156
201
  author = foo # "Author",
157
202
  title = foo # bar
158
203
  }
159
- > bib.replace_strings
204
+ > bib.replace
160
205
  > puts bib[:abook].to_s
161
206
  @book{abook,
162
207
  author = "foo" # "Author",
163
208
  title = "foo" # "bar"
164
209
  }
165
- > bib.join_strings
210
+ > bib.join
166
211
  @book{abook,
167
212
  author = {fooAuthor},
168
213
  title = {foobar}
169
214
  }
170
215
 
216
+ ### Names
217
+
218
+ Since version 1.3, BibTeX-Ruby features a name parser. You can use the top-level
219
+ `BibTeX.names` utility to quickly parse individual name values. Alternatively,
220
+ you can call `Bibliography.parse_names` to convert all name fields contained
221
+ in the bibliography. When parsing BibTeX files, BibTeX-Ruby will automatically
222
+ convert names; if you do not want the names to be parsed you can set the
223
+ `:parse_names` parser option to false.
224
+
225
+ Note that the string replacement and concatenation features described above
226
+ are not supported for name objects; therefore, BibTeX-Ruby tries to replace
227
+ and join all values before name conversion; name fields containing string
228
+ symbols that cannot be replaced will not be parsed.
229
+
230
+ In the following example, string replacement can take place, thus all names
231
+ are parsed and can easily be mapped to their last names:
232
+
233
+ >> BibTeX.parse(<<-END)[1].author.map(&:last)
234
+ @string{ ht = "Nathaniel Hawthorne" }
235
+ @book{key,
236
+ author = ht # " and Melville, Herman"
237
+ }
238
+ END
239
+ => ["Hawthorne", "Melville"]
240
+
241
+
171
242
  ### Conversions
172
243
 
173
244
  Furthermore, BibTeX-Ruby allows you to export your bibliography for processing
174
245
  by other tools. Currently supported formats include YAML, JSON, and XML.
175
246
  Of course, you can also export your bibliography back to BibTeX; if you include
176
- `:meta_comments', your export should be identical to the original '.bib' file,
247
+ `:meta_content', your export should be identical to the original '.bib' file,
177
248
  except for whitespace, blank lines and letter case (BibTeX-Ruby will downcase
178
249
  all keys).
179
250
 
@@ -181,126 +252,66 @@ In order to export your bibliography use **#to\_s**, **#to\_yaml**, **#to\_json*
181
252
  **#to\_xml**, respectively. For example, the following line constitutes a simple
182
253
  BibTeX to YAML converter:
183
254
 
184
- BibTeX.open('example.bib').to_yaml
255
+ >> BibTeX.open('example.bib').to_yaml
185
256
 
186
257
  Look at the 'examples' directory for more elaborate examples of a BibTeX to YAML
187
258
  and a BibTeX to HTML converter.
188
259
 
189
-
260
+ BibTeX-Ruby offers an API which lets you manipulate BibTeX objects (string
261
+ replacement, name parsing etc.); however, sometimes you just want quick access
262
+ to your bibliography's contents. In these cases the **to_hash** method is
263
+ useful (use **to_a** if you are only interested in the bibliography's contents):
264
+ it converts all objects into simple Ruby hashes made up of symbols
265
+ and strings. Furthermore, often you would like to control what sort of quotes
266
+ are used in an export;
267
+ therefore, all conversion methods accept an options hash which lets you define
268
+ what quotes to use (note that BibTeX-Ruby will always use regular double
269
+ quotes if a value consists of more than one token, because these tokens will
270
+ be concatenated using BibTeX's '#' operator).
271
+
272
+ >> BibTeX.parse(<<-END).to_a # implies: :quotes => ['{','}']
273
+ @book{pickaxe,
274
+ Address = {Raleigh, North Carolina},
275
+ Author = {Thomas, Dave, and Fowler, Chad, and Hunt, Andy},
276
+ Publisher = {The Pragmatic Bookshelf},
277
+ Title = {Programming Ruby 1.9: The Pragmatic Programmer's Guide},
278
+ Year = {2009}
279
+ }
280
+ END
281
+ => [{:key=>:pickaxe, :type=>:book,
282
+ :address=>"{Raleigh, North Carolina}",
283
+ :author=>"{Thomas, Dave, and Fowler, Chad, and Hunt, Andy}",
284
+ :publisher=>"{The Pragmatic Bookshelf}",
285
+ :title=>"{Programming Ruby 1.9: The Pragmatic Programmer's Guide}",
286
+ :year=>"{2009}"}]
287
+
288
+ For post-processing in Ruby most of the time you do not need any explicit
289
+ quotes; therefore you can simply add the :quotes option with an empty string:
290
+
291
+ >> BibTeX.parse(<<-END).to_a(:quotes => '')
292
+ ...
293
+ END
294
+ => [{:key=>:pickaxe, :type=>:book,
295
+ :address=>"Raleigh, North Carolina",
296
+ :author=>"Thomas, Dave, and Fowler, Chad, and Hunt, Andy",
297
+ :publisher=>"The Pragmatic Bookshelf",
298
+ :title=>"Programming Ruby 1.9: The Pragmatic Programmer's Guide",
299
+ :year=>"2009"}]
190
300
 
191
301
  The Parser
192
302
  ----------
193
303
 
194
- The BibTeX-Ruby parser is generated using the wonderful
304
+ The BibTeX-Ruby parser is generated using the awesome
195
305
  [racc](http://i.loveruby.net/en/projects/racc/) parser generator. You can take
196
- look at the grammar definition in the file `lib/bibtex/bibtex.y`.
197
-
198
-
199
- ### The BibTeX Format
200
-
201
- At first glance, the BibTeX file format seems very clear and simple;
202
- however, there are a number of peculiarities which warrant some
203
- explanation. The best place to start reading is probably at [your closest
204
- ctan server](http://www.ctan.org/get/biblio/bibtex/) where
205
- the original `bibtex` from 1988 still lives. Additionally, Xavier Decoret
206
- has written
207
- [a great summary](http://artis.imag.fr/~Xavier.Decoret/resources/xdkbibtex/bibtex_summary.html)
208
- of the format; another invaluable source of information is [Nicolas Markey's
209
- website](http://www.lsv.ens-cachan.fr/~markey/bibla.php). Unfortunately,
210
- even after consulting these documents, a number of issues remain.
211
- Therefore, it is the purpose of this section to deliver the rationale
212
- that went into some of the design decision in BibTeX-Ruby.
213
-
214
- A BibTeX bibliography is typically stored in a file with the file
215
- extension '.bib'. This file may contain any number of BibTeX objects;
216
- everything that is not a BibTeX object is assumed to be a comment and
217
- ignored.
218
-
219
- The individual objects are discussed in further detail below. First, however, a
220
- number of general remarks:
221
-
222
- * BibTeX-Ruby begins in comment-mode, treating all text it encounters as comments.
223
- Normally these comments are ignored; however, if you wish the parser to include
224
- them, you can do so by adding the symbol `:meta_comments` to the `:include` array
225
- in the parser's options.
226
- * Note that string literals in BibTeX are either contained in quotes or braces;
227
- nested quotes in a quoted literal are not escaped with a usual backslash but
228
- must be placed inside braces. Nested braces must be balanced in literals, regardless
229
- of whether they are surrounded by quotes or braces.
230
- * Quoted strings and string constants (which are defined by @string objects) can be
231
- concatted by the '#' symbol. String literals in braces can not be concatted in
232
- this way.
233
- * The '@' symbol may only occur in quoted string literals (not in braced out literals)
234
- in the original BibTeX; note, however, that this is not true for BibTeX-Ruby (i.e.,
235
- it will parse any string containing an '@').
236
-
237
- ### @comment
238
-
239
-
240
- The purpose of the @comment object is not entirely clear, because everything
241
- outside of an object is treated as a comment anyway. Nicolas Markay argues that
242
- a @comment makes it possible to quickly comment out a number of consecutive
243
- objects; however, as Xavier Decoret points out that this does not work with the
244
- original `bibtex' program (following a @comment, it simply ignores everything
245
- until the end of the line). Indeed, on page 13 of [the original
246
- documentation](http://www.ctan.org/get/biblio/bibtex/contrib/doc/btxdoc.pdf),
247
- Oren Patashnik explains that @comment objects are not really necessary; they
248
- exist only for _Scribe_ system compatibility.
249
-
250
- Because they would be useless otherwise, BibTeX-Ruby treats @comment objects
251
- as Nicolas Markay describes them: thus, everything inside a @comment is treated
252
- as a comment and is ignored -- everything,
253
- that is, until the object is closed. For this reason, BibTeX-Ruby assumes that
254
- braces inside a @comment are balanced! Obviously, BibTeX-Ruby differs from
255
- `bibtex` in that respect; though, the gain is, that it is now possible to
256
- comment out a sequence of entries, without removing their respective '@' symbols.
257
-
258
- ### @string
259
-
260
- The @string object defines a single string constant (for multiple constant
261
- assignments, it is necessary to define separate @string objects). These
262
- constants can be used within string assignments in other @string or @preamble
263
- objects, as well as in regular BibTeX entries. For example, this is a valid constant
264
- definition and usage:
265
-
266
- @string{ generator = "BibTeX-Ruby"}
267
- @preamble{ "This bibliography was generated by " # generator }
268
-
269
-
270
- ### @preamble
271
-
272
- Typically, the purpose of @preamble objects is to define LaTeX statements, which
273
- will be put into the '.bbl' file by `bibtex`. A @preamble object may contain
274
- a single string literal, a single string constant (defined by a @string object), or
275
- a concatenation of literals and constants.
276
-
277
- ### Entries
278
-
279
- These represent proper BibTeX objects (e.g., @book, @collection, etc.).
306
+ look at the grammar definition in the file
307
+ [lib/bibtex/bibtex.y](https://github.com/inukshuk/bibtex-ruby/blob/master/lib/bibtex/bibtex.y).
280
308
 
309
+ For more information about the BibTeX format and the parser's idiosyncrasies
310
+ [refer to the project wiki](https://github.com/inukshuk/bibtex-ruby/wiki/The-BibTeX-Format).
281
311
 
282
- Credits
283
- -------
284
-
285
- The BibTeX-Ruby package was written by [Sylvester Keil](http://sylvester.keil.or.at/),
286
- with contributions by [Frank Fischer](https://github.com/lyro).
287
312
 
288
-
289
- License
313
+ Credits
290
314
  -------
291
315
 
292
- BibTeX-Ruby
293
- Copyright (C) 2010-2011 [Sylvester Keil](http://sylvester.keil.or.at)
294
-
295
- This program is free software: you can redistribute it and/or modify
296
- it under the terms of the GNU General Public License as published by
297
- the Free Software Foundation, either version 3 of the License, or
298
- (at your option) any later version.
299
-
300
- This program is distributed in the hope that it will be useful,
301
- but WITHOUT ANY WARRANTY; without even the implied warranty of
302
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
303
- GNU General Public License for more details.
304
-
305
- You should have received a copy of the GNU General Public License
306
- along with this program. If not, see <http://www.gnu.org/licenses/>.
316
+ The BibTeX-Ruby package was written by [Sylvester Keil](http://sylvester.keil.or.at/);
317
+ kudos and thanks to all [contributors](https://github.com/inukshuk/bibtex-ruby/contributors)!