bibtex-ruby 2.0.8 → 2.0.9

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.

@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bibtex-ruby (2.0.8)
4
+ bibtex-ruby (2.0.9)
5
5
  latex-decode (>= 0.0.6)
6
6
  multi_json (~> 1.3)
7
7
 
@@ -1,3 +1,9 @@
1
+ 2.0.9 / 2012-05-15
2
+ ==================
3
+
4
+ * Added extend_initials! method to extend all names in the bibliography
5
+ * Added unify method as a shorthand to unify fields across the bibliography
6
+
1
7
  2.0.8 / 2012-05-11
2
8
  ==================
3
9
 
data/README.md CHANGED
@@ -54,6 +54,25 @@ Extend first name initials throughout your bibliography:
54
54
  b[:pickaxe].author.to_s
55
55
  #=> "Thomas, Dave and Fowler, Chad and Hunt, Andy"
56
56
 
57
+ You can also extend all names in the bibliography to their prototypical
58
+ (i.e., the longest available) form:
59
+
60
+ b.extend_initials! #=> extends all names in the bibliography
61
+
62
+ Use with caution as this method will treat two names as identical if they
63
+ look the same in their `#sort_order(:initials => true)` form.
64
+
65
+ Unify certain fields across the bibliography:
66
+
67
+ b.unify :publisher, /o'?reilly/i, "O'Reilly"
68
+
69
+ b.unify :publisher, /^penguin/i do |entry|
70
+ entry.publisher = 'Penguin Books'
71
+ entry.address = 'London'
72
+ end
73
+
74
+ This will unify various spellings of entries published by O'Reilly and Penguin.
75
+
57
76
  Render your bibliography in one of
