BibTeXParser - Simple BibTeX parsing

BibTeXParser is a simple parser for the BibTeX format, allowing a file containing BibTeX citations to be parsed into individual entries consisting of key-value pairs.

This is a new library, and so there are likely to be bugs and strange citation formats that I have not yet encountered. If you happen to find a citation which is not parsed properly by this library, please see the Bug Reporting section below.

Author

Jeff Shantz <x@y, where x is equal to jshantz4, y = csd.uwo.ca>

License

BibTeXParser is Copyright © 2010, Jeff Shantz, and is licensed under the MIT license. Please see LICENSE for more details

Parsing a BibTeX File

A BibTeX file can be parsed using the BibTeX::Parser class:

require 'rubygems'
require 'bibtex_parser'

entries = BibTeX::Parser.parse_bibtex_file('thesis.bib')

entries.each do |entry|
    puts entry.title
    puts entry.year
end

Parsing a BibTeX string

A string containing BibTeX content can be parse BibTeX file can also be parsed using the BibTeX::Parser class:

require 'rubygems'
require 'bibtex_parser'

bibtex =<<EOF

@inproceedings{shantz:2009,
    author = "Jeffrey Shantz",
    title = "BibTeX::Parser",
    year = "2009"
}

EOF

entries = BibTeX::Parser.parse_bibtex(bibtex)

entries.each do |entry|
    puts entry.title
    puts entry.year
end

Accessing entry data

To access entry data, you can either index into the entry, specifying the key name to get or set, or simply use “dot” notation, as shown below:

require 'rubygems'
require 'bibtex_parser'

entries = BibTeX::Parser.parse_bibtex_file('thesis.bib')
entry = entries[0]

entry.year = 2010
entry[:year] = 2010  # Equivalent

puts entry.title
puts entry[:title]  # Equivalent

Getting author data

Authors (or editors) are stored under the ‘author’ key as an array of BibTeX::Author objects. A BibTeX::Author contains an author’s name parsed into First, Middle, and Last parts. Unlike other BibTeX parsers currently in existence, this parser does not extract “von” and “jr” parts of names. This may be implemented in the future, should demand arise.

require 'rubygems'
require 'bibtex_parser'

entries = BibTeX::Parser.parse_bibtex_file('thesis.bib')
entry = entries[0]
first_author = entry.authors[0]

puts first_author.first_name
puts first_author.middle_name
puts first_author.last_name

Checking whether or not a citation is valid

The ‘valid?’ method and the ‘errors’ array will be of assistance in determining whether or not a citation is valid and, if not, why it is failing.

require 'rubygems'
require 'bibtex_parser'

entries = BibTeX::Parser.parse_bibtex_file('thesis.bib')
entry = entries[0]

unless (entry.valid?)
    entry.errors.each do |error|
        puts error
    end
end

Special entry attributes

The following attributes of the BibTexEntry class may be of interest to you:

* raw_bibtex    - Returns the raw BibTeX citation from which the entry was
                  created
* valid         - Whether or not the citation was properly parsed
* type          - The citation type (e.g. "inproceedings" or "techreport")
* key           - The entry's citation key
* errors        - Array of parsing errors, if the citation is invalid

Reporting Bugs

Should you find a bug with the parser, please send an email to the address below with the following:

* The version of the parser you are using
* The failing BibTeX citation attached

Send bug reports to: x@y, where x is equal to jshantz4, and y = csd.uwo.ca