Sha256: f029d4622c3ff94708d3901fbed22fad4acbacba5a086ebd7237c057038a9e8e

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

#!/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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bibtex_munge-0.0.1 bin/bibtex_munge