bibtex-ruby 1.0.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.
- 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
data/Manifest
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
History.txt
|
2
|
+
LICENSE
|
3
|
+
Manifest
|
4
|
+
README.rdoc
|
5
|
+
Rakefile
|
6
|
+
examples/bib2html.rb
|
7
|
+
examples/markdown.bib
|
8
|
+
lib/bibtex.rb
|
9
|
+
lib/bibtex/bibliography.rb
|
10
|
+
lib/bibtex/bibtex.y
|
11
|
+
lib/bibtex/elements.rb
|
12
|
+
lib/bibtex/entry.rb
|
13
|
+
lib/bibtex/error.rb
|
14
|
+
lib/bibtex/lexer.rb
|
15
|
+
lib/bibtex/parser.output
|
16
|
+
lib/bibtex/parser.rb
|
17
|
+
lib/bibtex/string_replacement.rb
|
18
|
+
lib/extensions/core.rb
|
19
|
+
test/bib/00_empty.bib
|
20
|
+
test/bib/01_no_bibtex.bib
|
21
|
+
test/bib/02_string.bib
|
22
|
+
test/bib/03_string.bib
|
23
|
+
test/bib/04_string_replacement.bib
|
24
|
+
test/bib/05_comment.bib
|
25
|
+
test/bib/06_preamble.bib
|
26
|
+
test/bib/07_entry.bib
|
27
|
+
test/bib/08_decoret.bib
|
28
|
+
test/bib/09_errors.bib
|
29
|
+
test/bib/10_bibdesk.bib
|
30
|
+
test/test_bibtex.rb
|
31
|
+
test/test_comment.rb
|
32
|
+
test/test_entry.rb
|
33
|
+
test/test_preamble.rb
|
34
|
+
test/test_string.rb
|
data/README.rdoc
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
= BibTeX-Ruby
|
2
|
+
|
3
|
+
The BibTeX-Ruby package contains a parser for BibTeX
|
4
|
+
bibliography files and a class structure to manage BibTeX objects in
|
5
|
+
Ruby. It is designed to support all BibTeX objects (including @comment and
|
6
|
+
string-replacements via @string) and handles all content outside of BibTeX
|
7
|
+
objects as `meta comments' which may be included in post-processing.
|
8
|
+
|
9
|
+
|
10
|
+
== Quickstart
|
11
|
+
|
12
|
+
* require 'bibtex'
|
13
|
+
* bib = BibTeX::Bibliography.open('file.bib')
|
14
|
+
|
15
|
+
|
16
|
+
== Installation
|
17
|
+
|
18
|
+
If you just want to use it:
|
19
|
+
|
20
|
+
* gem install bibtex-ruby
|
21
|
+
|
22
|
+
If you want to work with the sources:
|
23
|
+
|
24
|
+
* gem install racc
|
25
|
+
* git clone http://github.com/inukshuk/bibtex-ruby.git
|
26
|
+
* cd bibtex-ruby
|
27
|
+
* rake racc
|
28
|
+
* rake rdoc
|
29
|
+
* rake test
|
30
|
+
|
31
|
+
Or, alternatively, fork the (project on github)[http://github.com/inukshuk/bibtex-ruby.git].
|
32
|
+
|
33
|
+
|
34
|
+
== Requirements
|
35
|
+
|
36
|
+
* The parser generator {+racc+}[http://i.loveruby.net/en/projects/racc/] is required to generate parser.
|
37
|
+
* The +minitest+ gem is required to run the tests in older Ruby versions (prior to 1.9).
|
38
|
+
|
39
|
+
|
40
|
+
== Usage
|
41
|
+
|
42
|
+
Look at the `examples' directory for a simple BibTeX to HTML converter.
|
43
|
+
|
44
|
+
|
45
|
+
== The Parser
|
46
|
+
|
47
|
+
The BibTeX-Ruby parser is generated using the wonderful
|
48
|
+
{+racc+}[http://i.loveruby.net/en/projects/racc/] parser generator.
|
49
|
+
|
50
|
+
== The BibTeX Format
|
51
|
+
|
52
|
+
At first glance, the BibTeX file format seems very clear and simple;
|
53
|
+
however, there are a number of peculiarities which warrant some
|
54
|
+
explanation. The best place to start reading is probably at {your closest
|
55
|
+
ctan server}[http://www.ctan.org/get/biblio/bibtex/] where
|
56
|
+
the original +bibtex+ from 1988 still lives. Additionally, Xavier Decoret
|
57
|
+
has written
|
58
|
+
{a great summary}[http://artis.imag.fr/~Xavier.Decoret/resources/xdkbibtex/bibtex_summary.html]
|
59
|
+
of the format; another invaluable source of information is Nicolas Markey's
|
60
|
+
website[http://www.lsv.ens-cachan.fr/~markey/bibla.php]. Unfortunately,
|
61
|
+
even after consulting these documents, a number of issues remain.
|
62
|
+
Therefore, it is the purpose of this section to deliver the rationale
|
63
|
+
that went into some of the design decision in BibTeX-Ruby.
|
64
|
+
|
65
|
+
A BibTeX bibliography is typically stored in a file with the file
|
66
|
+
extension `.bib'. This file may contain any number of BibTeX objects;
|
67
|
+
everything that is not a BibTeX object is assumed to be a comment and
|
68
|
+
ignored.
|
69
|
+
|
70
|
+
The individual objects are discussed in further detail below. First, however, a
|
71
|
+
number of general remarks:
|
72
|
+
|
73
|
+
* BibTeX-Ruby begins in comment-mode, treating all text it encounters as comments.
|
74
|
+
Normally these comments are ignored; however, if you wish the parser to include
|
75
|
+
them, you can do so by adding the symbol +:meta_comments+ to the +:include+ array
|
76
|
+
in the parser's options.
|
77
|
+
* Note that string literals in BibTeX are either contained in quotes or braces;
|
78
|
+
nested quotes in a quoted literal are not escaped with a usual backslash but
|
79
|
+
must be placed inside braces. Nested braces must be balanced in literals, regardless
|
80
|
+
of whether they are surrounded by quotes or braces.
|
81
|
+
* Quoted strings and string constants (which are defined by @string objects) can be
|
82
|
+
concatted by the `#' symbol. String literals in braces can not be concatted in
|
83
|
+
this way.
|
84
|
+
* The `@' symbol may only occur in quoted string literals (not in braced out literals)
|
85
|
+
in the original BibTeX; note, however, that this is not true for BibTeX-Ruby (i.e.,
|
86
|
+
it will parse any string containing an `@').
|
87
|
+
|
88
|
+
=== @comment
|
89
|
+
|
90
|
+
The purpose of the @comment object is not entirely clear, because everything
|
91
|
+
outside of an object is treated as a comment anyway. Nicolas Markay argues that
|
92
|
+
a @comment makes it possible to quickly comment out a number of consecutive
|
93
|
+
objects; however, as Xavier Decoret points out that this does not work with the
|
94
|
+
original +bibtex+ program (following a @comment, it simply ignores everything
|
95
|
+
until the end of the line). Indeed, on page 13 of {the original
|
96
|
+
documentation}[http://www.ctan.org/get/biblio/bibtex/contrib/doc/btxdoc.pdf],
|
97
|
+
Oren Patashnik explains that @comment objects are not really necessary; they
|
98
|
+
exist only for _Scribe_ system compatibility.
|
99
|
+
|
100
|
+
Because they would be useless otherwise, BibTeX-Ruby treats @comment objects
|
101
|
+
as Nicolas Markay describes them: thus, everything inside a @comment is treated
|
102
|
+
as a comment and is ignored—everything,
|
103
|
+
that is, until the object is closed. For this reason, BibTeX-Ruby assumes that
|
104
|
+
braces inside a @comment are balanced! Obviously, BibTeX-Ruby differs from
|
105
|
+
+bibtex+ in that respect; though, the gain is, that it is now possible to
|
106
|
+
comment out a sequence of entries, without removing their respective `@' symbols.
|
107
|
+
|
108
|
+
=== @string
|
109
|
+
|
110
|
+
The @string object defines a single string constant (for multiple constant
|
111
|
+
assignments, it is necessary to define separate @string objects). These
|
112
|
+
constants can be used within string assignments in other @string or @preamble
|
113
|
+
objects, as well as in regular BibTeX entries. For example, this is a valid constant
|
114
|
+
definition and usage:
|
115
|
+
|
116
|
+
* @string{ generator = "BibTeX-Ruby"}
|
117
|
+
* @preamble{ "This bibliography was generated by " # generator }
|
118
|
+
|
119
|
+
|
120
|
+
=== @preamble
|
121
|
+
|
122
|
+
Typically, the purpose of @preamble objects is to define LaTeX statements, which
|
123
|
+
will be put into the `.bbl' file by +bibtex+. A @preamble object may contain
|
124
|
+
a single string literal, a single string constant (defined by a @string object), or
|
125
|
+
a concatenation of literals and constants.
|
126
|
+
|
127
|
+
=== Entries
|
128
|
+
|
129
|
+
These represent proper BibTeX objects (e.g., @book, @collection, etc.).
|
130
|
+
|
131
|
+
|
132
|
+
== Credits
|
133
|
+
|
134
|
+
The BibTeX-Ruby package was written by {Sylvester Keil}[http://sylvester.keil.or.at/].
|
135
|
+
|
136
|
+
== License
|
137
|
+
|
138
|
+
BibTeX-Ruby
|
139
|
+
Copyright (C) 2010-2011 {Sylvester Keil}[http://sylvester.keil.or.at]
|
140
|
+
|
141
|
+
This program is free software: you can redistribute it and/or modify
|
142
|
+
it under the terms of the GNU General Public License as published by
|
143
|
+
the Free Software Foundation, either version 3 of the License, or
|
144
|
+
(at your option) any later version.
|
145
|
+
|
146
|
+
This program is distributed in the hope that it will be useful,
|
147
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
148
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
149
|
+
GNU General Public License for more details.
|
150
|
+
|
151
|
+
You should have received a copy of the GNU General Public License
|
152
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rake'
|
5
|
+
require 'rake/clean'
|
6
|
+
require 'rake/rdoctask'
|
7
|
+
require 'rake/testtask'
|
8
|
+
require 'echoe'
|
9
|
+
|
10
|
+
Echoe.new('bibtex-ruby', '1.0.0') do |p|
|
11
|
+
p.description = "A BibTeX parser written in Ruby"
|
12
|
+
p.url = "http://github.com/inukshuk/bibtex-ruby"
|
13
|
+
p.author = "Sylvester Keil"
|
14
|
+
p.email = "http://sylvester.keil.or.at"
|
15
|
+
p.ignore_pattern = []
|
16
|
+
p.development_dependencies = [['racc', '>=1.4.6']]
|
17
|
+
p.need_tgz = true
|
18
|
+
p.rdoc_options = ["--line-numbers", "--inline-source", "--title", "BibTeX-Ruby Documentation", "--main", "README.rdoc"]
|
19
|
+
end
|
20
|
+
|
21
|
+
Rake::RDocTask.new(:rdoc_task) do |rd|
|
22
|
+
rd.main = 'README.rdoc'
|
23
|
+
rd.title = "BibTeX-Ruby Documentation"
|
24
|
+
rd.rdoc_files.include('README.rdoc',"lib/**/*.rb")
|
25
|
+
rd.rdoc_dir = "doc/html"
|
26
|
+
rd.options << '--webcvs=http://github.com/inukshuk/bibtex-ruby/tree/master/'
|
27
|
+
end
|
28
|
+
|
29
|
+
Rake::TestTask.new(:test_task) do |t|
|
30
|
+
t.libs << "test"
|
31
|
+
t.test_files = FileList['test/test*.rb']
|
32
|
+
t.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :default => ['racc']
|
36
|
+
|
37
|
+
desc 'Generates the BibTeX parser'
|
38
|
+
task :racc => ['lib/bibtex/parser.rb']
|
39
|
+
|
40
|
+
desc 'Generates RDoc documentation for BibTeX-Ruby'
|
41
|
+
task :rdoc => ['clean','racc','rdoc_task']
|
42
|
+
|
43
|
+
task :test => ['racc','test_task']
|
44
|
+
|
45
|
+
file 'lib/bibtex/parser.rb' => ['lib/bibtex/bibtex.y'] do
|
46
|
+
sh 'racc -v -g -o lib/bibtex/parser.rb lib/bibtex/bibtex.y'
|
47
|
+
end
|
48
|
+
|
49
|
+
CLEAN.include('lib/bibtex/parser.rb')
|
50
|
+
CLEAN.include('lib/bibtex/parser.output')
|
51
|
+
CLEAN.include('doc/html')
|
52
|
+
|
53
|
+
|
54
|
+
# vim: syntax=ruby
|
data/bibtex-ruby.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{bibtex-ruby}
|
5
|
+
s.version = "1.0.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Sylvester Keil"]
|
9
|
+
s.cert_chain = ["/Users/sylvester/.gem/keys/gem-public_cert.pem"]
|
10
|
+
s.date = %q{2011-01-18}
|
11
|
+
s.description = %q{A (fairly complete) BibTeX parser written in Ruby}
|
12
|
+
s.email = %q{http://sylvester.keil.or.at}
|
13
|
+
s.extra_rdoc_files = ["LICENSE", "README.rdoc", "lib/bibtex.rb", "lib/bibtex/bibliography.rb", "lib/bibtex/bibtex.y", "lib/bibtex/elements.rb", "lib/bibtex/entry.rb", "lib/bibtex/error.rb", "lib/bibtex/lexer.rb", "lib/bibtex/parser.output", "lib/bibtex/parser.rb", "lib/bibtex/string_replacement.rb", "lib/extensions/core.rb"]
|
14
|
+
s.files = ["History.txt", "LICENSE", "Manifest", "README.rdoc", "Rakefile", "examples/bib2html.rb", "examples/markdown.bib", "lib/bibtex.rb", "lib/bibtex/bibliography.rb", "lib/bibtex/bibtex.y", "lib/bibtex/elements.rb", "lib/bibtex/entry.rb", "lib/bibtex/error.rb", "lib/bibtex/lexer.rb", "lib/bibtex/parser.output", "lib/bibtex/parser.rb", "lib/bibtex/string_replacement.rb", "lib/extensions/core.rb", "test/bib/00_empty.bib", "test/bib/01_no_bibtex.bib", "test/bib/02_string.bib", "test/bib/03_string.bib", "test/bib/04_string_replacement.bib", "test/bib/05_comment.bib", "test/bib/06_preamble.bib", "test/bib/07_entry.bib", "test/bib/08_decoret.bib", "test/bib/09_errors.bib", "test/bib/10_bibdesk.bib", "test/test_bibtex.rb", "test/test_comment.rb", "test/test_entry.rb", "test/test_preamble.rb", "test/test_string.rb", "bibtex-ruby.gemspec"]
|
15
|
+
s.homepage = %q{http://github.com/inukshuk/bibtex-ruby}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "BibTeX-Ruby Documentation", "--main", "README.rdoc"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{bibtex-ruby}
|
19
|
+
s.rubygems_version = %q{1.3.7}
|
20
|
+
s.signing_key = %q{/Users/sylvester/.gem/keys/gem-private_key.pem}
|
21
|
+
s.summary = %q{A BibTeX parser written in Ruby}
|
22
|
+
s.test_files = ["test/test_bibtex.rb", "test/test_comment.rb", "test/test_entry.rb", "test/test_preamble.rb", "test/test_string.rb"]
|
23
|
+
|
24
|
+
if s.respond_to? :specification_version then
|
25
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
26
|
+
s.specification_version = 3
|
27
|
+
|
28
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
29
|
+
s.add_development_dependency(%q<racc>, [">= 1.4.6"])
|
30
|
+
else
|
31
|
+
s.add_dependency(%q<racc>, [">= 1.4.6"])
|
32
|
+
end
|
33
|
+
else
|
34
|
+
s.add_dependency(%q<racc>, [">= 1.4.6"])
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bibtex'
|
3
|
+
|
4
|
+
|
5
|
+
# Open a bibliography file
|
6
|
+
bib = BibTeX::Bibliography.open(File.expand_path('../markdown.bib',__FILE__),
|
7
|
+
:include => [:meta_comments])
|
8
|
+
|
9
|
+
|
10
|
+
# Convert the BibTeX entries into a simple text format
|
11
|
+
content = bib.data.map do |d|
|
12
|
+
result = ''
|
13
|
+
|
14
|
+
if d.class == BibTeX::Entry
|
15
|
+
d.replace!(bib.strings)
|
16
|
+
result = [d[:author], '. ', d[:title], '. ', d[:publisher], ': ', d[:address], ', ', d[:year], '.'].join
|
17
|
+
end
|
18
|
+
|
19
|
+
if d.class == BibTeX::MetaComment
|
20
|
+
result = d.to_s
|
21
|
+
end
|
22
|
+
|
23
|
+
result
|
24
|
+
end
|
25
|
+
|
26
|
+
# Convert all non BibTeX text (i.e., the `meta comments') using the maruku gem
|
27
|
+
require 'maruku'
|
28
|
+
puts Maruku.new(content.join).to_html_document
|
@@ -0,0 +1,39 @@
|
|
1
|
+
Bibliography
|
2
|
+
============
|
3
|
+
|
4
|
+
Here is a little bibliography to illustrate how *BibTeX* and
|
5
|
+
*Markdown* can be seamlessly integrated. This is a _valid_ BibTex
|
6
|
+
File; at the same time its comments are written using Markdown
|
7
|
+
markup. For example, this is a list:
|
8
|
+
|
9
|
+
* More interestingly, though, we can also include
|
10
|
+
* any BibTeX object we want.
|
11
|
+
|
12
|
+
It will not show up in the HTML, but here we define a string:
|
13
|
+
|
14
|
+
@string{ thoreau = "Thoreau, Henry David" }
|
15
|
+
@string{ nyc = "New York" }
|
16
|
+
|
17
|
+
Now we can add a few BibTeX entries and convert them to text
|
18
|
+
for inclusion.
|
19
|
+
|
20
|
+
In the HTML version we will print the BibTeX entries in a simple
|
21
|
+
text format instead.
|
22
|
+
|
23
|
+
@book{key,
|
24
|
+
author = thoreau,
|
25
|
+
title = {Walden; or Life in the Woods},
|
26
|
+
publisher = "Dover",
|
27
|
+
address = nyc,
|
28
|
+
year = 1995
|
29
|
+
}
|
30
|
+
|
31
|
+
@book{poe,
|
32
|
+
author = {Poe, Edgar Allen},
|
33
|
+
title = "Poetry, Tales, and Selected Essays",
|
34
|
+
address = nyc,
|
35
|
+
publisher = "Library of America",
|
36
|
+
year = 1996
|
37
|
+
}
|
38
|
+
|
39
|
+
Neat, huh?
|
data/lib/bibtex.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
#--
|
2
|
+
# BibTeX-Ruby
|
3
|
+
# Copyright (C) 2010-2011 Sylvester Keil <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
|
+
# = BibTeX
|
20
|
+
#
|
21
|
+
# This module encompasses a parser for BibTeX files and
|
22
|
+
# auxiliary classes to model the individual
|
23
|
+
# BibTeX objects: +String+, +Preamble+, +Comment+, and
|
24
|
+
# +Entry+.
|
25
|
+
#
|
26
|
+
# Author:: {Sylvester Keil}[http://sylvester.keil.or.at]
|
27
|
+
# Copyright:: Copyright (c) 2010 Sylvester Keil
|
28
|
+
# License:: GNU GPL 3.0
|
29
|
+
#
|
30
|
+
module BibTeX
|
31
|
+
require 'logger'
|
32
|
+
|
33
|
+
# The current library version.
|
34
|
+
VERSION = '1.0.0'
|
35
|
+
|
36
|
+
#
|
37
|
+
# An instance of the Ruby core class +Logger+.
|
38
|
+
# Used for logging by BibTeX-Ruby.
|
39
|
+
#
|
40
|
+
Log = Logger.new(STDERR)
|
41
|
+
Log.level = ENV.has_key?('DEBUG') ? Logger::DEBUG : Logger::WARN
|
42
|
+
Log.datetime_format = "%Y-%m-%d %H:%M:%S"
|
43
|
+
|
44
|
+
require 'bibtex/string_replacement'
|
45
|
+
require 'bibtex/elements'
|
46
|
+
require 'bibtex/entry'
|
47
|
+
require 'bibtex/error'
|
48
|
+
require 'bibtex/parser'
|
49
|
+
require 'bibtex/bibliography'
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
#--
|
2
|
+
# BibTeX-Ruby
|
3
|
+
# Copyright (C) 2010 Sylvester Keil <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
|
+
|
21
|
+
#
|
22
|
+
# The Bibliography class models a BibTeX bibliography;
|
23
|
+
# typically, it corresponds to a `.bib' file.
|
24
|
+
#
|
25
|
+
class Bibliography
|
26
|
+
|
27
|
+
attr_accessor :path
|
28
|
+
attr_reader :data, :strings, :entries, :errors
|
29
|
+
|
30
|
+
#
|
31
|
+
# Opens and parses the `.bib' file at the given +path+. Returns
|
32
|
+
# a new Bibliography instance corresponding to the file.
|
33
|
+
#
|
34
|
+
# The options argument is passed on to BibTeX::Parser.new.
|
35
|
+
#
|
36
|
+
def self.open(path, options={})
|
37
|
+
Log.debug('Opening file ' + path.to_s)
|
38
|
+
BibTeX::Parser.new(options).parse(File.read(path))
|
39
|
+
end
|
40
|
+
|
41
|
+
#
|
42
|
+
# Creates a new bibliography; empty if no path is specified, otherwise
|
43
|
+
# by parsing the file at the given path.
|
44
|
+
#
|
45
|
+
def initialize(data=[])
|
46
|
+
@path = path
|
47
|
+
@data = []
|
48
|
+
@strings = {}
|
49
|
+
@entries = {}
|
50
|
+
@errors = []
|
51
|
+
add(data)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Adds a new element, or a list of new elements to the bibliography.
|
55
|
+
def add(data)
|
56
|
+
raise(ArgumentError,'BibTeX::Bibliography.add data expected to be enumerable or of type BibTeX::Element; was: ' + data.class.name) unless data.respond_to?(:each) || data.kind_of?(Element)
|
57
|
+
data.kind_of?(Element) ? self << data : data.each { |d| self << d }
|
58
|
+
self
|
59
|
+
end
|
60
|
+
|
61
|
+
# Saves the bibliography to the current path.
|
62
|
+
def save
|
63
|
+
save_to(@path)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Saves the bibliography to a file at the given path.
|
67
|
+
def save_to(path)
|
68
|
+
File.write(path,to_s)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Add an object to the bibliography. Returns the bibliography.
|
72
|
+
def <<(obj)
|
73
|
+
raise(ArgumentError, 'A BibTeX::Bibliography can contain only BibTeX::Elements; was: ' + obj.class.name) unless obj.kind_of?(Element)
|
74
|
+
@data << obj.added_to_bibliography(self)
|
75
|
+
self
|
76
|
+
end
|
77
|
+
|
78
|
+
# Delete an object from the bibliography. Returns the object, or nil
|
79
|
+
# if the object was not part of the bibliography.
|
80
|
+
def delete(obj)
|
81
|
+
@data.delete(obj.removed_from_bibliography(self))
|
82
|
+
end
|
83
|
+
|
84
|
+
def delete_all
|
85
|
+
@data.each { |obj| obj.removed_from_bibliography(self) }
|
86
|
+
@data = []
|
87
|
+
end
|
88
|
+
|
89
|
+
# Returns all @preamble objects.
|
90
|
+
def preamble
|
91
|
+
find_by_type(BibTeX::Preamble)
|
92
|
+
end
|
93
|
+
|
94
|
+
# Returns the first entry with a given key.
|
95
|
+
def [](key)
|
96
|
+
@entries[key.to_s]
|
97
|
+
end
|
98
|
+
|
99
|
+
# Returns all @comment objects.
|
100
|
+
def comments
|
101
|
+
find_by_type(BibTeX::Comment)
|
102
|
+
end
|
103
|
+
|
104
|
+
# Returns all meta comments, i.e., all text outside of BibTeX objects.
|
105
|
+
def meta_comments
|
106
|
+
find_by_type(BibTeX::MetaComment)
|
107
|
+
end
|
108
|
+
|
109
|
+
# Returns all objects which could not be parsed successfully.
|
110
|
+
def errors
|
111
|
+
@errors
|
112
|
+
end
|
113
|
+
|
114
|
+
# Returns true if there are object which could not be parsed.
|
115
|
+
def errors?
|
116
|
+
!errors.empty?
|
117
|
+
end
|
118
|
+
|
119
|
+
# Replaces all string constants which are defined in the bibliography.
|
120
|
+
#
|
121
|
+
# By default constants in @string, @preamble and entries are defined; this
|
122
|
+
# behaviour can be changed using the options argument by setting
|
123
|
+
# the :include option to a list of types.
|
124
|
+
#
|
125
|
+
# Note that strings are replaced in the order in which they occur in the
|
126
|
+
# bibliography.
|
127
|
+
#
|
128
|
+
# call-seq:
|
129
|
+
# replace_strings
|
130
|
+
# replace_strings({ :include => [BibTeX::String,BibTeX::Preamble]})
|
131
|
+
#
|
132
|
+
def replace_strings(options={})
|
133
|
+
options[:include] ||= [BibTeX::String, BibTeX::Preamble, BibTeX::Entry]
|
134
|
+
find_by_type(options[:include]).each { |e| e.replace!(@strings) if e.respond_to?(:replace!)}
|
135
|
+
end
|
136
|
+
|
137
|
+
# Returns true if the bibliography is currently empty.
|
138
|
+
def empty?
|
139
|
+
@data.empty?
|
140
|
+
end
|
141
|
+
|
142
|
+
# Returns the number of objects in the bibliography (including meta comments).
|
143
|
+
def length
|
144
|
+
@data.length
|
145
|
+
end
|
146
|
+
|
147
|
+
# Returns a string representation of the bibliography.
|
148
|
+
def to_s
|
149
|
+
@data.map(&:to_s).join
|
150
|
+
end
|
151
|
+
|
152
|
+
def to_yaml
|
153
|
+
@data.map(&:to_yaml).join
|
154
|
+
end
|
155
|
+
|
156
|
+
def to_xml
|
157
|
+
xml = REXML::Document.new
|
158
|
+
xml << REXML::XMLDecl.new('1.0','UTF-8')
|
159
|
+
root = REXML::Element.new('bibliography')
|
160
|
+
@data.each { |e| root.add_element(e.to_xml) }
|
161
|
+
xml << root
|
162
|
+
xml
|
163
|
+
end
|
164
|
+
|
165
|
+
private
|
166
|
+
|
167
|
+
def find_by_type(type)
|
168
|
+
@data.find_all { |x| type.respond_to?(:inject) ? type.inject(false) { |s,n| s || x.kind_of?(n) } : x.kind_of?(type) }
|
169
|
+
end
|
170
|
+
|
171
|
+
def find_entry(key)
|
172
|
+
entries.find { |e| e.key == key.to_s }
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|