58
77
  [many different citation styles](https://github.com/citation-style-language/styles)
59
78
  (requires the **citeproc-ruby** gem):
@@ -161,17 +180,17 @@ BibTeX-Ruby will return any fields inherited from the parent entry:
161
180
 
162
181
  > b = BibTeX.parse <<-END
163
182
  @inbook{fraassen_1989b,
164
- Crossref = {fraassen_1989},
165
- Pages = {40-64},
166
- Title = {Ideal Science: David Lewis's Account of Laws},
183
+ Crossref = {fraassen_1989},
184
+ Pages = {40-64},
185
+ Title = {Ideal Science: David Lewis's Account of Laws},
167
186
  }
168
187
 
169
188
  @book{fraassen_1989,
170
- Address = {Oxford},
171
- Author = {Bas C. van Fraassen},
172
- Publisher = {Oxford University Press},
173
- Title = {Laws and Symmetry},
174
- Year = 1989
189
+ Address = {Oxford},
190
+ Author = {Bas C. van Fraassen},
191
+ Publisher = {Oxford University Press},
192
+ Title = {Laws and Symmetry},
193
+ Year = 1989
175
194
  }
176
195
  END
177
196
  > b['fraassen_1989b'].booktitle
@@ -313,6 +332,20 @@ use the following snippet:
313
332
  end
314
333
  end
315
334
 
335
+ There is also a short-hand for this use case:
336
+
337
+ b.extend_initials ['Edgar Allen', 'Poe']
338
+
339
+ Alternatively, if your bibliography contains the same names in various
340
+ forms (e.g., 'Poe, Edgar A.', 'Poe, E.A.', 'Poe, E. Allen') you can also
341
+ set all names to their longest available form:
342
+
343
+ b.extend_initials!
344
+
345
+ Use with caution, though, as this method will treat names as identical
346
+ as long as their initials are the same. That is to say, 'Poe, Eric A.' would
347
+ be extend to 'Poe, Edgar Allen'.
348
+
316
349
 
317
350
  ### Filters
318
351
 
@@ -351,7 +384,7 @@ apply the filter upon opening or parsing the Bibliography. You can do this,
351
384
  by passing the `:filter` option:
352
385
 
353
386
  BibTeX.open 'references.bib', :filter => :latex
354
-
387
+
355
388
 
356
389
  ### Exports
357
390
 
@@ -290,12 +290,74 @@ module BibTeX
290
290
  # b.extend_initials(['Edgar Allen', 'Poe'], ['Nathaniel', 'Hawthorne'])
291
291
  # #=> Extends the initials in names like 'E.A. Poe' or 'Hawethorne, N.'
292
292
  # in the bibliography.
293
+ #
294
+ # Extends the initials for the given names. Returns the Bibliography.
293
295
  def extend_initials(*arguments)
294
296
  arguments.each do |with_first, for_last|
295
297
  names.each do |name|
296
298
  name.extend_initials(with_first, for_last)
297
299
  end
298
300
  end
301
+
302
+ self
303
+ end
304
+
305
+ # This method combines all names in the bibliography that look identical
306
+ # when using initials as first names and then tries to extend the first
307
+ # names for all names in each group to the longest available form.
308
+ # Returns the bibliography.
309
+ #
310
+ # If your bibliography contains the names 'Poe, Edgar A.', 'Poe, E.A.',
311
+ # and 'Poe, E. A.' calling this method would convert all three names to
312
+ # 'Poe, Edgar A.'.
313
+ def extend_initials!
314
+ groups = Hash.new do |h,k|
315
+ h[k] = { :prototype => nil, :names => [] }
316
+ end
317
+
318
+ # group names together
319
+ names.each do |name|
320
+ group = groups[name.sort_order(:initials => true).gsub(/\s+/, '')]
321
+ group[:names] << name
322
+
323
+ if group[:prototype].nil? || group[:prototype].first.length < name.first.length
324
+ group[:prototype] = name
325
+ end
326
+ end
327
+
328
+ # extend all names in group to prototype
329
+ groups.each_value do |group|
330
+ group[:names].each do |name|
331
+ name.set(group[:prototype])
332
+ end
333
+ end
334
+
335
+ self
336
+ end
337
+
338
+ # call-seq:
339
+ # b.unify :publisher, /o'?reilly/i, "O'Reilly"
340
+ # #=> Unifies the publisher name "O'Reilly" in the bibliography
341
+ #
342
+ # Sets all fields matching the passed-in pattern to the supplied value.
343
+ # If a block is given, each matching entry will be passed to the block
344
+ # instead. Returns the bibliography.
345
+ def unify(field, pattern, value = nil)
346
+ pattern = Regexp.new(pattern) unless pattern.is_a?(Regexp)
347
+
348
+ block = if block_given?
349
+ Proc.new
350
+ else
351
+ Proc.new { |e| e[field] = value }
352
+ end
353
+
354
+ each_entry do |entry|
355
+ if entry.field?(field) && entry[field].to_s =~ pattern
356
+ block.call(entry)
357
+ end
358
+ end
359
+
360
+ self
299
361
  end
300
362
 
301
363
  def sort(*arguments, &block)
@@ -416,7 +478,15 @@ module BibTeX
416
478
  end
417
479
 
418
480
  alias q query
419
-
481
+
482
+ def each_entry
483
+ if block_given?
484
+ q('@entry').each(&Proc.new)
485
+ else
486
+ q('@entry').to_enum
487
+ end
488
+ end
489
+
420
490
  def find_by_type(*types, &block)
421
491
  q(types.flatten.compact.map { |t| "@#{t}" }.join(', '), &block)
422
492
  end
@@ -151,7 +151,7 @@ module BibTeX
151
151
 
152
152
  # Set the name tokens to the values defined in the passed-in hash.
153
153
  def set(attributes = {})
154
- attributes.each do |key, value|
154
+ attributes.each_pair do |key, value|
155
155
  send("#{key}=", value) if respond_to?(key)
156
156
  end
157
157
 
@@ -18,6 +18,6 @@
18
18
 
19
19
  module BibTeX
20
20
  module Version
21
- STRING = '2.0.8'.freeze
21
+ STRING = '2.0.9'.freeze
22
22
  end
23
23
  end
@@ -68,6 +68,14 @@ module BibTeX
68
68
  publisher={O'Reilly}
69
69
  }
70
70
  @string{ foo = "foobar" }
71
+ @misc{flanagan-1,
72
+ title={{Foo Bar}},
73
+ author={Flanagan, Dav}
74
+ }
75
+ @misc{flanagan-2,
76
+ title={{Foo Bar}},
77
+ author={Flanagan, David}
78
+ }
71
79
  END
72
80
  end
73
81
 
@@ -85,6 +93,36 @@ module BibTeX
85
93
  end
86
94
  end
87
95
 
96
+ describe '#extend_initials!' do
97
+ it 'extends the initials of all names to the longest prototype' do
98
+ assert_equal "Ruby, Sam Thomas, Dave Hansson Heinemeier, David Flanagan, David Matsumoto, Y. Segaran, T.",
99
+ @bib.extend_initials!.names.map(&:sort_order).uniq.join(' ')
100
+ end
101
+ end
102
+
103
+ describe '#unify' do
104
+ it 'sets all fields matching the given pattern to the passed-in value' do
105
+ @bib.unify :publisher, /reilly/i, 'OReilly'
106
+ assert_equal 'OReilly', @bib['segaran2007'].publisher
107
+ assert_equal 'OReilly', @bib['flanagan2008'].publisher
108
+ end
109
+
110
+ it 'does not change the value of fields that do not match' do
111
+ @bib.unify :publisher, /reilly/i, 'OReilly'
112
+ assert_equal 'The Pragmatic Bookshelf', @bib['rails'].publisher
113
+ end
114
+
115
+ it 'passes each entry with matching fields to the block if given' do
116
+ years = []
117
+ @bib.unify(:publisher, /reilly/i) { |e| years << e.year.to_s }
118
+ assert_equal ['2007','2008'], years.sort
119
+ end
120
+
121
+ it 'returns the bibliography' do
122
+ assert_equal @bib, @bib.unify(:publisher, /reilly/i, 'OReilly')
123
+ end
124
+ end
125
+
88
126
  it 'supports access by index' do
89
127
  assert_equal 'ruby', @bib[1].keywords
90
128
  end
@@ -139,7 +177,7 @@ module BibTeX
139
177
  end
140
178
 
141
179
  it 'supports queries with negative conditions' do
142
- assert_equal 2, @bib['@*[keywords!=ruby]'].length
180
+ assert_equal 4, @bib['@*[keywords!=ruby]'].length
143
181
  end
144
182
 
145
183
  it 'supports queries with pattern conditions' do
@@ -186,15 +224,15 @@ module BibTeX
186
224
  describe '#query' do
187
225
 
188
226
  it 'returns all elements when passed no arguments' do
189
- @bib.query.length.must_be :==, 4
227
+ @bib.query.length.must_be :==, 6
190
228
  end
191
229
 
192
230
  it 'returns all elements when passed :all and an empty condition' do
193
- @bib.query(:all, '').length.must_be :==, 4
231
+ @bib.query(:all, '').length.must_be :==, 6
194
232
  end
195
233
 
196
234
  it 'returns all entries when passed a * wildcard' do
197
- @bib.query('@*').length.must_be :==, 3
235
+ @bib.query('@*').length.must_be :==, 5
198
236
  end
199
237
 
200
238
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bibtex-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.8
4
+ version: 2.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-11 00:00:00.000000000 Z
12
+ date: 2012-05-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: latex-decode
16
- requirement: &70229986220740 !ruby/object:Gem::Requirement
16
+ requirement: &70317354105840 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.0.6
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70229986220740
24
+ version_requirements: *70317354105840
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: multi_json
27
- requirement: &70229986220220 !ruby/object:Gem::Requirement
27
+ requirement: &70317354105180 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '1.3'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70229986220220
35
+ version_requirements: *70317354105180
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70229986219680 !ruby/object:Gem::Requirement
38
+ requirement: &70317354104100 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0.9'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70229986219680
46
+ version_requirements: *70317354104100
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: racc
49
- requirement: &70229986219200 !ruby/object:Gem::Requirement
49
+ requirement: &70317354103320 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '1.4'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70229986219200
57
+ version_requirements: *70317354103320
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rdoc
60
- requirement: &70229986218720 !ruby/object:Gem::Requirement
60
+ requirement: &70317354102160 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '3.9'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70229986218720
68
+ version_requirements: *70317354102160
69
69
  description: ! "\t\tBibTeX-Ruby is the Rubyist's swiss-army-knife for all things BibTeX.
70
70
  It\n includes a parser for all common BibTeX objects (@string, @preamble,\n @comment
71
71
  and regular entries) and a sophisticated name parser that\n tokenizes correctly
@@ -182,7 +182,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
182
182
  version: '0'
183
183
  segments:
184
184
  - 0
185
- hash: -4363834808819370688
185
+ hash: -50105295349997103
186
186
  required_rubygems_version: !ruby/object:Gem::Requirement
187
187
  none: false
188
188
  requirements:
@@ -191,7 +191,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
191
191
  version: '0'
192
192
  segments:
193
193
  - 0
194
- hash: -4363834808819370688
194
+ hash: -50105295349997103
195
195
  requirements: []
196
196
  rubyforge_project:
197
197
  rubygems_version: 1.8.10