#!/usr/bin/env ruby require 'bibtex_munge' replace = false path = nil HELP = <<-EOF Usage: bibtex_munge [-hR] filename The filename should be the name of a .bib or .bcf file. If it is a .bib file, the output will consist of cleaned and normalized bibliography entries. If it is a .bcf file, in addition to cleaning and sanitizing, a subset bibliography will be created: only those entries that were actually cited in the document from which the .bcf was created will be retained. Options: --help -h show this message --replace -R rather than printing the output to STDOUT, save it as filename.bib, and move the former contents of filename.bib to filename.bib.bak EOF def bad_input puts "Invalid input!" puts HELP end define_method :do_output do |bib| if replace if path.end_with? ".bcf" path = path.chomp('.bcf') + ".bib" end bib.save_and_backup(path) else puts bib end end while ARGV.length > 0 do o = ARGV.shift case o when '-h', '--help' print HELP exit when '-R', '--replace' replace = true else if File.exists? o path = o else bad_input end end end if path if path.end_with? ".bcf" bib = BibTeX::Bibliography.from_bcf(path) bib.fix_everything! do_output(bib) elsif path.end_with? ".bib" bib = BibTeX::Bibliography.from_bib(path) bib.fix_everything! do_output(bib) else bad_input end else bad_input end