bibtex-ruby 5.1.6 → 6.1.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.
- checksums.yaml +4 -4
- data/Gemfile +3 -4
- data/History.txt +40 -0
- data/README.md +315 -260
- data/Rakefile +0 -8
- data/bibtex-ruby.gemspec +2 -1
- data/features/support/env.rb +2 -2
- data/lib/bibtex/name_parser.rb +66 -64
- data/lib/bibtex/names.rb +1 -1
- data/lib/bibtex/parser.rb +25 -23
- data/lib/bibtex/version.rb +2 -2
- data/lib/bibtex.rb +6 -1
- data/test/bibtex/test_utilities.rb +1 -1
- data/test/helper.rb +1 -7
- data/test/test_bibtex.rb +1 -1
- data/test/test_export.rb +1 -1
- metadata +21 -7
data/README.md
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
BibTeX-Ruby
|
|
2
2
|
===========
|
|
3
|
-
[](http://travis-ci.org/inukshuk/bibtex-ruby)
|
|
4
|
-
[](https://coveralls.io/r/inukshuk/bibtex-ruby)
|
|
5
3
|
|
|
6
4
|
BibTeX-Ruby is the Rubyist's swiss-army-knife for all things BibTeX. It
|
|
7
5
|
includes a parser for all common BibTeX objects (@string, @preamble, @comment
|
|
@@ -10,7 +8,7 @@ formatted names; BibTeX-Ruby recognizes BibTeX string replacements, joins
|
|
|
10
8
|
values containing multiple strings or variables, supports cross-references,
|
|
11
9
|
and decodes common LaTeX formatting instructions to unicode; if you are in a
|
|
12
10
|
hurry, it also allows for easy export/conversion to formats such as YAML,
|
|
13
|
-
JSON, CiteProc/CSL, XML (BibTeXML), and RDF (experimental).
|
|
11
|
+
JSON, CiteProc/CSL, XML (BibTeXML, requires `rexml`), and RDF (experimental).
|
|
14
12
|
|
|
15
13
|
For a list of projects using BibTeX-Ruby, take a look at the
|
|
16
14
|
[project wiki](https://github.com/inukshuk/bibtex-ruby/wiki/Projects-Using-BibTeX-Ruby).
|
|
@@ -26,50 +24,62 @@ Install and load BibTeX-Ruby in an IRB session:
|
|
|
26
24
|
|
|
27
25
|
Open a BibTeX bibliography:
|
|
28
26
|
|
|
29
|
-
|
|
27
|
+
```ruby
|
|
28
|
+
b = BibTeX.open('./ruby.bib')
|
|
29
|
+
```
|
|
30
30
|
|
|
31
31
|
Select a BibTeX entry and access individual fields:
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
```ruby
|
|
34
|
+
b['pickaxe'].title
|
|
35
|
+
#=> "Programming Ruby 1.9: The Pragmatic Programmer's Guide"
|
|
36
|
+
b[:pickaxe].author.length
|
|
37
|
+
#=> 3
|
|
38
|
+
b[:pickaxe].author.to_s
|
|
39
|
+
#=> "Thomas, D. and Fowler, Chad and Hunt, Andy"
|
|
40
|
+
b[:pickaxe].author[2].first
|
|
41
|
+
#=> "Andy"
|
|
42
|
+
```
|
|
41
43
|
|
|
42
44
|
Query a bibliography:
|
|
43
45
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
```ruby
|
|
47
|
+
b['@book'].length
|
|
48
|
+
#=> 3 - the number books in the bibliography
|
|
49
|
+
b['@article'].length
|
|
50
|
+
#=> 0 - the number of articles in the bibliography
|
|
51
|
+
b['@book[year=2009]'].length
|
|
52
|
+
#=> 1 - the number of books published in 2009
|
|
53
|
+
```
|
|
50
54
|
|
|
51
55
|
Extend first name initials throughout your bibliography:
|
|
52
56
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
57
|
+
```ruby
|
|
58
|
+
b.extend_initials ['Dave', 'Thomas']
|
|
59
|
+
b[:pickaxe].author.to_s
|
|
60
|
+
#=> "Thomas, Dave and Fowler, Chad and Hunt, Andy"
|
|
61
|
+
```
|
|
56
62
|
|
|
57
63
|
You can also extend all names in the bibliography to their prototypical
|
|
58
64
|
(i.e., the longest available) form:
|
|
59
65
|
|
|
60
|
-
|
|
66
|
+
```ruby
|
|
67
|
+
b.extend_initials! #=> extends all names in the bibliography
|
|
68
|
+
```
|
|
61
69
|
|
|
62
70
|
Use with caution as this method will treat two names as identical if they
|
|
63
71
|
look the same in their `#sort_order(:initials => true)` form.
|
|
64
72
|
|
|
65
73
|
Unify certain fields across the bibliography:
|
|
66
74
|
|
|
67
|
-
|
|
75
|
+
```ruby
|
|
76
|
+
b.unify :publisher, /o'?reilly/i, "O'Reilly"
|
|
68
77
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
78
|
+
b.unify :publisher, /^penguin/i do |entry|
|
|
79
|
+
entry.publisher = 'Penguin Books'
|
|
80
|
+
entry.address = 'London'
|
|
81
|
+
end
|
|
82
|
+
```
|
|
73
83
|
|
|
74
84
|
This will unify various spellings of entries published by O'Reilly and Penguin.
|
|
75
85
|
|
|
@@ -77,24 +87,24 @@ Render your bibliography in one of
|
|
|
77
87
|
[many different citation styles](https://github.com/citation-style-language/styles)
|
|
78
88
|
(requires the **citeproc-ruby** gem):
|
|
79
89
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
Guide. Raleigh, North Carolina: The Pragmatic Bookshelf, 2009."
|
|
90
|
+
```ruby
|
|
91
|
+
require 'citeproc'
|
|
92
|
+
CiteProc.process b[:pickaxe].to_citeproc, :style => :apa
|
|
93
|
+
#=> "Thomas, D., Fowler, C., & Hunt, A. (2009). Programming Ruby 1.9: The Pragmatic Programmer's Guide. The Facets of Ruby. Raleigh, North Carolina: The Pragmatic Bookshelf."
|
|
94
|
+
CiteProc.process b[:pickaxe].to_citeproc, :style => 'chicago-author-date'
|
|
95
|
+
#=> "Thomas, Dave, Chad Fowler, and Andy Hunt. 2009. Programming Ruby 1.9: The Pragmatic Programmer's Guide. The Facets of Ruby.Raleigh, North Carolina: The Pragmatic Bookshelf."
|
|
96
|
+
CiteProc.process b[:pickaxe].to_citeproc, :style => :mla
|
|
97
|
+
#=> "Thomas, Dave, Chad Fowler, and Andy Hunt. Programming Ruby 1.9: The Pragmatic Programmer's Guide. Raleigh, North Carolina: The Pragmatic Bookshelf, 2009."
|
|
98
|
+
```
|
|
90
99
|
|
|
91
100
|
Save a bibliography to a file:
|
|
92
101
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
102
|
+
```ruby
|
|
103
|
+
b.save
|
|
104
|
+
#=> saves the original file
|
|
105
|
+
b.save_to(file)
|
|
106
|
+
#=> saves the bibliography in a new file
|
|
107
|
+
```
|
|
98
108
|
|
|
99
109
|
Compatibility
|
|
100
110
|
-------------
|
|
@@ -108,6 +118,8 @@ Starting with BibTeX-Ruby version 3.0, support for Ruby versions 1.9.2 and
|
|
|
108
118
|
earlier has been dropped; most features will likely continue to work, but
|
|
109
119
|
compliance with old Rubies is not a priority going forward.
|
|
110
120
|
|
|
121
|
+
Starting with BibTex-Ruby version 7.0, only ruby versions still in "security maintenance phase" will be supported.
|
|
122
|
+
This is, as of writing Ruby 2.7 and up.
|
|
111
123
|
|
|
112
124
|
Documentation
|
|
113
125
|
-------------
|
|
@@ -121,16 +133,18 @@ everything, simply add `:include => [:meta_content]` to your invocation of
|
|
|
121
133
|
Once BibTeX-Ruby has parsed your '.bib' file, you can easily access individual
|
|
122
134
|
entries. For example, if you set up your bibliography as follows:
|
|
123
135
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
136
|
+
```ruby
|
|
137
|
+
b = BibTeX.parse <<-END
|
|
138
|
+
@book{pickaxe,
|
|
139
|
+
address = {Raleigh, North Carolina},
|
|
140
|
+
author = {Thomas, Dave and Fowler, Chad and Hunt, Andy},
|
|
141
|
+
publisher = {The Pragmatic Bookshelf},
|
|
142
|
+
series = {The Facets of Ruby},
|
|
143
|
+
title = {Programming Ruby 1.9: The Pragmatic Programmer's Guide},
|
|
144
|
+
year = {2009}
|
|
145
|
+
}
|
|
146
|
+
END
|
|
147
|
+
```
|
|
134
148
|
|
|
135
149
|
You could easily access it, using the entry's key, 'pickaxe', like so:
|
|
136
150
|
`b[:pickaxe]`; you also have easy access to individual fields, for example:
|
|
@@ -149,31 +163,36 @@ problem with a field's value, simply convert it to a string by calling the
|
|
|
149
163
|
Instead of parsing strings you can also create BibTeX elements directly in
|
|
150
164
|
Ruby:
|
|
151
165
|
|
|
152
|
-
|
|
166
|
+
```ruby
|
|
167
|
+
bib = BibTeX::Bibliography.new
|
|
168
|
+
```
|
|
153
169
|
|
|
154
170
|
Using a Hash:
|
|
155
171
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
172
|
+
```ruby
|
|
173
|
+
bib << BibTeX::Entry.new({
|
|
174
|
+
:bibtex_type => :book,
|
|
175
|
+
:key => :rails,
|
|
176
|
+
:address => 'Raleigh, North Carolina',
|
|
177
|
+
:author => 'Ruby, Sam and Thomas, Dave, and Hansson, David Heinemeier',
|
|
178
|
+
:booktitle => 'Agile Web Development with Rails',
|
|
179
|
+
:edition => 'third',
|
|
180
|
+
:keywords => 'ruby, rails',
|
|
181
|
+
:publisher => 'The Pragmatic Bookshelf',
|
|
182
|
+
:series => 'The Facets of Ruby',
|
|
183
|
+
:title => 'Agile Web Development with Rails',
|
|
184
|
+
:year => '2009'
|
|
185
|
+
})
|
|
186
|
+
```
|
|
169
187
|
|
|
170
188
|
Or programmatically:
|
|
171
189
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
190
|
+
```ruby
|
|
191
|
+
book = BibTeX::Entry.new
|
|
192
|
+
book.type = :book
|
|
193
|
+
book.key = :mybook
|
|
194
|
+
bib << book
|
|
195
|
+
```
|
|
177
196
|
|
|
178
197
|
### Cross References
|
|
179
198
|
|
|
@@ -183,24 +202,25 @@ and `inproceedings`. When an entry has a valid citation key in the field
|
|
|
183
202
|
`crossref`, BibTeX-Ruby will return any fields inherited from the parent
|
|
184
203
|
entry:
|
|
185
204
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
205
|
+
```ruby
|
|
206
|
+
b = BibTeX.parse <<-END
|
|
207
|
+
@inbook{fraassen_1989b,
|
|
208
|
+
Crossref = {fraassen_1989},
|
|
209
|
+
Pages = {40-64},
|
|
210
|
+
Title = {Ideal Science: David Lewis's Account of Laws},
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
@book{fraassen_1989,
|
|
214
|
+
Address = {Oxford},
|
|
215
|
+
Author = {Bas C. van Fraassen},
|
|
216
|
+
Publisher = {Oxford University Press},
|
|
217
|
+
Title = {Laws and Symmetry},
|
|
218
|
+
Year = 1989
|
|
219
|
+
}
|
|
220
|
+
END
|
|
221
|
+
> b['fraassen_1989b'].booktitle
|
|
222
|
+
#=> <"Laws and Symmetry">
|
|
223
|
+
```
|
|
204
224
|
|
|
205
225
|
### Queries
|
|
206
226
|
|
|
@@ -210,70 +230,75 @@ Additionally, you can access individual elements or groups of elements via
|
|
|
210
230
|
their index using `Bibliography#[]`; this accessor also exposes some of the
|
|
211
231
|
query functionality with the exception of yielding to a block. For instance:
|
|
212
232
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
233
|
+
```ruby
|
|
234
|
+
bib[-1]
|
|
235
|
+
#=> Returns the last element of the Bibliography or nil
|
|
236
|
+
bib[1,2]
|
|
237
|
+
#=> Returns the second and third elements or nil
|
|
238
|
+
bib[1..2]
|
|
239
|
+
#=> Same as above
|
|
240
|
+
|
|
241
|
+
bib[:key]
|
|
242
|
+
#=> Returns the first entry with key 'key' or nil
|
|
243
|
+
bib['key']
|
|
244
|
+
#=> Returns all entries with key 'key' or []
|
|
245
|
+
|
|
246
|
+
bib['@article']
|
|
247
|
+
#=> Returns all entries of type 'article' or []
|
|
248
|
+
bib['!@book']
|
|
249
|
+
#=> Returns all entries of any type other than 'book' or []
|
|
250
|
+
bib['@preamble']
|
|
251
|
+
#=> Returns all preamble objects (this is the same as Bibliography#preambles) or []
|
|
252
|
+
bib[/ruby/]
|
|
253
|
+
#=> Returns all objects that match 'ruby' anywhere or []
|
|
254
|
+
|
|
255
|
+
bib['@incollection[booktitle]']
|
|
256
|
+
#=> Returns all in-collection entries with a booktitle or []
|
|
257
|
+
|
|
258
|
+
# note that the above includes entries inheriting the book title
|
|
259
|
+
# from a cross-referenced entry!
|
|
260
|
+
|
|
261
|
+
bib['@book[keywords=ruby]']
|
|
262
|
+
#=> Returns all books whose keywords attribute equals 'ruby' or []
|
|
263
|
+
bib['@book[keywords!=ruby]']
|
|
264
|
+
#=> Returns all books whose keywords attribute does not equal 'ruby'
|
|
265
|
+
bib['@book[keywords/=ruby]']
|
|
266
|
+
#=> Same as above
|
|
267
|
+
|
|
268
|
+
bib.q('@book[keywords ^= ruby]')
|
|
269
|
+
#=> Returns all books whose keywords attribute matches /^ruby/
|
|
270
|
+
bib.q('@book[keywords ~= ruby]')
|
|
271
|
+
#=> Returns all books whose keywords attribute matches /ruby/
|
|
272
|
+
bib['@book[keywords!~ruby]']
|
|
273
|
+
#=> Returns all books whose keywords attribute does not match /ruby/ or don't have keywords attribute
|
|
274
|
+
|
|
275
|
+
bib.q('@article[year<=2007]')
|
|
276
|
+
#=> Returns all articles published in 2007 or earlier
|
|
277
|
+
bib.query('@book') { |e| e.keywords.split(/,/).length > 1 }
|
|
278
|
+
#=> Returns all book entries with two or more keywords or []
|
|
279
|
+
```
|
|
258
280
|
|
|
259
281
|
Queries offer syntactic sugar for common enumerator invocations:
|
|
260
282
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
283
|
+
```ruby
|
|
284
|
+
bib.query(:all, '@book')
|
|
285
|
+
#=> same as bib.select { |b| b.has_type?(:book) }
|
|
286
|
+
bib.query('@book')
|
|
287
|
+
#=> same as above
|
|
288
|
+
bib.query(:first, '@book')
|
|
289
|
+
#=> same as bib.detect { |b| b.has_type?(:book) }
|
|
290
|
+
bib.query(:none, '@book')
|
|
291
|
+
#=> same as bib.reject { |b| b.has_type?(:book) }
|
|
292
|
+
```
|
|
269
293
|
|
|
270
294
|
You can also use queries to delete entries in your bibliography:
|
|
271
295
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
296
|
+
```ruby
|
|
297
|
+
bib.delete(/ruby/)
|
|
298
|
+
#=> deletes all object that match 'ruby' in their string representation
|
|
299
|
+
bib.delete('@comment')
|
|
300
|
+
#=> strips all BibTeX comments from the bibliography
|
|
301
|
+
```
|
|
277
302
|
|
|
278
303
|
### String Replacement
|
|
279
304
|
|
|
@@ -284,39 +309,43 @@ You can replace the string symbols of an object by calling the object's
|
|
|
284
309
|
the **replace** method. Thus, to replace all strings defined in bibliography
|
|
285
310
|
b you could use the following code:
|
|
286
311
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
312
|
+
```ruby
|
|
313
|
+
b.each do |obj|
|
|
314
|
+
obj.replace(b.q('@string'))
|
|
315
|
+
end
|
|
316
|
+
```
|
|
317
|
+
|
|
291
318
|
A shorthand version for replacing all strings in a given bibliography is the
|
|
292
319
|
`Bibliography#replace` method. Similarly, you can use the
|
|
293
320
|
`Bibliography#join` method to join individual strings together. For instance:
|
|
294
321
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
}
|
|
322
|
+
```ruby
|
|
323
|
+
bib = BibTeX::Bibliography.new
|
|
324
|
+
bib.add BibTeX::Element.parse '@string{ foo = "foo" }'
|
|
325
|
+
bib << BibTeX::Element.parse '@string{ bar = "bar" }'
|
|
326
|
+
bib.add BibTeX::Element.parse <<-END
|
|
327
|
+
@book{abook,
|
|
328
|
+
author = foo # "Author",
|
|
329
|
+
title = foo # bar
|
|
330
|
+
}
|
|
331
|
+
END
|
|
332
|
+
puts bib[:abook].to_s
|
|
333
|
+
#@book{abook,
|
|
334
|
+
# author = foo # "Author",
|
|
335
|
+
# title = foo # bar
|
|
336
|
+
#}
|
|
337
|
+
bib.replace
|
|
338
|
+
puts bib[:abook].to_s
|
|
339
|
+
#@book{abook,
|
|
340
|
+
# author = "foo" # "Author",
|
|
341
|
+
# title = "foo" # "bar"
|
|
342
|
+
#}
|
|
343
|
+
bib.join
|
|
344
|
+
#@book{abook,
|
|
345
|
+
# author = {fooAuthor},
|
|
346
|
+
# title = {foobar}
|
|
347
|
+
#}
|
|
348
|
+
```
|
|
320
349
|
|
|
321
350
|
### Names
|
|
322
351
|
|
|
@@ -335,34 +364,42 @@ symbols that cannot be replaced will not be parsed.
|
|
|
335
364
|
In the following example, string replacement can take place, thus all names
|
|
336
365
|
are parsed and can easily be mapped to their last names:
|
|
337
366
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
367
|
+
```ruby
|
|
368
|
+
BibTeX.parse(<<-END)[1].author.map(&:last)
|
|
369
|
+
@string{ ht = "Nathaniel Hawthorne" }
|
|
370
|
+
@book{key,
|
|
371
|
+
author = ht # " and Melville, Herman"
|
|
372
|
+
}
|
|
373
|
+
END
|
|
374
|
+
#=> ["Hawthorne", "Melville"]
|
|
375
|
+
```
|
|
345
376
|
|
|
346
377
|
Another useful method is `Bibliography#names` which returns all names in
|
|
347
378
|
your bibliography (authors, editors, translators). For example, to quickly
|
|
348
379
|
expand the initials of a name across your entire bibliography, you could
|
|
349
380
|
use the following snippet:
|
|
350
381
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
382
|
+
```ruby
|
|
383
|
+
b.names.each do |name|
|
|
384
|
+
if name.sort_order =~ /^Poe, E/
|
|
385
|
+
name.first = 'Edgar Allen'
|
|
386
|
+
end
|
|
387
|
+
end
|
|
388
|
+
```
|
|
356
389
|
|
|
357
390
|
There is also a short-hand for this use case:
|
|
358
391
|
|
|
359
|
-
|
|
392
|
+
```ruby
|
|
393
|
+
b.extend_initials ['Edgar Allen', 'Poe']
|
|
394
|
+
```
|
|
360
395
|
|
|
361
396
|
Alternatively, if your bibliography contains the same names in various
|
|
362
397
|
forms (e.g., 'Poe, Edgar A.', 'Poe, E.A.', 'Poe, E. Allen') you can also
|
|
363
398
|
set all names to their longest available form:
|
|
364
399
|
|
|
365
|
-
|
|
400
|
+
```ruby
|
|
401
|
+
b.extend_initials!
|
|
402
|
+
```
|
|
366
403
|
|
|
367
404
|
Use with caution, though, as this method will treat names as identical
|
|
368
405
|
as long as their initials are the same. That is to say, 'Poe, Eric A.' would
|
|
@@ -376,11 +413,13 @@ names or initials, titles using different casing, different keywords etc.).
|
|
|
376
413
|
BibTex-Ruby allows you to group your bibliography by any number of fields
|
|
377
414
|
in order to detect such duplicate entries.
|
|
378
415
|
|
|
379
|
-
|
|
380
|
-
|
|
416
|
+
```ruby
|
|
417
|
+
b.select_duplicates_by :year, :title
|
|
418
|
+
#=> groups the bibliography by using the year and title field as key
|
|
381
419
|
|
|
382
|
-
|
|
383
|
-
|
|
420
|
+
b.duplicates?
|
|
421
|
+
#=> whether or not the bibliography contains any duplicates
|
|
422
|
+
```
|
|
384
423
|
|
|
385
424
|
For more complex requirements you can use the `#group_by` method directly.
|
|
386
425
|
This methods accepts a list of arguments whose value will be used for grouping
|
|
@@ -390,25 +429,29 @@ digest.
|
|
|
390
429
|
|
|
391
430
|
The duplicate methods above, for example, do something like this:
|
|
392
431
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
432
|
+
```ruby
|
|
433
|
+
group_by(:year, :title) do |digest, entry|
|
|
434
|
+
digest.gsub(/\s+/, '').downcase
|
|
435
|
+
end
|
|
436
|
+
```
|
|
396
437
|
|
|
397
438
|
You can use this method, for example, to match entries only by their author's
|
|
398
439
|
last name and so on and so forth.
|
|
399
440
|
|
|
400
441
|
Since version 4.1.0, BibTeX-ruby supports parsing of names spelled in the East Asian order (last name, then first name). Names contain a Chinese, Japanese or Korean letter are currently assumed to be such names. If the name is written in comma-separated manner, it is parsed in the normal way.
|
|
401
442
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
443
|
+
```ruby
|
|
444
|
+
b = BibTeX.parse(<<-END)
|
|
445
|
+
@book{key,
|
|
446
|
+
title = "プログラミング言語 Ruby",
|
|
447
|
+
author = "David Flanagan and まつもと ゆきひろ",
|
|
448
|
+
translator = "卜部, 昌平 and 長尾, 高弘",
|
|
449
|
+
year = "2009",
|
|
450
|
+
publisher = "O'Reilly Japan"
|
|
451
|
+
}
|
|
452
|
+
END
|
|
453
|
+
[*b[0].author, *b[0].translator].map(&:last) #=> [Flanagan, まつもと, 卜部, 長尾]
|
|
454
|
+
```
|
|
412
455
|
|
|
413
456
|
### Filters
|
|
414
457
|
|
|
@@ -419,24 +462,30 @@ to a given filter. Starting with version 1.3.9 BibTeX-Ruby includes a
|
|
|
419
462
|
LaTeX filter that depends on the
|
|
420
463
|
[latex-decode gem](http://rubygems.org/gems/latex-decode). Example:
|
|
421
464
|
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
465
|
+
```ruby
|
|
466
|
+
faust = '@book{faust, title = {Faust: Der Trag\"odie Erster Teil}}'
|
|
467
|
+
BibTeX.parse(faust).convert(:latex)[:faust].title
|
|
468
|
+
#=> "Faust: Der Tragödie Erster Teil"
|
|
469
|
+
```
|
|
425
470
|
|
|
426
471
|
Conditional conversions are also supported:
|
|
427
472
|
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
473
|
+
```ruby
|
|
474
|
+
faust1 = '@book{faust1, title = {Faust: Der Trag\"odie Erster Teil}}'
|
|
475
|
+
faust2 = '@book{faust2, title = {Faust: Der Trag\"odie Zweiter Teil}}'
|
|
476
|
+
p BibTeX.parse(faust1 + faust2).convert(:latex) { |e| e.key == :faust2 }.to_s
|
|
477
|
+
```
|
|
478
|
+
|
|
432
479
|
Returns:
|
|
433
480
|
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
481
|
+
```bibtex
|
|
482
|
+
@book{faust1,
|
|
483
|
+
title = {Faust: Der Trag\"odie Erster Teil}
|
|
484
|
+
}
|
|
485
|
+
@book{faust2,
|
|
486
|
+
title = {Faust: Der Tragödie Zweiter Teil}
|
|
487
|
+
}
|
|
488
|
+
```
|
|
440
489
|
|
|
441
490
|
If you need to express a condition on the basis of individual fields, use the
|
|
442
491
|
conversion methods of BibTeX::Entry with a block instead (the block will be
|
|
@@ -446,8 +495,9 @@ When working with Bibliographies that contain LaTeX it is often best to
|
|
|
446
495
|
apply the filter upon opening or parsing the Bibliography. You can do this,
|
|
447
496
|
by passing the `:filter` option:
|
|
448
497
|
|
|
449
|
-
|
|
450
|
-
|
|
498
|
+
```ruby
|
|
499
|
+
BibTeX.open 'references.bib', :filter => :latex
|
|
500
|
+
```
|
|
451
501
|
|
|
452
502
|
### Exports
|
|
453
503
|
|
|
@@ -463,23 +513,27 @@ In order to export your bibliography use **#to\_s**, **#to\_yaml**,
|
|
|
463
513
|
**#to\_json**, or **#to\_xml**, respectively. For example, the following line
|
|
464
514
|
constitutes a simple BibTeX to YAML converter:
|
|
465
515
|
|
|
466
|
-
|
|
516
|
+
```ruby
|
|
517
|
+
BibTeX.open('example.bib').to_yaml
|
|
518
|
+
```
|
|
467
519
|
|
|
468
520
|
Starting with version 2.0, BibTeX-Ruby's `#to_xml` exports your bibliography
|
|
469
|
-
to the [BibTeXML](http://bibtexml.sf.net/) format
|
|
521
|
+
to the [BibTeXML](http://bibtexml.sf.net/) format via `rexml`. By passing the option
|
|
470
522
|
`:extended => true` you can make use of the BibTeXML's extended format which
|
|
471
523
|
will return individual person elements and name tokens (provided you have
|
|
472
524
|
successfully parsed the names of your bibliography).
|
|
473
525
|
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
526
|
+
```ruby
|
|
527
|
+
BibTeX.parse(<<-END).to_xml(:extended => true).write($stdout, 2)
|
|
528
|
+
@book{pickaxe,
|
|
529
|
+
Address = {Raleigh, North Carolina},
|
|
530
|
+
Author = {Thomas, Dave, and Fowler, Chad, and Hunt, Andy},
|
|
531
|
+
Publisher = {The Pragmatic Bookshelf},
|
|
532
|
+
Title = {Programming Ruby 1.9: The Pragmatic Programmer's Guide},
|
|
533
|
+
Year = {2009}
|
|
534
|
+
}
|
|
535
|
+
END
|
|
536
|
+
```
|
|
483
537
|
|
|
484
538
|
This example parse a BibTeX entry, formats it as extended BibTeXML,
|
|
485
539
|
and writes the following XML to standard out:
|
|
@@ -529,35 +583,38 @@ hash which lets you define what quotes to use (note that BibTeX-Ruby will
|
|
|
529
583
|
always use regular double quotes if a value consists of more than one token,
|
|
530
584
|
because these tokens will be concatenated using BibTeX's '#' operator).
|
|
531
585
|
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
586
|
+
```ruby
|
|
587
|
+
BibTeX.parse(<<-END).to_a # implies: :quotes => ['{','}']
|
|
588
|
+
@book{pickaxe,
|
|
589
|
+
Address = {Raleigh, North Carolina},
|
|
590
|
+
Author = {Thomas, Dave, and Fowler, Chad, and Hunt, Andy},
|
|
591
|
+
Publisher = {The Pragmatic Bookshelf},
|
|
592
|
+
Title = {Programming Ruby 1.9: The Pragmatic Programmer's Guide},
|
|
593
|
+
Year = {2009}
|
|
594
|
+
}
|
|
595
|
+
END
|
|
596
|
+
#=> [{:bibtex_key=>:pickaxe, :bibtex_type=>:book,
|
|
597
|
+
# :address=>"{Raleigh, North Carolina}",
|
|
598
|
+
# :author=>"{Thomas, Dave, and Fowler, Chad, and Hunt, Andy}",
|
|
599
|
+
# :publisher=>"{The Pragmatic Bookshelf}",
|
|
600
|
+
# :title=>"{Programming Ruby 1.9: The Pragmatic Programmer's Guide}",
|
|
601
|
+
# :year=>"{2009}"}]
|
|
602
|
+
```
|
|
547
603
|
|
|
548
604
|
For post-processing in Ruby most of the time you do not need any explicit
|
|
549
605
|
quotes; therefore you can simply add the :quotes option with an empty string:
|
|
550
606
|
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
607
|
+
```ruby
|
|
608
|
+
>> BibTeX.parse(<<-END).to_a(:quotes => '')
|
|
609
|
+
...
|
|
610
|
+
END
|
|
611
|
+
#=> [{:bibtex_key=>:pickaxe, :bibtex_type=>:book,
|
|
612
|
+
# :address=>"Raleigh, North Carolina",
|
|
613
|
+
# :author=>"Thomas, Dave, and Fowler, Chad, and Hunt, Andy",
|
|
614
|
+
# :publisher=>"The Pragmatic Bookshelf",
|
|
615
|
+
# :title=>"Programming Ruby 1.9: The Pragmatic Programmer's Guide",
|
|
616
|
+
# :year=>"2009"}]
|
|
617
|
+
```
|
|
561
618
|
|
|
562
619
|
The Parser
|
|
563
620
|
----------
|
|
@@ -601,8 +658,6 @@ To execute the test suite continuously while you're working run:
|
|
|
601
658
|
|
|
602
659
|
$ bundle exec guard
|
|
603
660
|
|
|
604
|
-
|
|
605
|
-
|
|
606
661
|
Credits
|
|
607
662
|
-------
|
|
608
663
|
|