bibtex-ruby 1.3.0 → 1.3.1
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/README.md +28 -6
- data/examples/bib2html.rb +7 -8
- data/lib/bibtex/entry.rb +55 -7
- data/lib/bibtex/names.rb +15 -6
- data/lib/bibtex/value.rb +21 -5
- data/lib/bibtex/version.rb +1 -1
- data/test/bibtex/test_entry.rb +32 -2
- metadata +2 -2
data/README.md
CHANGED
@@ -2,12 +2,13 @@ BibTeX-Ruby
|
|
2
2
|
===========
|
3
3
|
|
4
4
|
BibTeX-Ruby is a fairly complete library and parser for BibTeX bibliography
|
5
|
-
files; it offers
|
5
|
+
files; it offers a rich interface to manage, search, or convert BibTeX objects in
|
6
6
|
Ruby. It is designed to support all BibTeX objects (including @comment,
|
7
7
|
string-replacements via @string, as well as string concatenation using '#')
|
8
|
-
and handles all content outside of BibTeX objects as 'meta content'
|
9
|
-
or may not be included in post-processing. BibTeX-Ruby also includes
|
10
|
-
parser to support comfortable access to the individual tokens of name
|
8
|
+
and optionally handles all content outside of BibTeX objects as 'meta content'
|
9
|
+
which may or may not be included in post-processing. BibTeX-Ruby also includes
|
10
|
+
a name parser to support comfortable access to the individual tokens of name
|
11
|
+
values.
|
11
12
|
|
12
13
|
|
13
14
|
Quickstart
|
@@ -32,9 +33,29 @@ Quickstart
|
|
32
33
|
=> 3
|
33
34
|
>> b['@article'].length
|
34
35
|
=> 0
|
35
|
-
>>
|
36
|
+
>> b['@book[year=2009]'].length
|
36
37
|
=> 1
|
37
38
|
|
39
|
+
BibTeX-Ruby helps you convert your bibliography to JSON, XML, or YAML;
|
40
|
+
alternatively, you can export to the JSON format used by
|
41
|
+
[CSL](http://citationstyles.org) processors and render the bibliography in
|
42
|
+
many styles:
|
43
|
+
|
44
|
+
>> require 'citeproc' # requires the citeproc-ruby gem
|
45
|
+
=> true
|
46
|
+
>> CiteProc.process b[:pickaxe].to_citeproc, :style => :apa
|
47
|
+
=> "Thomas, D., Fowler, C., & Hunt, A. (2009). Programming Ruby 1.9:
|
48
|
+
The Pragmatic Programmer's Guide. The Facets of Ruby.
|
49
|
+
Raleigh, North Carolina: The Pragmatic Bookshelf."
|
50
|
+
>> CiteProc.process b[:pickaxe].to_citeproc, :style => 'chicago-author-date'
|
51
|
+
=> "Thomas, Dave, Chad Fowler, and Andy Hunt. 2009. Programming Ruby 1.9:
|
52
|
+
The Pragmatic Programmer's Guide. The Facets of Ruby.
|
53
|
+
Raleigh, North Carolina: The Pragmatic Bookshelf."
|
54
|
+
>> CiteProc.process b[:pickaxe].to_citeproc, :style => :mla
|
55
|
+
=> "Thomas, Dave, Chad Fowler, and Andy Hunt. Programming Ruby 1.9:
|
56
|
+
The Pragmatic Programmer's Guide. Raleigh, North Carolina:
|
57
|
+
The Pragmatic Bookshelf, 2009."
|
58
|
+
|
38
59
|
|
39
60
|
Installation
|
40
61
|
------------
|
@@ -255,7 +276,8 @@ BibTeX to YAML converter:
|
|
255
276
|
>> BibTeX.open('example.bib').to_yaml
|
256
277
|
|
257
278
|
Look at the 'examples' directory for more elaborate examples of a BibTeX to YAML
|
258
|
-
and a BibTeX to HTML converter
|
279
|
+
and a BibTeX to HTML converter using **#to_citeproc** to format a bibliography
|
280
|
+
using [CSL](http://citationstyles.org/).
|
259
281
|
|
260
282
|
BibTeX-Ruby offers an API which lets you manipulate BibTeX objects (string
|
261
283
|
replacement, name parsing etc.); however, sometimes you just want quick access
|
data/examples/bib2html.rb
CHANGED
@@ -11,17 +11,16 @@ bib = BibTeX.open File.expand_path('../markdown.bib',__FILE__),
|
|
11
11
|
# converts each BibTeX entries to a string using Chicago style
|
12
12
|
# (all other elements are mapped to simple strings)
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
# end
|
14
|
+
begin
|
15
|
+
require 'citeproc'
|
16
|
+
rescue LoadError
|
17
|
+
puts 'this example depends on the citeproc-ruby gem'
|
18
|
+
exit
|
19
|
+
end
|
21
20
|
|
22
21
|
content = bib.replace.q('@entry, @meta_content').map do |b|
|
23
22
|
if b.entry?
|
24
|
-
|
23
|
+
CiteProc.process b.to_citeproc
|
25
24
|
else
|
26
25
|
b.to_s
|
27
26
|
end
|
data/lib/bibtex/entry.rb
CHANGED
@@ -45,9 +45,40 @@ module BibTeX
|
|
45
45
|
}).freeze
|
46
46
|
|
47
47
|
NAME_FIELDS = [:author, :editor, :translator].freeze
|
48
|
+
DATE_FIELDS = [:year].freeze
|
49
|
+
|
50
|
+
CSL_FIELDS = Hash.new {|h,k|k}.merge(Hash[*%w{
|
51
|
+
date issued
|
52
|
+
isbn ISBN
|
53
|
+
booktitle container-title
|
54
|
+
journal container-title
|
55
|
+
series collection-title
|
56
|
+
address publisher-place
|
57
|
+
pages page
|
58
|
+
number issue
|
59
|
+
url URL
|
60
|
+
doi DOI
|
61
|
+
year issued
|
62
|
+
}.map(&:intern)]).freeze
|
63
|
+
|
64
|
+
CSL_TYPES = Hash.new {|h,k|k}.merge(Hash[*%w{
|
65
|
+
booklet pamphlet
|
66
|
+
conference paper-conference
|
67
|
+
inbook chapter
|
68
|
+
incollection chapter
|
69
|
+
inproceedings paper-conference
|
70
|
+
manual book
|
71
|
+
mastersthesis thesis
|
72
|
+
misc article
|
73
|
+
phdthesis thesis
|
74
|
+
proceedings paper-conference
|
75
|
+
techreport report
|
76
|
+
unpublished manuscript
|
77
|
+
}.map(&:intern)]).freeze
|
78
|
+
|
48
79
|
|
49
80
|
attr_reader :type, :fields
|
50
|
-
def_delegators :@fields, :empty?, :each
|
81
|
+
def_delegators :@fields, :empty?, :each, :each_pair
|
51
82
|
|
52
83
|
# Creates a new instance. If a hash is given, the entry is populated accordingly.
|
53
84
|
def initialize(attributes = {})
|
@@ -90,6 +121,10 @@ module BibTeX
|
|
90
121
|
type.to_s.match(/^entry$/i) || @type == type.to_sym || super
|
91
122
|
end
|
92
123
|
|
124
|
+
def has_field?(field)
|
125
|
+
@fields.has_key?(field)
|
126
|
+
end
|
127
|
+
|
93
128
|
def method_missing(name, *args)
|
94
129
|
return self[name] if @fields.has_key?(name)
|
95
130
|
return self.send(:add, name.to_s.chop.to_sym, args[0]) if name.to_s.match(/=$/)
|
@@ -100,19 +135,25 @@ module BibTeX
|
|
100
135
|
@fields.has_key?(method.to_sym) || method.to_s.match(/=$/) || super
|
101
136
|
end
|
102
137
|
|
138
|
+
# Returns a copy of the Entry with all the field names renamed.
|
139
|
+
def rename(*arguments)
|
140
|
+
dup.rename!(*arguments)
|
141
|
+
end
|
142
|
+
|
103
143
|
# Renames the given field names unless a field with the new name already
|
104
144
|
# exists.
|
105
|
-
def rename(*arguments)
|
145
|
+
def rename!(*arguments)
|
106
146
|
Hash[*arguments.flatten].each_pair do |from,to|
|
107
|
-
if @
|
108
|
-
@
|
109
|
-
@
|
147
|
+
if @fields.has_key?(from) && !@fields.has_key?(to)
|
148
|
+
@fields[to] = @fields[from]
|
149
|
+
@fields.delete(from)
|
110
150
|
end
|
111
151
|
end
|
112
152
|
self
|
113
153
|
end
|
114
154
|
|
115
155
|
alias :rename_fields :rename
|
156
|
+
alias :rename_fields! :rename!
|
116
157
|
|
117
158
|
# Returns the value of the field with the given name.
|
118
159
|
def [](name)
|
@@ -209,11 +250,18 @@ module BibTeX
|
|
209
250
|
|
210
251
|
def to_hash(options = {})
|
211
252
|
options[:quotes] ||= %w({ })
|
212
|
-
|
253
|
+
hash = { :key => key, :type => type }
|
254
|
+
each_pair { |k,v| hash[k] = v.to_s(options) }
|
255
|
+
hash
|
213
256
|
end
|
214
257
|
|
215
258
|
def to_citeproc(options = {})
|
216
|
-
|
259
|
+
options[:quotes] ||= []
|
260
|
+
hash = { 'id' => key.to_s, 'type' => CSL_TYPES[type].to_s }
|
261
|
+
each_pair do |k,v|
|
262
|
+
hash[CSL_FIELDS[k].to_s] = k == :year ? v.to_date : v.to_citeproc(options)
|
263
|
+
end
|
264
|
+
hash
|
217
265
|
end
|
218
266
|
|
219
267
|
def to_xml(options = {})
|
data/lib/bibtex/names.rb
CHANGED
@@ -59,6 +59,14 @@ module BibTeX
|
|
59
59
|
|
60
60
|
def to_name; self; end
|
61
61
|
|
62
|
+
def to_citeproc(options = {})
|
63
|
+
map { |n| n.to_citeproc(options) }
|
64
|
+
end
|
65
|
+
|
66
|
+
def strip_braces
|
67
|
+
gsub!(/\{|\}/,'')
|
68
|
+
end
|
69
|
+
|
62
70
|
def add(name)
|
63
71
|
case
|
64
72
|
when name.is_a?(Name)
|
@@ -141,12 +149,13 @@ module BibTeX
|
|
141
149
|
end
|
142
150
|
end
|
143
151
|
|
144
|
-
def to_citeproc
|
145
|
-
{
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
152
|
+
def to_citeproc(options = {})
|
153
|
+
hash = {}
|
154
|
+
hash['family'] = family unless family.nil?
|
155
|
+
hash['given'] = given unless given.nil?
|
156
|
+
hash['suffix'] = suffix unless suffix.nil?
|
157
|
+
hash['dropping-particle'] = prefix unless prefix.nil?
|
158
|
+
hash
|
150
159
|
end
|
151
160
|
|
152
161
|
alias :family :last
|
data/lib/bibtex/value.rb
CHANGED
@@ -73,7 +73,11 @@ module BibTeX
|
|
73
73
|
when Symbol
|
74
74
|
@tokens << argument
|
75
75
|
else
|
76
|
-
|
76
|
+
if argument.respond_to?(:to_s)
|
77
|
+
@tokens << argument.to_s
|
78
|
+
else
|
79
|
+
raise(ArgumentError, "Failed to create Value from argument #{ argument.inspect }; expected String, Symbol or Value instance.")
|
80
|
+
end
|
77
81
|
end
|
78
82
|
self
|
79
83
|
end
|
@@ -158,9 +162,8 @@ module BibTeX
|
|
158
162
|
@tokens.length < 2
|
159
163
|
end
|
160
164
|
|
161
|
-
# Returns true if the
|
162
|
-
def name
|
163
|
-
end
|
165
|
+
# Returns true if the value is a BibTeX name value.
|
166
|
+
def name?; false; end
|
164
167
|
|
165
168
|
alias :names? :name?
|
166
169
|
alias :is_name? :name?
|
@@ -170,8 +173,17 @@ module BibTeX
|
|
170
173
|
end
|
171
174
|
|
172
175
|
alias :to_names :to_name
|
176
|
+
|
177
|
+
# Returns true if the Value's content looks like a date.
|
178
|
+
def date?
|
179
|
+
end
|
180
|
+
|
181
|
+
alias :is_date? :date?
|
173
182
|
|
174
|
-
|
183
|
+
# Returns the string as a citeproc date. TODO use edtf format instead.
|
184
|
+
def to_date
|
185
|
+
numeric? ? { 'date-parts' => [[to_i]] } : { 'literal' => to_s(:quotes => [])}
|
186
|
+
end
|
175
187
|
|
176
188
|
# Returns true if the Value's content is numeric.
|
177
189
|
def numeric?
|
@@ -180,6 +192,10 @@ module BibTeX
|
|
180
192
|
|
181
193
|
alias :is_numeric? :numeric?
|
182
194
|
|
195
|
+
def to_citeproc(options = {})
|
196
|
+
to_s(options)
|
197
|
+
end
|
198
|
+
|
183
199
|
# Returns true if the Value contains at least one symbol.
|
184
200
|
def symbol?
|
185
201
|
@tokens.detect { |v| v.is_a?(Symbol) }
|
data/lib/bibtex/version.rb
CHANGED
data/test/bibtex/test_entry.rb
CHANGED
@@ -3,12 +3,43 @@ require 'helper.rb'
|
|
3
3
|
module BibTeX
|
4
4
|
class EntryTest < MiniTest::Spec
|
5
5
|
|
6
|
-
context 'a new
|
6
|
+
context 'a new entry' do
|
7
7
|
should 'not be nil' do
|
8
8
|
assert Entry.new
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
+
context 'given an entry' do
|
13
|
+
setup do
|
14
|
+
@entry = Entry.new do |e|
|
15
|
+
e.type = :book
|
16
|
+
e.key = :key
|
17
|
+
e.title = 'Moby Dick'
|
18
|
+
e.author = 'Herman Melville'
|
19
|
+
e.publisher = 'Penguin'
|
20
|
+
e.address = 'New York'
|
21
|
+
e.year = 1993
|
22
|
+
e.parse_names
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
should 'support renaming of field attributes' do
|
27
|
+
@entry.rename(:title => :foo)
|
28
|
+
refute @entry.has_field?(:title)
|
29
|
+
assert_equal 'Moby Dick', @entry[:foo]
|
30
|
+
end
|
31
|
+
|
32
|
+
should 'support citeproc export' do
|
33
|
+
e = @entry.to_citeproc
|
34
|
+
assert_equal 'book', e['type']
|
35
|
+
assert_equal 'New York', e['publisher-place']
|
36
|
+
assert_equal [1993], e['issued']['date-parts'][0]
|
37
|
+
assert_equal 1, e['author'].length
|
38
|
+
assert_equal 'Herman', e['author'][0]['given']
|
39
|
+
assert_equal 'Melville', e['author'][0]['family']
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
12
43
|
def test_simple
|
13
44
|
bib = BibTeX::Bibliography.open(Test.fixtures(:entry), :debug => false)
|
14
45
|
refute_nil(bib)
|
@@ -94,6 +125,5 @@ module BibTeX
|
|
94
125
|
|
95
126
|
end
|
96
127
|
|
97
|
-
|
98
128
|
end
|
99
129
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: bibtex-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.3.
|
5
|
+
version: 1.3.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Sylvester Keil
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-14 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|